如何从集合中检索文档总数?

时间:2018-09-05 05:57:40

标签: mongodb mongoose mongodb-query aggregation-framework

我有一个收藏集filterrecords

filterrecords模式具有以下记录

{
    "_id" : ObjectId("5b8e877a47d6f21b1065f54a"),
    "Id" : ObjectId("5b8930c09e973018f8fe7377"),
    "SubmittedOn" : ISODate("2018-08-31T12:12:48.413Z"),
    "5b7bde155cabec2148afae09" : "2018-05-12T00:00:00+0530",
    "5b7bde155cabec2148afae0a" : "Palwal",
    "5b7bde155cabec2148afae0b" : "Hafed Sale Point, Panchkula"
}{
    "_id" : ObjectId("5b8e877a47d6f21b1065f54b"),
    "Id" : ObjectId("5b89306b9e973018f8fe7322"),
    "SubmittedOn" : ISODate("2018-08-31T12:11:23.119Z"),
    "5b7bde155cabec2148afae09" : "2018-06-16T00:00:00+0530",
    "5b7bde155cabec2148afae0a" : "Sonipat",
    "5b7bde155cabec2148afae0b" : "Hafed Sale Point, Sonipat"
}{
    "_id" : ObjectId("5b8e877a47d6f21b1065f54c"),
    "Id" : ObjectId("5b892ff69e973018f8fe72cf"),
    "SubmittedOn" : ISODate("2018-08-31T12:09:26.191Z"),
    "5b7bde155cabec2148afae09" : "2018-07-21T00:00:00+0530",
    "5b7bde155cabec2148afae0a" : "Fatehabad",
    "5b7bde155cabec2148afae0b" : "Hafed Sale Point, Ratia"
}{
    "_id" : ObjectId("5b8e877a47d6f21b1065f54d"),
    "Id" : ObjectId("5b892fbe9e973018f8fe72c2"),
    "SubmittedOn" : ISODate("2018-08-31T12:08:30.755Z"),
    "5b7bde155cabec2148afae09" : "2018-08-25T00:00:00+0530",
    "5b7bde155cabec2148afae0a" : "Ambala",
    "5b7bde155cabec2148afae0b" : "Hafed Sale Point, Ambala"
}{
    "_id" : ObjectId("5b8e877a47d6f21b1065f54e"),
    "Id" : ObjectId("5b892f639e973018f8fe7273"),
    "SubmittedOn" : ISODate("2018-08-31T12:06:59.792Z"),
    "5b7bde155cabec2148afae09" : "2018-07-06T00:00:00+0530",
    "5b7bde155cabec2148afae0a" : "Sonipat",
    "5b7bde155cabec2148afae0b" : "Hafed Sale Point, Sonipat"
}{
    "_id" : ObjectId("5b8e877a47d6f21b1065f54f"),
    "Id" : ObjectId("5b892f109e973018f8fe7266"),
    "SubmittedOn" : ISODate("2018-08-31T12:05:36.121Z"),
    "5b7bde155cabec2148afae09" : "2018-08-17T00:00:00+0530",
    "5b7bde155cabec2148afae0a" : "Faridabad",
    "5b7bde155cabec2148afae0b" : "Hafed Sale Point, Faridabad"
}{
    "_id" : ObjectId("5b8e877a47d6f21b1065f550"),
    "Id" : ObjectId("5b892e129e973018f8fe721b"),
    "SubmittedOn" : ISODate("2018-08-31T12:01:22.722Z"),
    "5b7bde155cabec2148afae09" : "2018-04-05T00:00:00+0530",
    "5b7bde155cabec2148afae0a" : "Chandigarh",
    "5b7bde155cabec2148afae0b" : "Hafed Sale Point Chandigarh"
}{
    "_id" : ObjectId("5b8e877a47d6f21b1065f551"),
    "Id" : ObjectId("5b892dd39e973018f8fe71d2"),
    "SubmittedOn" : ISODate("2018-08-31T12:00:19.345Z"),
    "5b7bde155cabec2148afae09" : "2018-05-17T00:00:00+0530",
    "5b7bde155cabec2148afae0a" : "Sirsa",
    "5b7bde155cabec2148afae0b" : "Hafed Sale Point, Sirsa",
    "5b7bde155cabec2148afae0c" : "Oil",
    "5b7bde155cabec2148afae0d" : "KACHCHI GHANI MUSTARD OIL"
}{
    "_id" : ObjectId("5b8e877a47d6f21b1065f552"),
    "Id" : ObjectId("5b892d749e973018f8fe718b"),
    "SubmittedOn" : ISODate("2018-08-31T11:58:44.234Z"),
    "5b7bde155cabec2148afae09" : "2018-07-12T00:00:00+0530",
    "5b7bde155cabec2148afae0a" : "Jind",
    "5b7bde155cabec2148afae0b" : "Hafed Sale Point, Jind"
}

