简体   繁体   English

如何查询mongodb以获取包含$ in存储在我的`var ids`数组中的ID的文档?

[英]How can I query mongodb for documents that ids are stored in my `var ids` array with $in?

I want to query all documents that has ids from some array I've passed to my code from user's input: 我想查询从用户输入传递给代码的某个数组中具有ID的所有文档:

var attackersIds = fights[i].attackersIds;
attackers = cardsCollection.find({"_id" : { "$oid" : { $in: attackersIds } } });

The problem is I get an error for it: 问题是我得到一个错误:

MongoDB: Can't canonicalize query: BadValue unknown operator: $oid

I have found that it can be solved by using ObjectId(...) but when I do so: 我发现可以使用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} } });

I get another error: 我收到另一个错误:

ReferenceError: \"ObjectId\" is not defined.

I guess it's because of old version of node.js but I cannot change it as my server provider does not allow me to make an upgrade. 我想这是因为node.js的版本过旧,但是我无法更改它,因为我的服务器提供商不允许我进行升级。

So, how can I query mongodb for documents that ids are stored in my var attackersIds array? 因此,如何查询mongodb以获取ID存储在var attackersIds数组中的文档?

The 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" }

Try to swap order of $in and $oid 尝试交换$ in和$ oid的顺序

"_id": {
        "$in": [
            {
                "$oid": "54651022bffebc03098b4567"
            },
            {
                "$oid": "54651022bffebc03098b4568"
            }
        ]
   }

In your case, you would need small helper function, to go from array of id's to array of objects with $oid field 在您的情况下,您将需要小的辅助函数,以便从id数组到具有$ oid字段的对象数组

attackers = cardsCollection.find({"_id" : { "$in" : attackersIds.map(function(element){
    return {
        "$oid": element
    });

Try to convert your attackersIds array to array of ObjectId type. 尝试将您的AttackersIds数组转换为ObjectId类型的数组。 How you can do it depends on the package which you use to query mongodb collection. 如何执行取决于您用于查询mongodb集合的软件包。

with mongojs: 与mongojs:

var mongojs = require('mongojs');
for(var a=0; a<attackersIds.length; a++){
    attackersIds[a] = mongojs.ObjectId(attackersIds[a]);
}

with mongodb: 与mongodb:

ObjectID = require('mongodb').ObjectID;
for(var a=0; a<attackersIds.length; a++){
    attackersIds[a] = new ObjectId(attackersIds[a]);
}

Then try to do query just on _id: 然后尝试仅对_id进行查询:

attackers = cardsCollection.find({"_id" : { $in: attackersIds } });

You can try this to cast to an objectid like this: 您可以尝试将其强制转换为如下所示的对象标识:

mongoose.Types.ObjectId(string)

Where mongoose is your mongoose handle: 猫鼬是您的猫鼬把柄:

var mongoose = require('mongoose')

Or you may not need to: 否则您可能不需要:

CardsCollection.find({_id:{$in:attackersids}})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM