mongodb增刪改查,mongodb增刪改

來源:互聯網
上載者:User

mongodb增刪改查,mongodb增刪改

********************************1.插入********************************use tblorders;--方法1db.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();--方法2db.tblorders.save({orderno: "A2014089904", pname: "atalas", scity:"烏魯木齊",sdate: "2015-08-08"} );--方法3for (var i = 1; i <= 300; i++) db.tblorders.save({id: i, name: 'ocpyang'});********************************2.更新********************************---方法:set將所有ocpyang更新為Atalasdb.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 } );--方法1db.tblorders.find();--方法2db.tblorders.find({"pname" : "kiki"});--方法3db.tblorders.find({"pname" : "kiki"}).limit(1);--方法4db.tblorders.find({"pname" : "kiki"}).forEach(printjson);db.tblorders.find({"pname": "劉德華"}).forEach(printjson);--方法5:大於、大於等於、小於、小於等於、betweendb.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小於10000db.tblorders.find({"price":{$gt: 1700,$lt : 10000}}).forEach(printjson);--方法6:不等於db.tblorders.find({"price":{$ne: 1630}}).forEach(printjson);eg:大於1300小於10000不等於1630db.tblorders.find({"price":{$ne: 1630,$gt: 1300,$lt : 10000}}).forEach(printjson);--方法7:indb.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 indb.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();


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.