与查找到另一个集合的总和

时间:2017-02-10 11:03:33

标签: mongodb mongoose mongodb-query aggregation-framework

我有两个模式,一个是User,另一个是PetPet架构在$sum管道中包含我想要User.aggregate的信息。

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

如何从Pet模式中将信息注入'lost_pet'属性?

1 个答案:

答案 0 :(得分:1)

以下管道应该返回所需的结果:

SELECT 
    CHANNELS.ChanID 
FROM 
    CHANNELS 
LEFT JOIN 
    SUBSCRIBERS ON  SUBSCRIBERS.ChanID = CHANNELS.ChanID
                AND SUBSCRIBERS.nickname = 'Jonny' 
WHERE
    CHANNELS.type = 'public' 
AND
    SUBSCRIBERS.ChanID IS NULL; -- keep only outer-joined records, i.e. non-matches

或从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
相关问题