mongo学习(一)——CRUD命令

2017-07-18  本文已影响0人  小明滚出去_

插入数据

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(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

默认

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方法

> 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 }
> 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" }
> 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.collection.find(query, projection)

条件语句查询

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

AND查询

> 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}})

类型
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
上一篇下一篇

猜你喜欢

热点阅读