一、find() 方法以非結構化的方式來顯示所有文檔。如果你需要以易讀的方式來讀取數據,可以使用 pretty() 方法,語法格式如下:db.collection_name.find().pretty()
1、查看數據庫下的所有的collection: show collections
2、查看collection下的數據 db.collection_name.find().pretty()
MongoDB數據更新可以使用update()函數。在執行remove()函數前先執行find()命令來判斷執行的條件是否正確,這是一個比較好的習慣。
> db.user.save({name:"xxxxx",password:"11111"})
WriteResult({ "nInserted" : 1 })
> db.user.find().pretty()
{
"_id" : ObjectId("569476a63a18f4867aecbcd3"),
"name" : "xxxxx",
"password" : "11111"
}
> db.user.remove("name":"xxxxx")
2016-01-12T11:45:41.976+0800 E QUERY [thread1] SyntaxError: missing ) after a
rgument list @(shell):1:21
> db.user.remove({"name":"xxxxx"})
WriteResult({ "nRemoved" : 1 })
>還是沒有注意檢查括號
3、find()會返回匹配文檔的所有列:
> db.user.find().pretty()
{
"_id" : ObjectId("569476a63a18f4867aecbcd3"),
"name" : "xxxxx",
"password" : "11111"
}
4、返回指定字段和_id字段(默認會返回_id字段):
db.user.find({"name":"xxxxx"},{name:1})
5、返回排除字段以外的所有字段:
db.user.find({"name":"xxxxx"},{password:0}).pretty()
所以4中要想不返回_id 字段,排除掉_id 字段即可:db.user.find({"name":"xxxxx"},{name:1, _id:0})
6、另外內嵌文檔查詢(點表示法):
> db.tao.find({"SourceInfo.Platform": "Taobao"},{SourceInfo:1}).pretty(){
"_id" : ObjectId("57467a880f281ce229632257"),
"SourceInfo" : {
"Platform" : "Taobao",
"Link" : "https://item.taobao.com/item.htm?spm=a219r.lm869.14.14.n6XAO5&id=527138809962&ns=1&abbucket=17#detail",
"Site" : "",
"SiteID" : "",
"ProductID" : "527138809962"
}
}
{
"_id" : ObjectId("57467ab60f281ce3adbd6b0a"),
"SourceInfo" : {
"Platform" : "Taobao",
"Link" : "https://item.taobao.com/item.htm?spm=a219r.lm869.14.14.n6XAO5&id=527138809962&ns=1&abbucket=17#detail",
"Site" : "",
"SiteID" : "",
"ProductID" : "527138809962"
}
}
> db.tao.find({"SourceInfo.Platform": "Taobao"},{"SourceInfo.Platform":1}).pretty()
{
"_id" : ObjectId("57467a880f281ce229632257"),
"SourceInfo" : {
"Platform" : "Taobao"
}
}
{
"_id" : ObjectId("57467ab60f281ce3adbd6b0a"),
"SourceInfo" : {
"Platform" : "Taobao"
}
}
7、當內嵌文檔變得復雜后,如鍵的值為內嵌文檔的數組,內嵌文檔的匹配需要些許技巧,例如使用$elemMatch操作符。
"SourceInfo" :
[
"Platform" : "Taobao",
"Link" : "https://item.taobao.com/item.htm?spm=a219r.lm869.14.14.n6XAO5&id=527138809962&ns=1&abbucket=17#detail",
"Site" : "",
"SiteID" : "",
"ProductID" : "527138809962"
]
> db.tao.find({},{"SourceInfo":{$elemMatch:{"Platform":"Taobao"}}, _id:0}).pretty(),【1】
二、remove()
在執行remove()函數前先執行find()命令來判斷執行的條件是否正確,這是一個比較好的習慣。
> db.user.save({name:"xxxxx",password:"11111"})
WriteResult({ "nInserted" : 1 })
> db.user.find().pretty()
{
"_id" : ObjectId("569476a63a18f4867aecbcd3"),
"name" : "xxxxx",
"password" : "11111"
}
> db.user.remove("name":"xxxxx")
2016-01-12T11:45:41.976+0800 E QUERY [thread1] SyntaxError: missing ) after a
rgument list @(shell):1:21
> db.user.remove({"name":"xxxxx"})
WriteResult({ "nRemoved" : 1 })
>還是沒有注意檢查括號
如果你想刪除所有數據,可以使用以下方式(類似常規 SQL 的 truncate 命令):
db.user.remove()
三、MongoDB 與 RDBMS Where 語句比較(表格來自http://www.runoob.com/mongodb/mongodb-query.html)
如果你熟悉常規的 SQL 數據,通過下表可以更好的理解 MongoDB 的條件語句查詢:
操作 | 格式 | 范例 | RDBMS中的類似語句 |
---|---|---|---|
等於 | {<key>:<value> } |
db.col.find({"by":"菜鳥教程"}).pretty() |
where by = '菜鳥教程' |
小於 | {<key>:{$lt:<value>}} |
db.col.find({"likes":{$lt:50}}).pretty() |
where likes < 50 |
小於或等於 | {<key>:{$lte:<value>}} |
db.col.find({"likes":{$lte:50}}).pretty() |
where likes <= 50 |
大於 | {<key>:{$gt:<value>}} |
db.col.find({"likes":{$gt:50}}).pretty() |
where likes > 50 |
大於或等於 | {<key>:{$gte:<value>}} |
db.col.find({"likes":{$gte:50}}).pretty() |
where likes >= 50 |
不等於 | {<key>:{$ne:<value>}} |
db.col.find({"likes":{$ne:50}}).pretty() |
where likes != 50 |
1、為admin添加成績50,
> db.user.update({"name":"admin"},{$set:{"grade":50}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
2、為aaaaaa添加成績99:
> db.user.update({"name":"aaaaaaaaa"},{$set:{"grade":99}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
3、查詢grade大於50的:
> db.user.find({"grade":{$gt:50}}).pretty()
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456",
"grade" : 99
}
4、查詢成績大於等於50的:
> db.user.find({"grade":{$gte:50}}).pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin",
"grade" : 50
}
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456",
"grade" : 99
}