2017-02-10 14 views
1

我有兩個模式,一個是User另一個是PetPet架構在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}, 
     } 

這是查找:

{'$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] 
      } 
     } 

總管道:

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); 
     }); 

我如何注入到信息財產'lost_pet'從寵物模式?

+0

看看[$查找(https://開頭的文檔.mongodb.com/manual/reference/operator/aggregation/lookup /) – felix

+0

@felix這就是我現在正在做的事情,如果我設法做到這一點,我會回髮結果 –

+0

當我嘗試展開陣列y'with'{'$ unwind':'$ pets'},'我得到這個錯誤:'MongoError:$ size的參數必須是一個數組,但是類型爲:objectId ' --- 這是schema類型:'pets:[{type:Schema.Types.ObjectId,ref:'Pets'}],' –

回答

1

下面的管道應返回所需的結果:

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模型作爲聚合

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); 
}); 
+0

用戶和lostpets是0.我有用戶角色爲'user',並且一個丟失的寵物設置爲true。這裏的響應:'[ { 「角色」:空, 「統計」:{ 「用戶」:0, 「志願者」:1, 「寵物」:1, 「lostpets」:0 } } ]' 編輯:用戶似乎工作,lostpets仍然停留在0 –

+0

您可以嘗試從上面的'寵物'模型聚合。 – chridam

相關問題