我想要基于SubmittedOn字段的记录,文档中有很多记录。我希望记录的数量以今天,周,月和年为基准。 为此,我正在这样做,

db.filterrecords.aggregate([{
            "$match" : {
                "_id" : {
                    "$in" : [
                        ObjectId("5b8e877a47d6f21b1065f54a"),
                        ObjectId("5b8e877a47d6f21b1065f54b"),
                        ObjectId("5b8e877a47d6f21b1065f54c"),
                        ObjectId("5b8e877a47d6f21b1065f54d"),
                        ObjectId("5b8e877a47d6f21b1065f54e"),
                        ObjectId("5b8e877a47d6f21b1065f54f"),
                        ObjectId("5b8e877a47d6f21b1065f550"),
                        ObjectId("5b8e877a47d6f21b1065f551"),
                        ObjectId("5b8e877a47d6f21b1065f552"),
                        ObjectId("5b8e877a47d6f21b1065f553"),
                        ObjectId("5b8e877a47d6f21b1065f554"),
                        ObjectId("5b8e877a47d6f21b1065f555"),
                        ObjectId("5b8e877a47d6f21b1065f556"),
                        ObjectId("5b8e877a47d6f21b1065f557"),
                        ObjectId("5b8e877a47d6f21b1065f558"),
                        ObjectId("5b8e877a47d6f21b1065f559")                        
                    ]
                }
            }
        }
    ]).pretty()

它为我提供了匹配查询的总记录,但是我可以在哪里放置类似的代码

{
                $addFields: {
                    TodaysFormSubmissions: {
                        $filter: {
                            input: "$filterrecords",
                            as: "filterrecords_field",
                            cond: {
                                $gte: ["$$filterrecords_field.SubmittedOn", today]
                            }
                        }
                    },
                    WeeksFormSubmissions: {
                        $filter: {
                            input: "$filterrecords",
                            as: "filterrecords_field",
                            cond: {
                                $gte: ["$$filterrecords_field.SubmittedOn", week]
                            }
                        }
                    },
                    MonthFormSubmissions: {
                        $filter: {
                            input: "$filterrecords",
                            as: "filterrecords_field",
                            cond: {
                                $gte: ["$$filterrecords_field.SubmittedOn", month]
                            }
                        }
                    },
                    YearFormSubmissions: {
                        $filter: {
                            input: "$filterrecords",
                            as: "filterrecords_field",
                            cond: {
                                $gte: ["$$filterrecords_field.SubmittedOn", year]
                            }
                        }
                    }
                }
            }, {
                $addFields: {
                    TodaysFormSubmissions: {
                        $size: "$TodaysFormSubmissions"
                    },
                    WeeksFormSubmissions: {
                        $size: "$WeeksFormSubmissions"
                    },
                    MonthFormSubmissions: {
                        $size: "$MonthFormSubmissions"
                    },
                    YearFormSubmissions: {
                        $size: "$YearFormSubmissions"
                    },
                    TotalFormSubmissions: {
                        $size: "$FormSubmissions"
                    }
                }
            }

week="2018-08-28T13:30:00.000Z",today="2018-09-04T13:30:00.000Z",month="2018-08-04T13:30:00.000Z",year="2017-09-04T13:30:00.000Z"上方

我不知道每周,今天等获取记录的确切条件。可能是我在这里错了。所以请指导我在这里获取记录总数的正确输出。

0 个答案:

没有答案