Mongodb 通过查找到另一个集合来聚合总和
我有两个模式,一个是Mongodb 通过查找到另一个集合来聚合总和,mongodb,mongoose,mongodb-query,aggregation-framework,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我有两个模式,一个是User,另一个是Pet。Pet模式在User.aggregate管道中包含我想要$sum的信息 Mongo版本是3.4.1 用户模式: pets: [{type: Schema.Types.ObjectId, ref: 'Pets'}] owner: {type: Schema.Types.ObjectId, ref: 'User'}, petLost: { lost : {type: Boolean, defa
User
,另一个是Pet
。Pet
模式在User.aggregate
管道中包含我想要$sum
的信息
Mongo版本是3.4.1
用户模式:
pets: [{type: Schema.Types.ObjectId, ref: 'Pets'}]
owner: {type: Schema.Types.ObjectId, ref: 'User'},
petLost: {
lost : {type: Boolean, default: false},
lostDate : {type: String},
selectedComRange: {type: Number, default: 5},
circumstances : {type: String},
extraInfoLost : {type: String},
rewardCheck : {type: Boolean, default: false},
reward : {type: String},
addressLost : {type: String},
}
User.aggregate([
{
$group: {
_id : null,
'users_count' : {
'$sum': {
'$cond': [{'$eq': ['$role', 'user']}, 1, 0]
}
},
'volunteer_count': {
'$sum': {
'$cond': [{'$eq': ['$isVolunteer', true]}, 1, 0]
}
},
'pet_count' : {
'$sum': {
$size: '$pets'
}
},
'lost_pet' : {
'$sum': {
**// how can i call another collection and $sum some data?**
}
}
}
},
{
'$project': {
'_id' : 0, 'role': '$_id',
'statistics': {
'users' : '$users_count',
'volunteers': '$volunteer_count',
'pets' : '$pet_count'
}
}
}
]).exec((err, result) => {
if (err) {
console.log(err);
}
res.status(200).json(result);
});
Pets模式:
pets: [{type: Schema.Types.ObjectId, ref: 'Pets'}]
owner: {type: Schema.Types.ObjectId, ref: 'User'},
petLost: {
lost : {type: Boolean, default: false},
lostDate : {type: String},
selectedComRange: {type: Number, default: 5},
circumstances : {type: String},
extraInfoLost : {type: String},
rewardCheck : {type: Boolean, default: false},
reward : {type: String},
addressLost : {type: String},
}
User.aggregate([
{
$group: {
_id : null,
'users_count' : {
'$sum': {
'$cond': [{'$eq': ['$role', 'user']}, 1, 0]
}
},
'volunteer_count': {
'$sum': {
'$cond': [{'$eq': ['$isVolunteer', true]}, 1, 0]
}
},
'pet_count' : {
'$sum': {
$size: '$pets'
}
},
'lost_pet' : {
'$sum': {
**// how can i call another collection and $sum some data?**
}
}
}
},
{
'$project': {
'_id' : 0, 'role': '$_id',
'statistics': {
'users' : '$users_count',
'volunteers': '$volunteer_count',
'pets' : '$pet_count'
}
}
}
]).exec((err, result) => {
if (err) {
console.log(err);
}
res.status(200).json(result);
});
以下是查找:
{'$unwind': '$pets'},
{
'$lookup': {
'from' : 'pets',
'localField' : '_id',
'foreignField': '_id',
'as' : 'lostPets'
}
},
{'$unwind': {path: '$lostPets', preserveNullAndEmptyArrays:true}}
以下是我试图达到的条件:
'lostPet_count': {
'$sum': {
'$cond': [{'$eq': ['$lostPets.lost', true]}, 1, 0]
}
}
聚合管道:
pets: [{type: Schema.Types.ObjectId, ref: 'Pets'}]
owner: {type: Schema.Types.ObjectId, ref: 'User'},
petLost: {
lost : {type: Boolean, default: false},
lostDate : {type: String},
selectedComRange: {type: Number, default: 5},
circumstances : {type: String},
extraInfoLost : {type: String},
rewardCheck : {type: Boolean, default: false},
reward : {type: String},
addressLost : {type: String},
}
User.aggregate([
{
$group: {
_id : null,
'users_count' : {
'$sum': {
'$cond': [{'$eq': ['$role', 'user']}, 1, 0]
}
},
'volunteer_count': {
'$sum': {
'$cond': [{'$eq': ['$isVolunteer', true]}, 1, 0]
}
},
'pet_count' : {
'$sum': {
$size: '$pets'
}
},
'lost_pet' : {
'$sum': {
**// how can i call another collection and $sum some data?**
}
}
}
},
{
'$project': {
'_id' : 0, 'role': '$_id',
'statistics': {
'users' : '$users_count',
'volunteers': '$volunteer_count',
'pets' : '$pet_count'
}
}
}
]).exec((err, result) => {
if (err) {
console.log(err);
}
res.status(200).json(result);
});
如何将信息从pet架构注入到
'lost_pet'
属性中?以下管道应返回所需的结果:
Pets.aggregate([
{
'$lookup': {
'from': 'users',
'localField': 'owner',
'foreignField': '_id',
'as': 'users'
}
},
{ '$unwind': { 'path': '$users', 'preserveNullAndEmptyArrays': true } },
{
'$group': {
'_id': '$users._id',
'users_count': {
'$sum': {
'$cond': [{'$eq': ['$users.role', 'user']}, 1, 0]
}
},
'volunteer_count' : {
'$sum': {
'$cond': ['$users.isVolunteer', 1, 0]
}
},
'pet_count': { '$sum': 1 },
'lost_pets': {
'$sum': {
'$cond': ['$petLost.lost', 1, 0]
}
}
}
},
{
'$project': {
'_id': 0, 'role': '$_id',
'statistics': {
'users': '$users_count',
'volunteers': '$volunteer_count',
'pets': '$pet_count',
'lostpets': '$lost_pets'
}
}
}
]).exec((err, result) => {
if (err) {
console.log(err);
}
res.status(200).json(result);
});
或者从
用户
模型聚合为
User.aggregate([
{ '$unwind': '$pets' },
{
'$lookup': {
'from': 'pets',
'localField': 'pets',
'foreignField': '_id',
'as': 'pets'
}
},
{ '$unwind': { 'path': '$pets', 'preserveNullAndEmptyArrays': true } }
{
'$group': {
'_id': '$_id',
'role': { '$first': '$role' },
'isVolunteer': { '$first': '$isVolunteer' },
'pet_count': { '$sum': 1 },
'lost_pets': {
'$sum': {
'$cond': ['$pets.petLost.lost', 1, 0]
}
}
}
},
{
'$group': {
'_id': null,
'users_count': {
'$sum': {
'$cond': [{'$eq': ['$role', 'user']}, 1, 0]
}
},
'volunteer_count' : {
'$sum': {
'$cond': ['$isVolunteer', 1, 0]
}
},
'pet_count': { '$sum': '$pet_count' },
'lost_pets': { '$sum': '$lost_pets' }
}
},
{
'$project': {
'_id': 0, 'role': '$_id',
'statistics': {
'users': '$users_count',
'volunteers': '$volunteer_count',
'pets': '$pet_count',
'lostpets': '$lost_pets'
}
}
}
]).exec((err, result) => {
if (err) {
console.log(err);
}
res.status(200).json(result);
});
看看@felix,这就是我现在正在做的,如果我成功地做到了,我会发回结果当我尝试用“{'$unwind':'$pets'}”解开数组时,我得到了这个错误:
MongoError:$size的参数必须是一个数组,但类型是:objectId
——这是模式类型:pets:[{type:Schema.Types.ObjectId,ref:'Pets'}],
用户和lostpets是0。我有一个角色为user
的用户,并且一个LostPet设置为true。下面是响应:[{“role”:null,statistics:{“users”:0,“志愿者”:1,“Pets”:1,“lostpets”:0}]
edit:用户似乎可以工作,lostpets仍然保持在0。您可以尝试从Pets
模型中进行聚合,如上所述。