Following is my collection:
[
{
"_id": 1,
"favorite": {
"color": "red",
"foods": {
"fruits": "banana",
"fastfood": [
"burger",
"sandwich"
]
}
}
},
{
"_id": 2,
"favorite": {
"color": "green",
"foods": {
"noodles": "ramen",
"fastfood": [
"fries",
"burger",
"corn dog"
]
}
}
},
{
"_id": 3,
"favorite": {
"color": "red",
"foods": {
"soup": "cream soup"
}
}
}
]
I need to check fastfood item is Identical or not. To achieve this, I have the mongo shell query already.
db.collection.find({
$expr: {
$eq: [
[
"burger",
"sandwich"
],
"$favorite.foods.fastfood"
]
}
})
But, I need this in mongoTemplate as I am using Spring Boot Application. I can not do it so far.
I think the translation to
MongoTemplatecould be this one where you say to mongo "give me the elements wherefavorite.foods.fastfoodis["burger", "sandwich"].But it will return data if the array is exactly the same including the elements order.
So for example array
["burger","sandwich"]will work fine.But
["sandwich","burger"]will not return any data.Maybe you don't need this but it can help, you can also try this query where order is noy checked, so all arrays with these elements will be returned.
Using
MongoTemplateit can be something like this (not tested):