一步步學MongoDB之增,刪,改查等操作

chenfeng發表於2015-12-12
C:\Users\duansf>mongo
MongoDB shell version: 2.6.6
connecting to: test
> use chenfeng
switched to db chenfeng
>
>
插入十條記錄:
> for (var i=1;i<10;i++) db.books.save({x:4,j:i});
WriteResult({ "nInserted" : 1 })
>
>
> db.books.find();
{ "_id" : ObjectId("566b793cfdb73eaa183669bf"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c0"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c1"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c2"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c3"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c4"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c5"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c6"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c7"), "x" : 4, "j" : 9 }
>

修改記錄:
> db.books.update({"j" :9},{$set:{"j" :99}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.books.find();
{ "_id" : ObjectId("566b793cfdb73eaa183669bf"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c0"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c1"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c2"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c3"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c4"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c5"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c6"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c7"), "x" : 4, "j" : 99 }
>

刪除記錄:
> db.books.remove({"x" : 4, "j" : 1})
WriteResult({ "nRemoved" : 1 })
> db.books.find();
{ "_id" : ObjectId("566b793cfdb73eaa183669c0"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c1"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c2"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c3"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c4"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c5"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c6"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c7"), "x" : 4, "j" : 99 }
>

查詢小於5的記錄
> db.books.find({"j":{$lt:5}})
{ "_id" : ObjectId("566b793cfdb73eaa183669c0"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c1"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c2"), "x" : 4, "j" : 4 }
>

查詢大於等於5的記錄
> db.books.find({"j":{$gte:5}})
{ "_id" : ObjectId("566b793cfdb73eaa183669c3"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c4"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c5"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c6"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c7"), "x" : 4, "j" : 99 }
>


查詢小於等於5的記錄
> db.books.find({"j":{$lte:5}})
{ "_id" : ObjectId("566b793cfdb73eaa183669c0"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c1"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c2"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("566b793cfdb73eaa183669c3"), "x" : 4, "j" : 5 }
>


查詢大於5小於7的記錄
> db.books.find({"j":{$gt:5,$lt:7}})
{ "_id" : ObjectId("566b793cfdb73eaa183669c4"), "x" : 4, "j" : 6 }
>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-1870811/,如需轉載,請註明出處,否則將追究法律責任。

相關文章