如何使用 $in 查询 mongodb 以获取 ID 存储在我的 `var ids` 数组中的文档?
How can I query mongodb for documents that ids are stored in my `var ids` array with $in?
我想从我从用户输入传递到我的代码的某个数组中查询具有 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" }
尝试将您的 attackersIds 数组转换为 ObjectId 类型的数组。如何做到这一点取决于您用来查询 mongodb 集合的包。
使用 mongojs:
var mongojs = require('mongojs');
for(var a=0; a<attackersIds.length; a++){
attackersIds[a] = mongojs.ObjectId(attackersIds[a]);
}
与mongodb:
ObjectID = require('mongodb').ObjectID;
for(var a=0; a<attackersIds.length; a++){
attackersIds[a] = new ObjectId(attackersIds[a]);
}
然后尝试只查询 _id:
attackers = cardsCollection.find({"_id" : { $in: attackersIds } });
您可以像这样尝试转换为 objectid:
mongoose.Types.ObjectId(string)
猫鼬是你的猫鼬句柄:
var mongoose = require('mongoose')
或者您可能不需要:
CardsCollection.find({_id:{$in:attackersids}})
尝试调换 $in 和 $oid 的顺序
"_id": {
"$in": [
{
"$oid": "54651022bffebc03098b4567"
},
{
"$oid": "54651022bffebc03098b4568"
}
]
}
在你的情况下,你需要一个小的辅助函数,从 id 数组到具有 $oid 字段的对象数组
attackers = cardsCollection.find({"_id" : { "$in" : attackersIds.map(function(element){
return {
"$oid": element
});
我想从我从用户输入传递到我的代码的某个数组中查询具有 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" }
尝试将您的 attackersIds 数组转换为 ObjectId 类型的数组。如何做到这一点取决于您用来查询 mongodb 集合的包。
使用 mongojs:
var mongojs = require('mongojs');
for(var a=0; a<attackersIds.length; a++){
attackersIds[a] = mongojs.ObjectId(attackersIds[a]);
}
与mongodb:
ObjectID = require('mongodb').ObjectID;
for(var a=0; a<attackersIds.length; a++){
attackersIds[a] = new ObjectId(attackersIds[a]);
}
然后尝试只查询 _id:
attackers = cardsCollection.find({"_id" : { $in: attackersIds } });
您可以像这样尝试转换为 objectid:
mongoose.Types.ObjectId(string)
猫鼬是你的猫鼬句柄:
var mongoose = require('mongoose')
或者您可能不需要:
CardsCollection.find({_id:{$in:attackersids}})
尝试调换 $in 和 $oid 的顺序
"_id": {
"$in": [
{
"$oid": "54651022bffebc03098b4567"
},
{
"$oid": "54651022bffebc03098b4568"
}
]
}
在你的情况下,你需要一个小的辅助函数,从 id 数组到具有 $oid 字段的对象数组
attackers = cardsCollection.find({"_id" : { "$in" : attackersIds.map(function(element){
return {
"$oid": element
});