Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在mongoose中使用ID变量数组查询find()?_Arrays_Node.js_Mongodb_Mongoose - Fatal编程技术网

Arrays 如何在mongoose中使用ID变量数组查询find()?

Arrays 如何在mongoose中使用ID变量数组查询find()?,arrays,node.js,mongodb,mongoose,Arrays,Node.js,Mongodb,Mongoose,我有一个数组p_id,它保存名为“Panel”的模式的ObjectId。现在我想从“候选者”模式中找到与p\u id数组中的id相同的parentPanel候选者 这是候选方案: var candidateSchema = new Schema ({ name: { type: String, required: true, }, votedBy: [{ type: Schema.Types.ObjectId,

我有一个数组
p_id
,它保存名为“Panel”的模式的
ObjectId
。现在我想从“候选者”模式中找到与
p\u id
数组中的id相同的
parentPanel
候选者

这是
候选方案

var candidateSchema = new Schema ({
    name: {
        type: String,
        required: true,
    },
    votedBy: [{
        type: Schema.Types.ObjectId,
        ref: 'User',
        default: null,
    }],
    parentPanel: {
        type: Schema.Types.ObjectId,
        ref: 'Panel'
    }
});
我试过这样的东西

Candidate.find({parentPanel: {$in: [p_id]})
         .then(candidates => {
               res.send(candidates)
         })
         .catch(err=>console.log(err));
这就是我得到的错误:

消息:“值转换为ObjectId失败”[ 5d8734d2bf53280d76a3915a,'+ “5d8734d2bf53280d76a3915e]”位于模型的路径“父面板”+ “候选”,名称:“CastError”,stringValue:“[5d8734d2bf53280d76a3915a,5d8734d2bf53280d76a3915e]”,种类: “ObjectId”,值:[5d8734d2bf53280d76a3915a, 5d8734d2bf53280d76a3915e],路径:“父面板”,原因: 未定义,模型:模型{Candidate}


p\u id
已经是数组了吗?因此,您在数组中搜索
[p\u id]
数组,请尝试
Candidate.find({parentPanel:{$in:p\u id})
已经尝试过了。似乎不起作用!出现了一些强制转换错误!已经尝试用mongoose.ObjectID(id)转换的id填充p\u id但仍然不起作用!您是如何获得
p_id
的?您确定这是一个字符串数组/ObjectId吗?您能展示一些您收集的示例数据吗?
CastError',stringValue:“[5d8734d2bf53280d76a3915a,5d8734d2bf53280d76a3915e]”
这是一个“字符串”而不是一个
ObjectId
值数组。充其量只能是一个“字符串”数组,mongoose中的自动广播可以处理其中的字符串。但似乎您的输入还没有被正确解析为字符串数组。硬编码
var p_id=[“5d8734d2bf53280d76a3915a”,“5d8734d2bf53280d76a3915e”]
例如,在使用
{$in:p_id}
时效果很好。请检查您的输入,因为它没有被正确发送/解析。