【问题标题】:Mongoose Join List猫鼬加入列表
【发布时间】:2017-10-24 23:09:36
【问题描述】:

我是猫鼬的新手,我有两个架构如下

contentschema
{ "_id" : autogenerated
  "title": "string",
  "description": "string",
}

viewedschema
{ "_id" : autogenerated
  "contentid": "ref content",
  "viewedby": "string",
 }

所有查看过内容的用户都将存储在viewedschema集合中,该集合具有contentid引用。

注意:由于查看的记录数量会很大,我不想将内容中的查看作为嵌入文档。

在 Mongoose 中,有没有办法查看所有内容(内容架构数组)。 [类似于 SQL 中的内连接]。

提前致谢。

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    我们可以使用$lookup 来合并同一数据库的两个不同集合中的文档,并对集合执行左外连接。

    让我们下面的文件在content集合

    {
            "_id" : ObjectId("59ef51f106b0505f997f84c8"),
            "title" : "myfavoritesong",
            "description" : "A wonderful composition using string instruments"
    }
    {
            "_id" : ObjectId("59ef52ad06b0505f997f84ca"),
            "title" : "myfavoritestory",
            "description" : "An interesting short story with a twisted ending"
    }
    

    viewed 集合中的文档

    {
            "_id" : ObjectId("59ef523706b0505f997f84c9"),
            "contentid" : ObjectId("59ef51f106b0505f997f84c8"),
            "viewedby" : "user1"
    }
    {
            "_id" : ObjectId("59ef52f406b0505f997f84cb"),
            "contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
            "viewedby" : "user2"
    }
    {
            "_id" : ObjectId("59ef53c706b0505f997f84cc"),
            "contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
            "viewedby" : "user3"
    }
    

    通过组合两个集合使用 $lookup 的最终聚合查询是

    db.viewed.aggregate({
        $lookup:{
            from : "content", 
            localField: "contentid", 
            foreignField:"_id",
            as:"viewed_contents"
        }
    })
    

    我们的样本数据的聚合查询结果是

    {
            "_id" : ObjectId("59ef523706b0505f997f84c9"),
            "contentid" : ObjectId("59ef51f106b0505f997f84c8"),
            "viewedby" : "user1",
            "viewed_contents" : [
                    {
                            "_id" : ObjectId("59ef51f106b0505f997f84c8"),
                            "title" : "myfavoritesong",
                            "description" : "A wonderful composition using string in
    struments"
                    }
            ]
    }
    {
            "_id" : ObjectId("59ef52f406b0505f997f84cb"),
            "contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
            "viewedby" : "user2",
            "viewed_contents" : [
                    {
                            "_id" : ObjectId("59ef52ad06b0505f997f84ca"),
                            "title" : "myfavoritestory",
                            "description" : "An interesting short story with a twist
    ed ending"
                    }
            ]
    }
    {
            "_id" : ObjectId("59ef53c706b0505f997f84cc"),
            "contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
            "viewedby" : "user3",
            "viewed_contents" : [
                    {
                            "_id" : ObjectId("59ef52ad06b0505f997f84ca"),
                            "title" : "myfavoritestory",
                            "description" : "An interesting short story with a twist
    ed ending"
                    }
            ]
    }
    

    请注意,您还可以将 viewed 中的集合交换为外部集合,将 content 集合为本地集合

    db.content.aggregate({
       $lookup:{
         from : "viewed", 
         localField: "_id", 
         foreignField:"contentid",
         as:"contents_viewed_by"
       }
    })
    

    本次聚合查询结果如下

    {
            "_id" : ObjectId("59ef51f106b0505f997f84c8"),
            "title" : "myfavoritesong",
            "description" : "A wonderful composition using string instruments",
            "contents_viewed_by" : [
                    {
                            "_id" : ObjectId("59ef523706b0505f997f84c9"),
                            "contentid" : ObjectId("59ef51f106b0505f997f84c8"),
                            "viewedby" : "user1"
                    }
            ]
    }
    {
            "_id" : ObjectId("59ef52ad06b0505f997f84ca"),
            "title" : "myfavoritestory",
            "description" : "An interesting short story with a twisted ending",
            "contents_viewed_by" : [
                    {
                            "_id" : ObjectId("59ef52f406b0505f997f84cb"),
                            "contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
                            "viewedby" : "user2"
                    },
                    {
                            "_id" : ObjectId("59ef53c706b0505f997f84cc"),
                            "contentid" : ObjectId("59ef52ad06b0505f997f84ca"),
                            "viewedby" : "user3"
                    }
            ]
    }
    

    【讨论】:

    • 非常感谢您的详细解释
    猜你喜欢
    • 2023-03-29
    • 2023-01-27
    • 2014-07-15
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 2018-10-10
    • 1970-01-01
    相关资源
    最近更新 更多