I have a collection like this
/* 9 */
{
"_id" : ObjectId("5a0da2f9a0d00b28dcc69b29"),
"list_id" : ObjectId("59ec6c97a0d00bc96b5680d4"),
"rowdatas" : [
{
"_id" : ObjectId("5a0da2f9a0d00b28dcc69b2a"),
"field_id" : "59ec6d33a0d00bc96b5680d8",
"field_name" : "Title2",
"field_value" : "Arvind",
"field_type" : "sltext"
},
{
"_id" : ObjectId("5a0da2faa0d00b28dcc69b2b"),
"field_id" : "59ec8a2ea0d00bc96b568111",
"field_name" : "Department",
"field_value" : "AS",
"field_type" : "dropdown"
}
]
}
/* 10 */
{
"_id" : ObjectId("5a0da306a0d00b28dcc69b2c"),
"list_id" : ObjectId("59ec6c97a0d00bc96b5680d4"),
"rowdatas" : [
{
"_id" : ObjectId("5a0da306a0d00b28dcc69b2d"),
"field_id" : "59ec6d33a0d00bc96b5680d8",
"field_name" : "Title2",
"field_value" : "Arnab",
"field_type" : "sltext"
},
{
"_id" : ObjectId("5a0da306a0d00b28dcc69b2e"),
"field_id" : "59ec8a2ea0d00bc96b568111",
"field_name" : "Department",
"field_value" : "SD",
"field_type" : "dropdown"
}
]
}
/* 11 */
{
"_id" : ObjectId("5a0da30fa0d00b28dcc69b2f"),
"list_id" : ObjectId("59ec6c97a0d00bc96b5680d4"),
"rowdatas" : [
{
"_id" : ObjectId("5a0da30fa0d00b28dcc69b30"),
"field_id" : "59ec6d33a0d00bc96b5680d8",
"field_name" : "Title2",
"field_value" : "Ankush",
"field_type" : "sltext"
},
{
"_id" : ObjectId("5a0da30fa0d00b28dcc69b31"),
"field_id" : "59ec8a2ea0d00bc96b568111",
"field_name" : "Department",
"field_value" : "SD",
"field_type" : "dropdown"
}
]
}
I'm trying to query this collection in a way that it returns only record no 9 and 10 that has the
"field_name" : "Department" and
"field_value" : "SD"
The query I have tried is
db.getCollection('ldata').find(
{ $and: [
{"list_id": ObjectId("59ec6c97a0d00bc96b5680d4")} ,
{ 'rowdatas.field_name': 'Department', 'rowdatas.field_value': 'AS' },
]
}
)
This returns the me 2 records correctly thats is 9 and 10. However, if I pass 'rowdatas.field_value': 'Arnab' it still returns me record no 10. This should return me zero result since I'm trying to find records with whose Department name is "SD" . In this case its matching the field_value without combining the field_name for the AND query.