MongoDB:如何查找和计算具有特定格式的同一查询?

时间:2019-04-03 17:37:17

标签: mongodb count mongodb-query find

使用MongoDB,我想在单个查询中执行查找和计数并打印结果。我以前发现了这个thread,但没有回答我的问题。当我用足够的信息替换时,由于内存限制,查询仍然失败。那就是我的文档的样子:

{ "_id" : ObjectId("5ca47bca0953f323b39019b2"), "Sample" : "test-exome-1_hg38", "Chromosome" : "chr1", "Position" : 69511, "Reference" : "A", "Mutation" : "G", "ReadDepth" : 206 }
{ "_id" : ObjectId("5ca47bca0953f323b39019cd"), "Sample" : "test-exome-1_hg38", "Chromosome" : "chr1", "Position" : 942451, "Reference" : "T", "Mutation" : "C", "ReadDepth" : 65 }
{ "_id" : ObjectId("5ca47bca0953f323b39019d5"), "Sample" : "test-exome-1_hg38", "Chromosome" : "chr1", "Position" : 946247, "Reference" : "G", "Mutation" : "A", "ReadDepth" : 114 }
{ "_id" : ObjectId("5ca47bca0953f323b39019d3"), "Sample" : "test-exome-1_hg38", "Chromosome" : "chr1", "Position" : 952421, "Reference" : "A", "Mutation" : "G", "ReadDepth" : 258 }
{ "_id" : ObjectId("5ca47bca0953f323b39019d4"), "Sample" : "test-exome-1_hg38", "Chromosome" : "chr1", "Position" : 953259, "Reference" : "T", "Mutation" : "C", "ReadDepth" : 161 }
{ "_id" : ObjectId("5ca47bca0953f323b39019d8"), "Sample" : "test-exome-1_hg38", "Chromosome" : "chr1", "Position" : 953279, "Reference" : "T", "Mutation" : "C", "ReadDepth" : 155 }
{ "_id" : ObjectId("5ca47bca0953f323b39019db"), "Sample" : "test-exome-1_hg38", "Chromosome" : "chr1", "Position" : 961945, "Reference" : "G", "Mutation" : "C", "ReadDepth" : 205 }

我可以做到:

db.test_mutindiv.find({"Sample": "test-exome-1_hg38", "Chromosome": "chr1", "Position": 69511, "Reference": "A", "Mutation": "G"})
db.test_mutindiv.find({"Sample": "test-exome-1_hg38", "Chromosome": "chr1", "Position": 69511, "Reference": "A", "Mutation": "G"}).count()

我尝试了以下操作:

db.test_mutindiv.aggregate(
    [
        { "$project": { 
            "Sample": "test-exome-1_hg38",
            "Chromosome":"chr1",
            "Position": 17512,
            "Reference": "C",
            "Mutation": "G",
            "count": { "$sum": 1 }
        }},
    ]
)

db.test_mutindiv.aggregate(
    [
        { "$group": {
            "_id": null, 
            "docs": { "$push": "$$ROOT" }, 
            "count": { "$sum": 1 }
        }},
        { "$project": { "_id": 0, "count": 1, "docs": { "$slice": [ "$docs", 5 ] } }}
    ]
)

但是他们都没有工作。最终,我想获得以下格式:

test-exome-1_hg38,chr1,69511,A,G,2

1 个答案:

答案 0 :(得分:2)

在计算/返回匹配的文档之前,您需要$match来应用过滤条件。然后,您可以利用$facet的优势,它允许您在该过滤后的数据集上运行多个聚合:

db.test_mutindiv.aggregate([
    {
        $match: {"Sample": "test-exome-1_hg38", "Chromosome": "chr1", "Position": 69511, "Reference": "A", "Mutation": "G"}
    },
    {
        $facet: {
            count: [ { $count: "total" } ],
            docs: [ { $match: {} } ]
        }
    },
    {
        $unwind: "$count"
    }
])