0

I am having the following issue. Generally I have a collection holding objects with the following structure:

{
    _id: ObjectId("4f941bb2e4b06c6af7f80a0d"),
    fooId: ObjectId("4f941bb2e4b06c6af7e50aff"), 
    barsIds: [ObjectId("4f941bb2f5606c6af7f80ff5"), ObjectId("4f941bbc3fb06c6af7f80ccf")]
}

What is the query to find all elements in the collection that contain a given id in the barsIds property ?

Is it something like:

db.collectionName.find({"barsIds" : [ { "$oid" : "5300c6ba4a5ce5614bcd5d9a"}]})

2 Answers 2

1

This is exactly what you are looking for:

db.collectionName.find({"barsIds" : ObjectId("4f941bb2f5606c6af7f80ff5") })

This query looks for documents whose barsIds elements are given object id. Or if barsIds is an array, this query looks for docs whose barsIds elements contain given object id.

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

Comments

0

Try with something like this

db.collectionName.find({:barsIds => {"$in" => BSON::ObjectId("5300c6ba4a5ce5614bcd5d9a")}})

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.