Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
are they in two separate collections? if so then you cannot do it in one query, and you cannot do it in one aggregation. –  Asya Kamsky Apr 1 '14 at 20:34
    
why do you need to use the home field at all? Isn't each user/author stored in the Posts? Why not just aggregate posts grouping by user counting likes? –  Asya Kamsky Apr 1 '14 at 20:35
    
cuz i need the home and everything else –  jay Apr 1 '14 at 21:21

1 Answer 1

up vote 2 down vote accepted

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}}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.