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 }
>