Looking at my dataset, it appears as follows:
{
"_id" : ObjectId("58efcbb9ac808c5c0771b1b0"),
"guid" : "cda474b9-1362-4ffe-99df-5c0783fff8be",
"g" : "SomeString",
"m" : "Something else",
"r" : null,
"dh" : ISODate("2017-04-13T19:00:00.000Z"),
"ei" : 60000,
"v" : [
{},
{},
{},
{},
{
"last" : 0.0,
"sum" : 0.0,
"cnt" : 1,
"minimum" : 0.0,
"maximum" : 0.0
},
{},
{}
]
}
There are many entries similar to the one above. In the case of a specific "guid," there could be around 100 entries like the one shown. My goal is to count the number of entries where the guid is equal to SOMEGUID and the "cnt" element exists at a particular index within the array.
The challenge lies in the fact that the array index is stored in a variable, and it is derived from another source.
This line of code works effectively:
print(myguid + ": " + db.MyCollection.find({"guid" : myguid, "v.4.cnt" : { $exists : true}} ).count());
This produces output similar to:
2e605c07-54b2-49e6-8a9f-f31d3dffe57c: 28
The issue I am facing is that I do not wish to hardcode "4" in the query. I have the index saved in a variable named arrayIndex, and I aim to include it in the query.
An attempt like the following does not yield the desired result:
print(myguid + ": " + db.MyCollection.find({"guid" : myguid, "v.arrayIndex.cnt" : { $exists : true}} ).count());
Any assistance on this matter would be greatly appreciated.