0

when i run db.abhishek.em.find({}) I have

{ "_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.a.b.find({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

[
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

{ "_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

6
  • you might want to use toArray(). Commented Apr 5, 2018 at 14:54
  • I want to use the command nested with $in in a bigger query to achieve some sort of relational querying -> What do you really want? Commented Apr 5, 2018 at 14:55
  • toArray gives me the complete array of objects (each bieng the complete object), i just want an array of Object ids and no further nesting to use it within $in Commented Apr 5, 2018 at 14:55
  • 1
    Try db.abhishek.em.find({}, {_id:1}) Commented Apr 5, 2018 at 14:57
  • @jonas-w I basically want to use something like db.a.c.find({_id:{$in:db.a.b.find{employed:true}.somemethod_thatworkswith$in}}) Commented Apr 5, 2018 at 14:57

2 Answers 2

2

Use MongoDB to only provide those fields from the model. In MongoDB terms this is called Projection. There is a MongoDB method .project() that you can chain onto your query like this:

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/

Sign up to request clarification or add additional context in comments.

2 Comments

it cannot find project method,what could have gone wrong?
The implementations differ per platform. The example I gave was for MongoDB from NodeJS. What platform are you using? In the reference I provided above, click the tab at the top of the page that corresponds do your platform.
0

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

toArray method used to convert objects nested in array hence failing $in operator

Thanks @veeram for telling about selection of fields

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.