Multiple Counts with single query in mongodb

前端 未结 3 774
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 07:35

I am new to Mongo Db and would appreciate some help with this query. I have been sifting through posts here for the past couple of days tearing my hair to see if I could fin

3条回答
  •  余生分开走
    2020-11-27 07:48

    db.Movies.aggregate(
    
        // Pipeline
        [
            // Stage 1
            {
                $group: {
                    _id: null,
                    Total: {
                        $sum: 1
                    },
                    docs: {
                        $push: '$$ROOT'
                    }
                }
            },
    
            // Stage 2
            {
                $project: {
                    _id: 0,
                    Total: 1,
                    Released: {
                        $filter: {
                            input: "$docs",
                            as: "doc",
                            cond: {
                                $ne: ["$$doc.ReleaseDate", ""]
                            }
                        }
                    },
                    Unreleased: {
                        $filter: {
                            input: "$docs",
                            as: "doc",
                            cond: {
                                $eq: ["$$doc.ReleaseDate", ""]
                            }
                        }
                    },
                }
            },
    
            // Stage 3
            {
                $project: {
                    Total: 1,
                    Released: {
                        $size: '$Released'
                    },
                    UnReleased: {
                        $size: '$Unreleased'
                    }
                }
            },
    
        ]
    
    
    
    );
    

提交回复
热议问题