mongodb - Go - mgo,从集合中检索所有嵌套字段

标签 mongodb go mgo

我有一个按以下方式定义的数据库结构。

{
    name: "Jane",
    films: [
        {
            title: "The Shawshank Redemption",
            year: "1994"
        },
        {
            title: "The Godfather",
            year: "1972"
        }
    ]
},
{
    name: "Jack",
    films: [
        {
            title: "12 Angry Men",
            year: "1957"
        },
        {
            title: "The Dark Knight",
            year: "2008"
        }
    ]
}

我想退回所有影片的一部分 - []Film并且,如果可能的话,在另一个查询中所有标题的 slice - []string从集合中。我可以提取整个集合并提取应用程序逻辑中的相关数据,但是是否可以在查询中实现?我尝试与 Select() 合作方法,像这样:c.Find(nil).Select(<various conditions>).All(&results)但我没有成功。

最佳答案

我认为这是 mongo 标签上最常见的问题之一。我必须说,如果你需要,你做错了什么,也许你应该使用 RDBMS 而不是 Mongo,因为此类查询的性能下降将抵消 Mongo 功能(如无模式、“一体式”文档等)的所有利润..

无论如何,答案很简单 - 你无法获得你想要的电影列表。 Mongo 的 find 只能返回完整或部分顶级文档。我的意思是使用 db.collection.find({}, {'films': 1}) 查询可以获得的最佳结果是这样的列表

{
    films: [
        {
            title: "The Shawshank Redemption",
            year: 1994
        },
        {
            title: "The Godfather",
            year: 1972
        }
    ]
},
{
    films: [
        {
            title: "12 Angry Men",
            year: 1957
        },
        {
            title: "The Dark Knight",
            year: 2008
        }
    ]
}

这不是你所期望的,对吧?

获取数组的唯一方法

{
    title: "The Shawshank Redemption",
    year: 1994
},
{
    title: "The Godfather",
    year: 1972
},
{
    title: "12 Angry Men",
    year: 1957
},
{
    title: "The Dark Knight",
    year: 2008
}

是使用 aggregation

检索电影数组的基本 Mongo 查询是

db.collection.aggregate([{
    $unwind: '$films'
}, {
    $project: {
        title: '$films.title',
        year: '$films.year'
    }
}])

此查询的 Go 代码是

package main

import (
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
    "fmt"
)

func main() {
    session, err := mgo.Dial("mongodb://127.0.0.1:27017/db")

    if err != nil {
        panic(err)
    }
    defer session.Close()
    session.SetMode(mgo.Monotonic, true)

    c := session.DB("db").C("collection")

    pipe := c.Pipe(
        []bson.M{
            bson.M{
                "$unwind": "$films",
            },
            bson.M{
                "$project": bson.M{
                    "title": "$films.title",
                    "year": "$films.year",
                },
            },
        },
    )
    result := []bson.M{}
    err = pipe.All(&result)
    fmt.Printf("%+v", result) // [map[_id:ObjectIdHex("57a2ed6640ce01187e1c9164") title:The Shawshank Redemption year:1994] map[_id:ObjectIdHex("57a2ed6640ce01187e1c9164") title:The Godfather year:1972] map[_id:ObjectIdHex("57a2ed6f40ce01187e1c9165") title:12 Angry Men year:1957] map[year:2008 _id:ObjectIdHex("57a2ed6f40ce01187e1c9165") title:The Dark Knight]]
}

如果您需要附加条件来选择顶级文档,代码将为

pipe := c.Pipe(
    []bson.M{
        bson.M{
            "$match": bson.M{
                "name": "Jane",
            },
        },
        bson.M{
            "$unwind": "$films",
        },
        bson.M{
            "$project": bson.M{
                "title": "$films.title",
                "year": "$films.year",
            },
        },
    },
)
// result [map[_id:ObjectIdHex("57a2ed6640ce01187e1c9164") title:The Shawshank Redemption year:1994] map[title:The Godfather year:1972 _id:ObjectIdHex("57a2ed6640ce01187e1c9164")]]

如果您需要过滤电影,您可以使用下一个查询

pipe := c.Pipe(
    []bson.M{
        bson.M{
            "$unwind": "$films",
        },
        bson.M{
            "$project": bson.M{
                "title": "$films.title",
                "year": "$films.year",
            },
        },
        bson.M{
            "$match": bson.M{
                "year": bson.M{
                    "$gt": 2000,
                },
            },
        },
    },
)
// result [map[_id:ObjectIdHex("57a2ed6f40ce01187e1c9165") year:2008 title:The Dark Knight]]

聚合的问题是一个简单的事实,即大多数聚合操作不使用索引,并且在大型集合上可能会很慢。这就是为什么我建议您考虑 RDBMS,如果您需要大量聚合,它可能是更好的选择。

并且无法从 mgo 获取 []string,因为它总是返回 bson.M (或 [] bson.M),即 map[string]interface{}

关于mongodb - Go - mgo,从集合中检索所有嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38757819/

相关文章:

go - 在 Go 中实现红黑树的惯用方法是什么?

go - Go 中的 CRON 作业未按预期运行

interface - Golang接口(interface)简化依赖?

javascript - Meteor - 查询嵌入文档

mongodb - mongo : ERROR: child process failed, 退出,错误号 100

node.js - 为什么我的引用文献不填充文档?

javascript - meteor 收集架构

go - 从 goroutine 中捕获返回值

go - MGO 和长期运行的 Web 服务 - 恢复

mongodb - 在go mongodb中的From Table上进行比赛阶段