0

my documents "parents" got the folowing structure:

{childrenIdList: [23, 24, 34]}
{childrenIdList: [23, 88]}
{childrenIdList: [1, 5, 8]}

how to select parents by childId in there childrenIdList?

Such query must return first two documents of 3 in my example if childId = 23.

I tried to use elemMatch method, but seemingly it works only with objects, i.e. it would work only if my data would be {childrenIdList: [{Id: 1}, {Id: 5}, {Id: 8}]}

2 Answers 2

1

You can just use db.collection.find({childrenIdList: 23}). See the Query Arrays section in the manual for more details.

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

Comments

0
db.collection.find({"parent.childrenIdList": {$in: [23]}})

1 Comment

Can you expand this answer to explain why this works?

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.