我如何使用嵌套外部文档MONGODB聚合框架 - Mongoose和NodeJS

时间:2014-04-01 18:17:09

标签: node.js mongodb mongoose aggregation-framework

我有这些Mongoose计划:

//用户架构

exports.User = new Schema({
        name: {
                 type: String,
                 required: true
        },
        home: [{
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Post'
              }]
});

//帖子架构

exports.Post = new Schema({
    likes: [{
        type: Schema.Types.ObjectId,
        ref: 'User'
  }],
    author: {
        id: {
            type: Schema.Types.ObjectId,
            ref: 'User',
            required: true
        },
        name: {
            type: String,
            required: true
        },
        shortId: String, // this is User id 
    },
    created: {
        type: Date,
        default: Date.now,
        required: true
    }
});

// DATABASE

中的数据

//用户

{"name" : "Mio Nome",
    "home" : [
        ObjectId("533af14b994c3647c5c97338")
    ]}

//发布

 {  "author" : {
        "id" : ObjectId("533af14b994c3647c5c97338"),
        "name" : "AutoreDelPost"        
    },  
         "likes" : [
              ObjectId("533af14b994c3647c5c97337"),
                  ObjectId("533af14b994c3647c5c97339"),
                  ObjectId("533af14b994c3647c5c97340")
         ]
     }

我想从用户处获取home字段中的帖子,并计算有多少like有一个用户

使用此代码,我可以显示家庭版populate中的所有帖子,但我无法计算喜欢。

req.db.User.find({
            _id: req.user._id   //req.user is my test user
        }, {
            home: 1
        })
            .limit(200)
            .populate('home')
            .exec(function (err) {
                if (!err) {
                    return res.json(200)
                }
                return res.json(500, err)
            });

//输出

[
    {
        "_id": "533af0ae994c3647c5c97337",
        "name" : "Mio Nome"
        "home": [
            {
                "_id": "533b004e6bcb9105d535597e",
                "author": {
                    "id": "533af14b994c3647c5c97338",
                    "name": "AutoreDelPost"
                },
                "likes": []  // i can't see like and i can't count they
      }        
]

我尝试使用aggregate来计算等等,但我无法看到帖子已经填充,但是他们的_id

req.db.User.aggregate({
        $match: {
            _id: req.user._id
        }
    }, {
        $project: {
            home: 1
        }
    }, {
        $unwind: "$home"
    }).exec(function (err, home) {
        if (!err) {

            return res.json(200, home)
        }
        return res.json(500, err)
    });

//输出

[
    {
        "_id": "533af0ae994c3647c5c97337",
        "home": "533b004e6bcb9105d535597e"
    },
    {
        "_id": "533af0ae994c3647c5c97337",
        "home": "533b004e6bcb9105d5355980"
    },
    {
        "_id": "533af0ae994c3647c5c97337",
        "home": "533b004f6bcb9105d5355982"
    },
    {
        "_id": "533af0ae994c3647c5c97337",
        "home": "533b004f6bcb9105d5355984"
    },
    {
        "_id": "533af0ae994c3647c5c97337",
        "home": "533b00506bcb9105d5355986"
    }
]

QUESTION:我想从用户那里获取主页中的帖子并计算用户拥有的数量

1 个答案:

答案 0 :(得分:2)

也许你可以存储更多非规范化的数据并添加一个计数器字段,该字段在每个新的“like”上递增。见http://cookbook.mongodb.org/patterns/votes/。类似的东西:

update = {'$push': {'voters': user_id}, '$inc': {vote_count: 1}}