Let's assume we have the following documents in the Mongo db
[
{
"Name": "Jack",
"info": {
"eyes": "brown",
"city": "paris",
"accomodation": "house"
}
},
{
"Name": "Mathew",
"info": {
"eyes": "yellow",
"city": "rome",
"accomodation": "apartment"
}
},
{
"Name": "Peter",
"info": {
"eyes": "brown",
"city": "barcelona",
"accomodation": "house",
"hair_color": "black"
}
}
]
Let's say the input JSON is
{
"eyes": "brown",
"city": "paris",
"accomodation": "house",
"hair_color": "black"
}
I would like to construct a query such that all the key-value pairs of the document should be present in the input json document but not necessarily the other way round. For the above example - the output should be
{
"Name": "Jack",
"info": {
"eyes": "brown",
"city": "paris",
"accomodation": "house"
}
}
Since all the key value pairs of Jack were met by the input json document even though the input JSON document had hair_color which wasn't present in Jack document
Mongo playground with input : https://mongoplayground.net/p/1T0ZL8yGhPW