mongo学习(一)——CRUD命令

插入数据

db.c_cons.insert({
    _id : 1574824688,
    name : "李大爷",
    addr : "狗蛋村",
    mpId : 10558463215,
    status : 0
});
db.c_cons.insert({
    _id : 1574824689,
    name : "邹老湿",
    addr : "狗蛋村",
    mpId : 10558463225,
    status : 0
});

db.c_cons.insert({
    _id : 1574824690,
    name : "邹老湿",
    addr : "丫蛋村",
    mpId : 10558463125,
    status : 9
});

更新数据

db.collection.update(
   ,
   ,
   {
     upsert: ,
     multi: ,
     writeConcern: 
   }
)
  • upsert、multi为可选属性,默认为false
  • upsert=true时,通过未匹配到数据,则会插入一条新数据
  • multi=true时,通过匹配到多条数据时会批量更新

默认

db.c_cons.update({'name':'邹老湿'},
    {$set:{'status':9}},
    {upsert:false,multi:false});

multi=true

执行前
> db.c_cons.find()                                                                  });
    { "_id" : 1574824688, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463215, "status" : 0 }
    { "_id" : 1574824689, "name" : "邹老湿", "addr" : "狗蛋村", "mpId" : 10558463225, "status" : 9 }
    { "_id" : 1574824690, "name" : "邹老湿", "addr" : "丫蛋村", "mpId" : 10558463125, "status" : 9 }
执行后
> db.c_cons.update({'name':'邹老湿'},
    ... {$set:{'status':0}},
    ... {upsert:false,multi:true});
    WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
    > db.c_cons.find()                                                                  });
    { "_id" : 1574824688, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463215, "status" : 0 }
    { "_id" : 1574824689, "name" : "邹老湿", "addr" : "狗蛋村", "mpId" : 10558463225, "status" : 0 }
    { "_id" : 1574824690, "name" : "邹老湿", "addr" : "丫蛋村", "mpId" : 10558463125, "status" : 0 }

upsert=true

> db.c_cons.update({'name':'邹老湿2'},
    ... {$set:{'status':0}},
    ... {upsert:true,multi:false});
    WriteResult({
            "nMatched" : 0,
            "nUpserted" : 1,
            "nModified" : 0,
            "_id" : ObjectId("596c88cb2e6bd83e8998188e")
    })
    > db.c_cons.find()                                                                  });
    { "_id" : 1574824688, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463215, "status" : 0 }
    { "_id" : 1574824689, "name" : "邹老湿", "addr" : "狗蛋村", "mpId" : 10558463225, "status" : 0 }
    { "_id" : 1574824690, "name" : "邹老湿", "addr" : "丫蛋村", "mpId" : 10558463125, "status" : 0 }
    { "_id" : ObjectId("596c88cb2e6bd83e8998188e"), "name" : "邹老湿2", "status" : 0 }

save方法

  • save会根据id替换所有document
> db.c_cons.save({
... "_id" : 1574824689,
... "name" : "zhoulaoshi"
... });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c_cons.find()
{ "_id" : 1574824688, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463215, "status" : 0 }
{ "_id" : 1574824689, "name" : "zhoulaoshi" }
{ "_id" : 1574824690, "name" : "邹老湿", "addr" : "丫蛋村", "mpId" : 10558463125, "status" : 0 }
{ "_id" : ObjectId("596c88cb2e6bd83e8998188e"), "name" : "邹老湿2", "status" : 0 }
  • 输入不存在的id会自动插入
> db.c_cons.save({ "_id" : 1574824189, "name" : "zhoulaoshi" });
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 1574824189 })
> db.c_cons.find()
{ "_id" : 1574824688, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463215, "status" : 0 }
{ "_id" : 1574824689, "name" : "zhoulaoshi" }
{ "_id" : 1574824690, "name" : "邹老湿", "addr" : "丫蛋村", "mpId" : 10558463125, "status" : 0 }
{ "_id" : ObjectId("596c88cb2e6bd83e8998188e"), "name" : "邹老湿2", "status" : 0 }
{ "_id" : 1574824189, "name" : "zhoulaoshi" }

删除文档

