Mar*_*ens 19 select rows mongodb
从我的MongoDB我想要等效的
SELECT column1, column2
FROM tbl
Run Code Online (Sandbox Code Playgroud)
使用此代码,我可以获得所有"行"以及所有"列"
DBCollection collection = database.getCollection("names");
DBCursor cursor = collection.find();
Run Code Online (Sandbox Code Playgroud)
例如,我想要所有 '行'但只有 '列':id,name,age
我该怎么做?
谢谢你的帮助!!
cjo*_*ohn 39
db.collection.find({},{_ id:1,name:1,age:1})
查找(谓词)的第一个参数是您的选择标准,例如
db.collection.find({age:{$ gte:21}})
第二个限制您检索的字段,因此对于21岁或以上的所有人的名称:
db.collection.find({age:{$ gte:21}},{name:1})
除非你专门关闭它,否则字段选择器总是拉回_id:
db.collection.find({},{_ id:0})
但是,Mongo默认不会检查字段是否存在.如果要选择某些字段,并且只匹配包含这些字段的结果,则需要使用:
db.collection.find({age:{$ exists:true}})
MongoDB网站有更详细的.find()函数说明!