I have a collection in mongo. Sample single entry
{
"_id": {
"$oid": "6278dc35b07e447b14feef57"
},
"formId": "6278d9f7b07e447b14feef54",
"formData": {
"0": {
"value": "297CSSIB"
},
"1": {
"value": "Alba Gonzalez"
},
"2": {
"value": "Concentrix"
},
"3": {
"value": "San Salvador"
}
}
}
now I want to filter with formId and value San Salvador. (where formId = "6278d9f7b07e447b14feef54" and index 3 value of formData is San Salvador)
I am unable to find any solution for that Please help.
CodePudding user response:
With the current document structure, it is only possible, if the value San Salvador, is always present at index 3.
In that case, the following query should work:
db.collection.find({
formId: "6278d9f7b07e447b14feef54",
"formData.3.value": "San Salvador"
})
However, I suggest you change your document structure so that formData is an array since it's keys are just indexes:
{
"_id": {
"$oid": "6278dc35b07e447b14feef57"
},
"formId": "6278d9f7b07e447b14feef54",
"formData": [
{
"value": "297CSSIB"
},
{
"value": "Alba Gonzalez"
},
{
"value": "Concentrix"
},
{
"value": "San Salvador"
}
]
}
In this way, the query can be more generic, rather than index specific:
db.collection.find({
formId: "6278d9f7b07e447b14feef54",
"formData.value": "San Salvador"
})