0

我试图最小化应用程序中数据库调用的数量。

是否可以在一次调用中完成这两个查询?

system_0 = System.objects(platform_id=platform_id, type=0).count()
system_1 = System.objects(platform_id=platform_id, type=1).count()
4

1 回答 1

0

我不知道什么是 mongoengine,但我认为您将能够将我的 mongo shell 答案翻译成适合您的内容。是的,您可以通过聚合来实现它。例如:

db.collection.aggregate([{ 
   $match : { platform_id : ... }
}, {
    $group: {
        _id: "$type",
        count: { $sum: 1 }
    }
}]);

如果您的类型多于 0、1,您也可以将它们排除在外$match

于 2014-10-10T23:18:18.883 回答