首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写mongo查询

如何编写mongo查询
EN

Stack Overflow用户
提问于 2019-08-27 18:13:36
回答 3查看 85关注 0票数 1

如何从下面的mongodb模式中获得特定电影的可用座位总数(该电影的所有影院中都有座位)。

我需要写一个mongo查询才能得到结果

代码语言:javascript
复制
{
    "_id" : ObjectId("5d637b5ce27c7d60e5c42ae7"),
    "name" : "Bangalore",
    "movies" : [
        {
            "name" : "KGF",
            "theatres" : [
                {
                    "name" : "PVR",
                    "seats" : 45
                },
                {
                    "name" : "IMAX",
                    "seats" : 46
                }
            ]
        },
        {
            "name" : "Avengers",
            "theatres" : [
                {
                    "name" : "IMAX",
                    "seats" : 50
                }
            ]
        }
    ],
    "_class" : "com.BMS_mongo.ZZ_BMS_mongo_demo.Entity.CityInfo"
}

我写了这段代码:

代码语言:javascript
复制
db.cities.aggregate( [ 
    { "$unwind" : "$movies" }, { "$unwind" : "$theatres" } , 
    { "$group" : { _id : "$movies.theatre`enter code here`s.seats" , 
        total : { "$sum" : "$seats" } } 
    } 
] )

我的模式:

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-08-30 09:52:47

我用这个查询得到了答案..。

代码语言:javascript
复制
db.cities.aggregate( [ 
    { "$match" : { "name" : "Bangalore" } },
    { "$unwind" : "$movies" } , 
    { "$match" : {"movies.name" : "KGF"} }, 
    { "$unwind" : "$theatres" },
    { "$group" : { _id : "$movies.name", total : { "$sum" : "$movies.theatres.seats" 
                  } } } 
] )
票数 0
EN

Stack Overflow用户

发布于 2019-08-27 18:44:33

以下查询可以获得预期的输出:

代码语言:javascript
复制
db.collection.aggregate([
    {
        $unwind:"$movies"
    },
    {
        $unwind:"$movies.theatres"
    },
    {
        $group:{
            "_id":"$movies.name",
            "movie":{
                $first:"$movies.name"
            },
            "totalSeats":{
                $sum:"$movies.theatres.seats"
            }
        }
    },
    {
        $project:{
            "_id":0
        }
    }
]).pretty()

数据集:

代码语言:javascript
复制
{
    "_id" : ObjectId("5d637b5ce27c7d60e5c42ae7"),
    "name" : "Bangalore",
    "movies" : [
        {
            "name" : "KGF",
            "theatres" : [
                {
                    "name" : "PVR",
                    "seats" : 45
                },
                {
                    "name" : "IMAX",
                    "seats" : 46
                }
            ]
        },
        {
            "name" : "Avengers",
            "theatres" : [
                {
                    "name" : "IMAX",
                    "seats" : 50
                }
            ]
        }
    ],
    "_class" : "com.BMS_mongo.ZZ_BMS_mongo_demo.Entity.CityInfo"
}

输出:

代码语言:javascript
复制
{ "movie" : "Avengers", "totalSeats" : 50 }
{ "movie" : "KGF", "totalSeats" : 91 }
票数 3
EN

Stack Overflow用户

发布于 2019-08-28 06:34:41

查询:

代码语言:javascript
复制
db.movie.aggregate([{ $unwind: {  path: "$movies",} }, 
{ $unwind: {  path: "$movies.theatres",} }, 
{ $group: {  _id: "$movies.name",  "moviename": { $first: "$movies.name"  },
"totalSeats": { $sum: "$movies.theatres.seats"  }} }])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57680200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档