Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use MONGODB aggregation framework with nested external document - Mongoose and NodeJS

I have these Mongoose schemes:

// User schema

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

// Post schema

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

// THE DATA IN THE DATABASE

// User

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

// Post

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

And i want to get from users the posts in home field and count how many likehave one user

With this code i can show all posts in home whit populate, but i can't count likes.

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

// output

[
    {
        "_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
      }        
]

I tryed to use aggregate, to count etc but i can't see the posts getting populated but their _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)
    });

// output

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

QUESTION: I want to get from users the posts in home field and count how many like a user has

like image 531
jay Avatar asked May 22 '26 17:05

jay


1 Answers

Perhaps you can store your data more denormalized and add a counter field which is incremented on each new "like". See http://cookbook.mongodb.org/patterns/votes/. Something like:

update = {'$push': {'voters': user_id}, '$inc': {vote_count: 1}}
like image 143
joafeldmann Avatar answered May 24 '26 07:05

joafeldmann