【发布时间】:2015-12-26 20:16:47
【问题描述】:
我想从用户输入的某个数组中查询所有具有 id 的文档:
var attackersIds = fights[i].attackersIds;
attackers = cardsCollection.find({"_id" : { "$oid" : { $in: attackersIds } } });
问题是我得到了一个错误:
MongoDB: Can't canonicalize query: BadValue unknown operator: $oid
我发现它可以通过使用ObjectId(...) 来解决,但是当我这样做时:
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} } });
我收到另一个错误:
ReferenceError: \"ObjectId\" is not defined.
我猜是因为 node.js 的旧版本,但我无法更改它,因为我的服务器提供商不允许我进行升级。
那么,如何在 mongodb 中查询 id 存储在我的 var attackersIds 数组中的文档?
示例文档:
{ "_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" }
【问题讨论】:
-
你能发一个文件的例子吗?你用猫鼬吗?
-
@VolodymyrSynytskyi 我添加了示例文档。关于猫鼬,我从来不关心它,我在服务器规范中找不到关于它的注释,所以我的回答是“也许”:P
-
你在用 Meteor 吗?
标签: javascript json mongodb mongodb-query mongojs