Reputation: 5190
I have an odd issue querying mongo.. I insert a bunch of records like this;
{
"_id" : "1f0aad54-85ef-446c-a02b-76bb0235e49c",
"internalId" : new BinData(3, "VP0LH++FbESgK3a7AjXknA=="),
"Data" : [
["otherId", "5e3b3293-ec93-469a-ba46-101a1feb1155"],
["test", "test"],
["test2", "test2"]
]
}
Then I do a simple query;
db.testCollection.find("Data.otherId" : "5e3b3293-ec93-469a-ba46-101a1feb1155")
The otherId is a .net guid.ToString() -- several random ones obviously.. Sometimes these return. Sometimes they do not. db.find() shows them, but querying for it explicitly seems to return 0 rows at random.
I can't explain why these will intermittently fail to be found by the query.. I've tested this in my sharded cluster and on my local instance that's just vanilla out of the box.
Has anyone encountered this?
Upvotes: 1
Views: 588
Reputation: 7447
I had this same problem, and it turned out to be nothing to do with Mongo, but was an async race-condition in the calling Node.js app.
I was unwittingly using a procedural style for just one path of execution so the insert and the select query were not being run in the order I had imagined. The insert was async, but the select was then being run in the outer method, not in the callback. Sometimes the insert did get there first, presumably due to some quirk of the tick mechanism in Node.
In C# I think that the async/await pattern is less prone to making this kind of mistake, due to the more explicit await syntax, which actually leads to procedural-looking code anyway, and doesn't rely on nesting callbacks. But perhaps its still possible to be as silly as me.
Upvotes: 0
Reputation: 9202
You're using the wrong selector. If your data would be:
{
"_id" : "1f0aad54-85ef-446c-a02b-76bb0235e49c",
"internalId" : new BinData(3, "VP0LH++FbESgK3a7AjXknA=="),
"Data" : {
"otherId" "5e3b3293-ec93-469a-ba46-101a1feb1155",
"test": "test",
"test2": "test2"
}
}
that selector would be correct but you've got arrays so you should use this selector:
db.testCollection.find({"Data.0" : ["otherId","5e3b3293-ec93-469a-ba46-101a1feb1155"]})
(I just tested it)
Upvotes: 1