> db.c_cons.find()
{ "_id" : 1574824688, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463215, "status" : 0 }
{ "_id" : 1574824689, "name" : "zhoulaoshi" }
{ "_id" : 1574824690, "name" : "邹老湿", "addr" : "丫蛋村", "mpId" : 10558463125, "status" : 0 }
{ "_id" : ObjectId("596c88cb2e6bd83e8998188e"), "name" : "邹老湿2", "status" : 0 }
{ "_id" : 1574824189, "name" : "zhoulaoshi" }
{ "_id" : ObjectId("596c8a4e5b45b2e28c94270d"), "name" : "zhoulaoshi" }
> db.c_cons.remove(
... {'name':'zhoulaoshi'},
... {justOne:true})
WriteResult({ "nRemoved" : 1 })
> db.c_cons.find();
{ "_id" : 1574824688, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463215, "status" : 0 }
{ "_id" : 1574824690, "name" : "邹老湿", "addr" : "丫蛋村", "mpId" : 10558463125, "status" : 0 }
{ "_id" : ObjectId("596c88cb2e6bd83e8998188e"), "name" : "邹老湿2", "status" : 0 }
{ "_id" : 1574824189, "name" : "zhoulaoshi" }
{ "_id" : ObjectId("596c8a4e5b45b2e28c94270d"), "name" : "zhoulaoshi" }
  • justOne默认false删除所有匹配项
> db.c_cons.remove( {'name':'zhoulaoshi'})
WriteResult({ "nRemoved" : 2 })
> db.c_cons.find();
{ "_id" : 1574824688, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463215, "status" : 0 }
{ "_id" : 1574824690, "name" : "邹老湿", "addr" : "丫蛋村", "mpId" : 10558463125, "status" : 0 }
{ "_id" : ObjectId("596c88cb2e6bd83e8998188e"), "name" : "邹老湿2", "status" : 0 }
  • 删除所有数据
    db.col.remove({})

查询文档

db.collection.find(query, projection)

  • projection作用是过滤显示的字段,默认省略,即显示全部字段
  • query 可选,查询条件,相当于sql中的where子句

条件语句查询

条件 格式 实例
等于 {:} db.c_cons.find({"name":"李大爷"})
小于 {:{$lt:}} db.c_cons.find({"mpId":{$lt:10558463200}})
小于或等于 {:{$lte:}} db.c_cons.find({"mpId":{$lte:10558463200}})
大于 {:{$gt:}} db.c_cons.find({"mpId":{$gt:10558463200}})
大于或等于 {:{$gte:}} db.c_cons.find({"mpId":{$gte:10558463200}})

AND查询

  • db.col.find({key1:value1, key2:value2})
> db.c_cons.find({"name":"李大爷"}))))
{ "_id" : 1574824688, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463215, "status" : 0 }
{ "_id" : 1574824698, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463211, "status" : 0 }
> db.c_cons.find({"name":"李大爷","mpId":10558463211})
{ "_id" : 1574824698, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463211, "status" : 0 }

OR查询

db.col.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
)
> db.c_cons.find(   {     $or:[         {"addr":"丫蛋村"},{"addr":"隔壁"}     ]   } )
{ "_id" : 1574824690, "name" : "邹老湿", "addr" : "丫蛋村", "mpId" : 10558463125, "status" : 0 }
{ "_id" : 1574824699, "name" : "老王", "addr" : "隔壁", "mpId" : 10558463211, "status" : 0 }

AND和OR联合使用

> db.c_cons.find({
      "name":"老王",
      $or:[{"addr":"狗蛋村"},{"addr":"隔壁"}]
})
{ "_id" : 1574824699, "name" : "老王", "addr" : "隔壁", "mpId" : 10558463211, "status" : 0 }
> db.c_cons.find({
...       $or:[{"name":"老王"},{"status":9}],
...       $or:[{"addr":"狗蛋村"},{"addr":"隔壁"}]
... })
{ "_id" : 1574824688, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463215, "status" : 0 }
{ "_id" : 1574824698, "name" : "李大爷", "addr" : "狗蛋村", "mpId" : 10558463211, "status" : 0 }
{ "_id" : 1574824699, "name" : "老王", "addr" : "隔壁", "mpId" : 10558463211, "status" : 0 }

$type操作

db.col.find({"title" : {$type : 2}})

  • 使用$type可以限制字段的格式
  • $type值如下
类型
Double 1
String 2
Object 3
Array 4
Binary data 5
Object id 7
Boolean 8
Date 9
Null 10
Regular Expression 11
JavaScript 12
Symbol 13
32-bit Integer 16
TimeStamp 17
64-bit Integer 18
Min-key 255
Max-key 127

你可能感兴趣的:(mongo学习(一)——CRUD命令)