Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅从MongoDB中选择单个字段_Mongodb - Fatal编程技术网

仅从MongoDB中选择单个字段

仅从MongoDB中选择单个字段,mongodb,Mongodb,这是我在MongoDB中的示例数据库 {"userName":"John Smith", "followers":8075,"body":"my text 1","confirmed":"no"} {"userName":"James Bond", "followers":819, "body":"my text 2", "confirmed":"yes"} {"userName":"Julia Roberts","followers":3882,"body":"my text 3","conf

这是我在MongoDB中的示例数据库

{"userName":"John Smith", "followers":8075,"body":"my text 1","confirmed":"no"}
{"userName":"James Bond", "followers":819, "body":"my text 2", "confirmed":"yes"}
{"userName":"Julia Roberts","followers":3882,"body":"my text 3","confirmed":"yes"}
{"userName":"Matt Damon","followers":6531, "body":"my text 4","confirmed":"yes"}
{"userName":"Amy Adams","followers":3941, "body":"my text 5","confirmed":"no"}
我需要选择有3000多名追随者的用户名,以及确认帐户的位置。 这就是我想做的:

db.collection.find( { $and:[{followers: { $gt: 3000 } },  {"confirmed" : "yes"}] })

但这种方式给了我完整的匹配行,而我只需要用户名。您能提供建议吗?

您必须指定返回字段

db.collection.find({$and:[{followers:{$gt:3000},{“已确认”:“是”}},{userName:1,\u id:0})

更多细节可以找到

另外,如果您想取消显示
\u id
字段,您希望添加
\u id:0
。有关详细信息,请参见

db.collection.find( { $and:[{followers: { $gt: 3000 } }, {"confirmed" : "yes"}] }, {"userName" :  1, _id : 0 })
你可以试试

db.collection.find( { $and:[{followers: { $gt: 3000 } },  {"confirmed" : "yes"}] }).select({"userName":1})

虽然这个代码片段可以解决这个问题,但它确实有助于提高文章的质量。请记住,您将在将来为读者回答这个问题,而这些人可能不知道您的代码建议的原因