Can someone help me with querying all documents based on IDs from an array passed to my code via user input?
var attackersIds = fights[i].attackersIds;
attackers = cardsCollection.find({"_id" : { "$oid" : { $in: attackersIds } } });
However, I encountered an error:
MongoDB: Can't canonicalize query: BadValue unknown operator: $oid
I tried resolving it by using ObjectId(...)
as follows:
var attackersIds = fights[i].attackersIds;
for(var a=0; a<attackersIds.length; a++){
attackersIds[a] = ObjectId(attackersIds[a]);
}
attackers = cardsCollection.find({"_id" : { "$oid" : { $in: attackersIds} } });
But now I'm getting another error:
ReferenceError: \"ObjectId\" is not defined.
This seems to be due to an older version of node.js that I can't upgrade because of limitations imposed by my server provider.
So, how should I go about querying MongoDB for documents whose IDs are in my var attackersIds
array?
Here are some sample documents:
{ "_id": { "$oid": "567ee17ae4b0128ba4ce9049" }, "classId": 9, "name": "Recruit", "description": "", "type": "creature", "cost": { "yellow": 1 }, "attack": 1, "defense": 0, "hp": 1, "area": "field1", "playerId": "56590c7ce4b03fe0cf20842d" }
{ "_id": { "$oid": "567ee17ae4b0128ba4ce904a" }, "classId": 1, "name": "Farm", "description": "", "type": "building", "cost": {}, "attack": 0, "defense": 0, "hp": 5, "generatesMana": { "yellow": 1 }, "area": "hand", "playerId": "56590c7ce4b03fe0cf20842d" }
{ "_id": { "$oid": "567ee17ae4b0128ba4ce904b" }, "classId": 1, "name": "Farm", "description": "", "type": "building", "cost": {}, "attack": 0, "defense": 0, "hp": 5, "generatesMana": { "yellow": 1 }, "area": "hand", "playerId": "56590c7ce4b03fe0cf20842d" }
{ "_id": { "$oid": "567ee17ae4b0128ba4ce904c" }, "classId": 9, "name": "Recruit", "description": "", "type": "creature", "cost": { "yellow": 1 }, "attack": 1, "defense": 0, "hp": 1, "area": "deck", "playerId": "56590c7ce4b03fe0cf20842d" }