MongoDb基本的crud

 

execute entire   执行整个
execute statement cursor  执行语句光标

 execute selection  执行选择

//数据源

db.inventory.insertMany([
   
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
 
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
 
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
 
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
 
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }

]);

//往集合中插入一条文档  collection是集合(相当于数据库中的表)
db.collection.insertOne(
 { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" }
)
//往集合中插入多个文档
db.collection.insertMany([
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
 
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
 
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" }
])
//查询所有,db.collection.find(),相当于:SELECT * FROM table_name

//按条件查询db.collection.find({item:"paper"}),相当于SELECT * FROM table_name WHERE name  = ?

//指定AND条件查询,相当于SELECT * FROM inventory WHERE status = “A” AND qty < 30
db.collection.find(
   {
     item:"notebook",
      qty:{
       $lt: 70
      }
   }
 )

//指定OR条件,相当于SELECT * FROM inventory WHERE status = “A” OR qty < 30
db.collection.find(
  {
    $or:[
      {
        status:'A'
      },
      {
        item:'paper'
      }
    ]
  }
 )


//指定AND和OR条件,相当于SELECT  *  FROM  inventory  WHERE  status  =  “A”  AND  ( qty  <  30  OR  item  LIKE  “p%” )
db.collection.find(
   {
      status:'A',
      $or:[
            {qty:{
               $lt: 70
            }},
           {
            item:/^p/
           }
        ]
   }
)

//Update操作详解
//数据源
db.collection.insertMany( [
   { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
   { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
   { item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
] );

//更新某一条文档
db.collection.updateOne(
  {
    item:"notebook"
  },
  {
    $set:{
           "size.uom":'out',
            status:"B"
    },
    $currentDate:{
        lastModified:true
    }
  }
)

//更新多条文档  qty小于80更新 size.uom值为:sbb,status为草泥马,并添加一个时间$currentDate
db.collection.updateMany(
 {
   qty:{
     $lt:80
   } 
 },
 {
   $set:{
     "size.uom":'sbb',
     status:'草泥马'
   },
   $currentDate:{
     lastTime:true
   }
 }
)

//替换某一条文档 
db.collection.replaceOne(
{item:'mat'},
{item:'mat',qty:'85',status:'A',size:{h:'15',w:'20',uom:'obd'}}
)

//Delete操作详解
//数据源
db.collection.insertMany( [
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
] );

//删除所有文档
db.collection.deleteMany({})
//删除与条件匹配的所有文档
db.collection.deleteMany(
{status:'A'}
)
//删除与条件匹配的一个文档
db.collection.deleteOne(
{status:'A'}
)


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值