猫鼬查找返回空数组

用户名

我有如下所示的mongoDB文件:

{
    "_id": {
        "$oid": "5b99247efb6fc01dae438815"
    },
    "participants": [
        "5b758a8341ee61f049ded486",
        "5b94fb4ffb6fc01dae40eae3"
    ]
}

Mongoose中的文档Schema定义如下

var conversationSchema = new mongoose.Schema({
    participants: [{ type: mongoose.Schema.ObjectId, ref: 'User'}],
});

我正在这样获取数据

var ccc = Conversation.find({participants : "5b758a8341ee61f049ded486"});
ccc.exec(function(err, conversations){
   res.status(200).json(conversations);
});

问题是我收到一个空数组响应[]

我认为问题出在Schema,但我不知道如何使它正常工作。

编辑,如果我将架构更改为以下内容,它将起作用:

var conversationSchema = new mongoose.Schema({
    participants: [{ type: String}],
});

但是我想使用mongoose.Schema.ObjectId而不是Strings作为外键。

维托马迪奥

尝试将Types添加到此行:

participants: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User'}]

它不会识别数组中的项目,因为它们是字符串,您应该改用{{$ oid“:” 585bb0086c57cd2265b1cbd3“}之类的对象,因此请在数据库中重新插入这些项目,然后重试。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章