标签:mongodb增删改查
******************************** 1.插入 ******************************** use tblorders; --方法1 db.tblorders.insert( { orderno: "A2014089901", pname: "tblorders", scity:"beijing" } ); db.tblorders.insert( { orderno: "A2014089902", pname: "snow", scity:"成都" } ); db.tblorders.insert( { orderno: "A2014089903", pname: "kiki", scity:"重庆" } ); db.tblorders.find(); --方法2 db.tblorders.save({orderno: "A2014089904", pname: "atalas", scity:"乌鲁木齐",sdate: "2015-08-08"} ); --方法3 for (var i = 1; i <= 300; i++) db.tblorders.save({id: i, name: 'ocpyang'}); ******************************** 2.更新 ******************************** ---方法:set 将所有ocpyang更新为Atalas db.tblorders.update({ name: "ocpyang" }, { $set: {name: "Atalas"} },false,true) ; ---方法:$inc $inc 用法:{ $inc : { field : value } } 意思对一个数字字段field增加value,例: db.tblorders.insert( { orderno: "10001", pname: "ocpyang", scity:"重庆",price:1500 } ); db.tblorders.find({"pname":"ocpyang"}).forEach(printjson); { "_id" : ObjectId("55bf126b4726e2d2dc5f43cd"), "orderno" : "10001", "pname" : "ocpyang", "scity" : "重庆", "price" : 1500 } db.tblorders.update( { "orderno": "10001" } , { $inc : { "price" : 130 } } ); db.tblorders.find({"pname":"ocpyang"}).forEach(printjson); { "_id" : ObjectId("55bf126b4726e2d2dc5f43cd"), "orderno" : "10001", "pname" : "ocpyang", "scity" : "重庆", "price" : 1630 } ******************************** 3.删除 ******************************** $unset 用法:{ $unset : { field : 1} } --1.满足条件的一行 db.tblorders.update({ "id": 1 }, { $unset: {"naje" : 1} }) ; --2.满足条件的所有行 db.tblorders.update({ "name": "Atalas" }, { $unset: {"name" : 1}},false,true) ; ******************************** 4.查询 ******************************** db.tblorders.insert( { orderno: "10001", pname: "ocpyang", scity:"重庆",price:1500 } ); db.tblorders.insert( { orderno: "10005", pname: "luces", scity:"天津",price:1280 } ); db.tblorders.insert( { orderno: "10010", pname: "刘德华", scity:"上海浦东",ecity:"休斯顿",price:9850 } ); --方法1 db.tblorders.find(); --方法2 db.tblorders.find({"pname" : "kiki"}); --方法3 db.tblorders.find({"pname" : "kiki"}).limit(1); --方法4 db.tblorders.find({"pname" : "kiki"}).forEach(printjson); db.tblorders.find({"pname": "刘德华"}).forEach(printjson); --方法5:大于、大于等于、小于、小于等于、between db.tblorders.find({"price":{$gt: 1500}}).forEach(printjson); db.tblorders.find({"price":{$gte: 1500}}).forEach(printjson); db.tblorders.find({"price":{$lt: 1500}}).forEach(printjson); db.tblorders.find({"price":{$lte: 1500}}).forEach(printjson); --大于1700小于10000 db.tblorders.find({"price":{$gt: 1700,$lt : 10000}}).forEach(printjson); --方法6:不等于 db.tblorders.find({"price":{$ne: 1630}}).forEach(printjson); eg:大于1300小于10000不等于1630 db.tblorders.find({"price":{$ne: 1630,$gt: 1300,$lt : 10000}}).forEach(printjson); --方法7:in db.tblorders.find({"price" : {$gt : 1000}}).forEach(printjson); { "_id" : ObjectId("55bf126b4726e2d2dc5f43cd"), "orderno" : "10001", "pname" : "ocpyang", "scity" : "重庆", "price" : 1630 } { "_id" : ObjectId("55bf15bd4726e2d2dc5f43ce"), "orderno" : "10001", "pname" : "ocpyang", "scity" : "重庆", "price" : 1500 } { "_id" : ObjectId("55bf15bd4726e2d2dc5f43cf"), "orderno" : "10005", "pname" : "luces", "scity" : "天津", "price" : 1280 } { "_id" : ObjectId("55bf15be4726e2d2dc5f43d0"), "orderno" : "10010", "pname" : "刘德华", "scity" : "上海浦东", "ecity" : "休斯顿", "price" : 9850 } db.tblorders.find({"price" : {$in : [1280,1500]}}).forEach(printjson); { "_id" : ObjectId("55bf15bd4726e2d2dc5f43ce"), "orderno" : "10001", "pname" : "ocpyang", "scity" : "重庆", "price" : 1500 } { "_id" : ObjectId("55bf15bd4726e2d2dc5f43cf"), "orderno" : "10005", "pname" : "luces", "scity" : "天津", "price" : 1280 } --方法8:not in db.tblorders.find({"price" : {$nin : [1280,1500],$gt : 1000}}).forEach(printjson); { "_id" : ObjectId("55bf126b4726e2d2dc5f43cd"), "orderno" : "10001", "pname" : "ocpyang", "scity" : "重庆", "price" : 1630 } { "_id" : ObjectId("55bf15be4726e2d2dc5f43d0"), "orderno" : "10010", "pname" : "刘德华", "scity" : "上海浦东", "ecity" : "休斯顿", "price" : 9850 } --方法9:skip限制返回记录的起点 db.tblorders.find().skip(2).limit(5); #从第3条开始返回5条 --方法10:sort排序 db.tblorders.find({"price": {$gt: 0}}).sort({price : 1}).forEach(printjson); db.tblorders.find({"price" : {$gt : 0 }}).sort({price : 1}); db.tblorders.find({"price" : {$gt : 0 }}).sort({price : -1}); --方法11:游标 for( var c = db.tblorders.find({"price" : {$gt : 0 }});c.hasNext();){printjson(c.next());} db.tblorders.find({"price" : {$gt : 0 }}).forEach(function(u) {printjson(u);}); ******************************** 5.统计 ******************************** db.tblorders.find().count();
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:mongodb增删改查
原文地址:http://blog.csdn.net/yangzhawen/article/details/47320645