Maxim Yefremov
Maxim Yefremov

Reputation: 14185

query find where element in array

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}]}

Upvotes: 0

Views: 31

Answers (2)

Loourr
Loourr

Reputation: 5125

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

Upvotes: 0

Gergo Erdosi
Gergo Erdosi

Reputation: 42073

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

Upvotes: 1

Related Questions