********************************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();