minjae kim
minjae kim

Reputation: 139

MongodB object array query

I want to figure out how to search object array, which has label "작성" and value is "Y"
I made find query like this {"attrs.label": "작성" , "attrs.value": "Y"}
But the problem is {..., attrs: [{label: "작성", value: "N"}, {label: "Another", value: "Y"}]}
this document was matched with my query I need full match in one array element.

I tried

{ $and: [{"attrs.label": /작성/}, {"attrs.value": "Y"}]}

Upvotes: 1

Views: 36

Answers (1)

mickl
mickl

Reputation: 49975

In such case you can use $elemMatch operator to indicate that at least one array item has to fullfill all the requirements:

db.collection.find({ attrs: { $elemMatch: { label: "작성", value: "Y" } } })

Mongo Playground

Upvotes: 1

Related Questions