这是我在MongoDB中的示例数据库
<title>{{ title_data }}</title>
我需要选择拥有超过3000个粉丝的用户名以及确认帐户的位置。 这就是我想要做的事情:
{"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"}
但这种方式给了我完整的匹配线,而我只需要userName。能告诉你吗?
答案 0 :(得分:2)
您必须指定返回字段;
db.collection.find( { $and:[{followers: { $gt: 3000 } }, {"confirmed" : "yes"}] }, {userName: 1, _id: 0})
可以找到更多详细信息here
此外,如果您要取消_id
字段,则需要添加_id: 0
。详情为here
答案 1 :(得分:0)
你可以尝试
db.collection.find( { $and:[{followers: { $gt: 3000 } }, {"confirmed" : "yes"}] }).select({"userName":1})
答案 2 :(得分:-1)
使用
db.collection.find( { $and:[{followers: { $gt: 3000 } }, {"confirmed" : "yes"}] }, {"userName" : 1, _id : 0 })