I have a MongoDB document with the following attributes:
{
"label": [
"ibc",
"ibd",
"ibe"
],
"location": "vochelle st"
}
and I have to return the document only if the documents label exactly matches the given array i.e., ["ibc","ibd"]
and for the same, I am using the query:
db.collection.find({"location":"vochelle st","dock_label":{"$all":["ibc", "ibd"]}})
Actual Response:
{
"label": [
"ibc",
"ibd",
"ibe"
],
"location": "vochelle st"
}
Expected Response:
{}
Since the label "ibe" doesn’t exist in the given array, the expected result has to be the empty dictionary.
3
Answers
$setIntersection
to intersect bothlabel
and input array.label
arrays are matched via$eq
.Sample Mongo Playground
Give
$size
in your querymongoplayground
If you want to check if the array exactly matches your input, you don’t need any operator, just compare it with your value: