mongodb获取过滤计数

mongodb获取过滤计数,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我没有关于如何创建mongodb查询的扩展知识,但我想问我如何才能获得类似以下内容的查询集合: { Total: 1000, Filtered: 459, DocumentArray: [] } db.collection.find(); db.collection.find().count(); db.colection.count(); 当然,在一个查询中可以这样做,所以我不需要这样做: { Total: 1000, Filtered: 459,

我没有关于如何创建mongodb查询的扩展知识,但我想问我如何才能获得类似以下内容的查询集合:

{
    Total: 1000,
    Filtered: 459,
    DocumentArray: []
}
db.collection.find();
db.collection.find().count();
db.colection.count(); 
当然,在一个查询中可以这样做,所以我不需要这样做:

{
    Total: 1000,
    Filtered: 459,
    DocumentArray: []
}
db.collection.find();
db.collection.find().count();
db.colection.count(); 

你可以按照以下思路做一些事情:

考虑到这样的文件:

{
    Total: 1000,
    Filtered: 459,
    DocumentArray: []
}
db.collection.find();
db.collection.find().count();
db.colection.count(); 
{“\u id”:ObjectId(“531251829DF824BDB53578”),“名称”:“a”,“类型”:“b”}
{“u id”:ObjectId(“531251899DF824BDB53579”),“name”:“a”,“type”:“c”}
{“u id”:ObjectId(“5312518e9df824bdb5357a”),“type”:“c”,“name”:“b”}
以及这样的聚合管道:

{
    Total: 1000,
    Filtered: 459,
    DocumentArray: []
}
db.collection.find();
db.collection.find().count();
db.colection.count(); 
db.collection.aggregate([
{“$group”:{
“_id”:空,
“totalCount”:{“$sum”:1},
“文档”:{“$push”:{
“名称”:“$name”,
“类型”:“$type”
}},
}},
{“$unwind”:“$docs”},
{“$match”:{“docs.name”:“a”},
{“$组”:{
“_id”:空,
“totalCount”:{“$first”:“$totalCount”},
“filteredCount”:{“$sum”:1},
“文档”:{“$push”:“$docs”}
}}
])
但是我不会推荐它。它肯定会在任何“real”收藏上爆炸,因为它超过了标准。我怀疑它会表现得很好。但这就是如何做到的,即使这种效用纯粹是学术性的

如果你需要信息,就做你正在做的事情。这是做这件事的“正确方法”