用Mongoose计算Mongodb中的多个集合的大小

我需要计算两个集合(设备,房间)内的文档数量。 我将Devices Schema和Rooms Schema中的细节保存为单独的集合。 如何查询这两个集合并返回文档数量?

你可以尝试使用count()

 var devicesCountQuery = DevicesModel.count(); var roomsCountQuery = RoomsModel.count(); 

用mongo你必须做两个单一的查询。

你可以使用Promise.all() (Mongoose支持promises)在一次调用中包装它:

 Promise.all([ DevicesModel.count().exec(), RoomsModel.count().exec() ]).then(function(counts) { console.log('Devices count %d', counts[0]); console.log('Rooms count %d', counts[1]); });