[英]reduce mongo db query to pure array
when i run db.abhishek.em.find({})
I have 当我运行
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 }
I want to reduce this result into an array of simple object ids like this by adding or chaining something to find function , something like db.abfind({employed:true}).somefunction() which can return the below array I want to use the command nested with $in in a bigger query to achieve some sort of relational querying 我想通过添加或链接某些东西来查找函数来将结果简化为一个简单的对象ID数组,例如db.abfind({employed:true})。somefunction()这样的函数,它可以返回以下我想使用的数组在较大的查询中嵌套有$ in的命令以实现某种关系查询
[
ObjectId("5ac62d35b075e574b3e7eeaa"),
ObjectId("5ac62d3fb075e574b3e7eeab")
]
-----------------EDIT---------------------- - - - - - - - - -编辑 - - - - - - - - - - -
For an example case I want to get employed employees by running 举个例子,我想通过跑步来获得受雇的员工
db.abhishek.another.find({id:{$in:db.abhishek.em.find({employed:true},{_id:1}).toArray()}})
or something similiar as this command is not working 或类似的东西,因为此命令不起作用
The db.a.another collection is db.a.另一个集合是
{ "_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" }
-----------------EDIT---------------------- Solved see my answer below -----------------编辑----------------------解决了,请看下面的答案
Use MongoDB to only provide those fields from the model. 使用MongoDB仅提供模型中的那些字段。 In MongoDB terms this is called Projection.
在MongoDB中,这称为Projection。 There is a MongoDB method
.project()
that you can chain onto your query like this: 您可以将MongoDB方法
.project()
链接到查询中,如下所示:
db.abhishek.em.find({}).project({});
that should do it. 应该这样做。 If you want to explicit exclude fields, you do:
如果要显式排除字段,请执行以下操作:
db.abhishek.em.find({}).project({name:0, employed:0});
then, finally, to get the output in array form do: 然后,最后以数组形式获取输出:
db.abhishek.em.find({}).project({name:0, employed:0}).toArray();
Reference here: 参考在这里:
https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/ https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/
Thanks everyone for providing directions and suggestions to move forward, I was able to successfully do some sort of relational based query using the following 感谢大家提供前进的方向和建议,我能够使用以下命令成功进行某种基于关系的查询
db.abhishek.another.find({id:{$in:db.abhishek.em.find({employed:true},{_id:1}).map(function(e) {return e._id})}},{name:1,_id:0})
It gave this as output 它将此作为输出
{ "name" : "Lets say person 1" }
{ "name" : "Lets say person 2" }
This query took all records from em collection with employed set to true,form its array and then pass it to $in operator on query for "another" collection to give output 该查询从em集合中获取了所有记录,并将employee set设置为true,形成其数组,然后将其传递给查询“另一个”集合的$ in运算符以提供输出
toArray method used to convert objects nested in array hence failing $in operator toArray方法用于转换嵌套在数组中的对象,因此$ in运算符失败
Thanks @veeram for telling about selection of fields 感谢@veeram讲述了字段选择
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.