我有两个系列: 客户和设施。 facility.clientId = clients._id 我想加入对象,但找不到任何好的代码示例。 我看过查找但无法让它工作 我想要一个设施计数与facility.clientId = clients._id 所以输出如果:
[
{
"_id": "1",
"firstname": "Blue",
"address1": "Address 1",
"address2": "Address 2",
"city": "Dublin",
"count": "2",
"__v": 0
},
{
"_id": "2",
"firstname": "Red",
"address1": "Address 1",
"address2": "Address 2",
"city": "Dublin",
"count": "1",
"__v": 0
},
{
"_id": "3",
"firstname": "Green",
"address1": "Address 1",
"address2": "Address 2",
"city": "Dublin",
"count": "0",
"__v": 0
}
]
其中count是facility.clientId = clients._id
的编号var clientsModel = require('../models/clientsModel.js');
var facilityModel = require('../models/facilityModel.js');
list: function (req, res) {
clientsModel.find({}, null, {sort: '_id'}, function (err, clientss) {
Object.keys(clientss).forEach(function(key) {
facilityModel.count({clientId: clientss[key]._id}, function(err, c) {
console.log('Count is ' + c + " key:" + key);
clientss[key].count = c;
});
});
return res.json(clientss);
});
},
我已尝试过所有功能,但无法使其正常工作,请将任何代码示例指向正确的方向,这样做会很好。
我知道有几个查询和聚合查询的例子,但我只是不让它工作。