【发布时间】:2018-04-05 14:50:38
【问题描述】:
当我运行db.abhishek.em.find({}) 我有
{ "_id" : ObjectId("5ac62d35b075e574b3e7eeaa"), "name" : "first", "employed" : true }
{ "_id" : ObjectId("5ac62d3fb075e574b3e7eeab"), "name" : "second", "employed" : true }
{ "_id" : ObjectId("5ac62d4eb075e574b3e7eeac"), "name" : "third", "employed" : false }
我想通过添加或链接一些东西来查找函数,将这个结果减少为一个简单的对象 id 数组,比如 db.a.b.find({employed:true}).somefunction() 可以返回以下数组 我想在更大的查询中使用嵌套在 $in 中的命令来实现某种关系查询
[
ObjectId("5ac62d35b075e574b3e7eeaa"),
ObjectId("5ac62d3fb075e574b3e7eeab")
]
------------------编辑----------------------
举个例子,我想通过跑步来雇佣员工
db.abhishek.another.find({id:{$in:db.abhishek.em.find({employed:true},{_id:1}).toArray()}})
或与此命令类似的东西不起作用
db.a.another 集合是
{ "_id" : ObjectId("5ac63de1b075e574b3e7eead"), "id" : ObjectId("5ac62d35b075e574b3e7eeaa"), "name" : "Lets say person 1" }
{ "_id" : ObjectId("5ac63df7b075e574b3e7eeae"), "id" : ObjectId("5ac62d3fb075e574b3e7eeab"), "name" : "Lets say person 2" }
{ "_id" : ObjectId("5ac63e06b075e574b3e7eeaf"), "id" : ObjectId("5ac62d4eb075e574b3e7eeac"), "name" : "Lets say person 3" }
------------------编辑---------------------- 解决了,看下面我的回答
【问题讨论】:
-
你可能想使用
toArray()。 -
I want to use the command nested with $in in a bigger query to achieve some sort of relational querying-> 你真正想要什么? -
toArray 为我提供了完整的对象数组(每个 bieng 都是完整的对象),我只想要一个对象 id 数组,而无需进一步嵌套以在 $in 中使用它
-
试试
db.abhishek.em.find({}, {_id:1}) -
@jonas-w 我基本上想使用类似 db.a.c.find({_id:{$in:db.a.b.find{employed:true}.somemethod_thatworkswith$in}}) 之类的东西
标签: javascript arrays mongodb mongodb-query aggregation-framework