mongo-查詢(1)——選擇顯示列


mongo通find來查找文檔。可以執行精確匹配和模糊匹配。

1. 精確匹配

#查詢全部

> db.tianyc02.find()
{ "_id" : ObjectId("50ea6eba12729d90ce6e3423"), "name" : "xttt", "age" : 111 }
{ "_id" : ObjectId("50ea6eba12729d90ce6e3424"), "name" : "xttt", "age" : 222 }
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
{ "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }

#指定一個查詢條件

> db.tianyc02.find({name:'xtt'})
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
{ "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22 }

#使用第二個參數,指定顯示的列
> db.tianyc02.find({name:'xtt'},{age:1})
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 11 }
{ "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "age" : 22 }

#使用第二個參數,指定不顯示的列
> db.tianyc02.find({name:'xtt'},{name:0})
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 11 }
{ "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "age" : 22 }

#我想把_id也去掉。
> db.tianyc02.find({name:'xtt'},{name:0,_id:0})
{ "age" : 11 }
{ "age" : 22 }
>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM