MongoDB的。从表中仅选择1列

时间:2017-03-16 22:52:50

标签: 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":"Jamal David","followers":6531, "body":"my text 4","confirmed":"yes"}
{"userName":"jane fonda","followers":3941, "body":"my text 5","confirmed":"no"}

我需要计算名称以Ja开头的关注者数量(不区分大小写

这就是我想要做的事情:

db.test.find({$and:[{followers:{$gte:0}}, {"userName":{$regex :/^Ja/i}}]})

但它给了我bach的整个匹配字符串,而我只需要userName。你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

projection添加到查询

db.test.find({followers:{$gte:0}, userName:{$regex :/^Ja/i}}, {_id:0, userName:1})

请记住:

    除非您在投影中指定_id:0,否则
  • find始终返回_id。
  • $和隐式使用操作 - 您不需要它来指定多个字段的过滤器