0

I have an array lets say [1,2,3] and a collection called 'Numbers' and it has a field called 'value'. I need to retain all the values in the array which are present against the 'value' field in any document in the collection.

Example,

Test array - [1,2,3]
Numbers collection - [{value: 1}, {value: 3}]
Result should be - [1,3]

Result is that way because '2' was not present against 'value' field in any documents within 'Numbers' collection.

How do i do this?

2
  • Where is your code? What have you done already? Commented Apr 20, 2017 at 18:56
  • I have not written the code for this logic as i dont know how to do it!! Thats why i have given what my test array is and sample collection is and what my output should be! Commented Apr 20, 2017 at 18:57

1 Answer 1

1

You can try below distinct query with projection and query filter.

db.Numbers.distinct( "value", { "value": { $in: [1,2,3] } } )
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! It works cool. If incase the 'value' is an ObjectID in a separate collection and if i need to do the same logic , how would i do that?

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.