简体   繁体   English

猫鼬在对象数组中查找值

[英]Mongoose find in object array value

Example of Article object array: Article对象数组的示例:

[
    {
        "_id": "72",
        "title": "Some title",
        "comments": [
            {
                "title": "title 1",
                "_id": "0",
            },
            {
                "title": "title 2",
                "_id": "1",
            },
            {
                "title": "title 3",
                "_id": "2",
            }
        ]
    },
    (...)
]

I would like to find some articles by their comments title (populated) 我想按他们的评论标题找到一些文章(已填充)

var ArticleSchema = new Schema({
    title: { type: String },
    comments: [{ type: Schema.ObjectId, ref: 'Comment' }]
});

I tried a lot of things like: 我尝试了很多类似的事情:

var options = {
    criteria: {
        'comments.title': regex
    }
};

Article
    .find(options.criteria, function (err, articles) {
        if (err) throw err;
        res.json(articles);
    })
    .populate('comments');

but does not work... 但不起作用...

Thanks 谢谢

  • Update * 更新*

Here my new code: three nested find() and two forEach() 这是我的新代码:三个嵌套的find()和两个forEach()

The code in your update (or something similar) is the only way to do it with your current schema. 更新(或类似内容)中的代码是使用当前架构进行操作的唯一方法。 However, I will propose another option which may or may not work with the rest of your use case. 但是,我将提出另一个选项,该选项可能会也可能不会与您的其余用例一起使用。

First, just to make sure we're both on the same page, I believe your current setup would actually look something like this when saved to the database: 首先,为了确保我们都在同一页上,我相信您当前的设置在保存到数据库时实际上看起来像这样:

Articles Collection (A Single Entry) 文章收藏(单项)

{
    _id: ObjectId("566d820ff8633ffc494998c5"),
    title: "Example Post",
    comments: [
        ObjectId("eed7ceadc82d23441cdbab8c")
    ]
}

Comments Collection (A Single Entry) 评论集(单项)

{
    _id: ObjectId("eed7ceadc82d23441cdbab8c"),
    title: "Example Comment on Example Post"
}

This is how it works when you ref another schema. 当您ref另一个模式时,它就是这样工作的。 I bring this up because this is different than your example of an article object. 我提出这一点是因为这与您的文章对象示例不同。

Unlike relational databases (such as MySQL), MongoDB doesn't have joins. 与关系数据库(例如MySQL)不同,MongoDB没有联接。 Populate essentially works by running your query, and then running a find command for all the comment IDs. 通过运行查询,然后为所有注释ID运行find命令,可以从本质上填充。 However, MongoDB has subdocuments. 但是,MongoDB有子文档。 If you were to change your ArticleSchema to something like this: 如果要将ArticleSchema更改为以下内容:

var ArticleSchema = new Schema({
    title: { type: String },
    comments: [ CommentSchema ]
});

Your data structure in the database would match your example, and since all of your information would be in one collection, you could easily perform the search you're trying to. 数据库中的数据结构将与您的示例匹配,并且由于所有信息都在一个集合中,因此您可以轻松地执行要尝试的搜索。

However, this may make other queries you perform more difficult, so it depends on your use case. 但是,这可能会使您执行其他查询更加困难,因此取决于您的用例。

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

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