MongoDB - How to make a nested query on a foreach after another query

I am new on Node.js and what I am trying to do is the next thing:

In my model, I have "paddocks" with this estructure:

{
  _id: ObjectId("53f39dd0ad2f71d36a872cf4"),
  name: "2 animals",
  size: "523",
  latitude: "",
  longitude: "",
  propertyId: "53f39dc9ad2f71d36a872cf3"
}

And I have animals, one paddock can have multiple animals assigned, this way:

{
  _id: "123123",
  propertyId: "53f39dc9ad2f71d36a872cf3",
  paddockId: "53f39dd0ad2f71d36a872cf4",
  breed: "NO",
  groupId: "53f3a28f50364eaa7243c621",
  birthDate: "2014-08-12T03:00:00.000Z",
  gender: "F",
  traceable: "true",
  pureBreed: "true",
  active: "true"
}

What I want to do is, for each paddock, retrieve the quantity of animals. See this line:

paddock.cattlesQuantity = items.length;

The weird thing is that always returns 0 for animals when I have several animals for each paddock.

I did a console.log of the query and used the query on mongo console, it retrieves me the animals, but doing this it doesn't.

router.post('/paddockListById', function(req, res) {
    var db = req.db;
    var propertyId = req.body.propertyId;

    db.collection('paddock').find({"propertyId": propertyId}).toArray(function (err, paddocks) {
        if(paddocks.length > 0){

            var counter = 0;
            var paddockArray = new Array();

            paddocks.forEach(function(paddock){

                db.collection('animal').find({"paddockId": paddock._id}).toArray(function (err, items) {

                    counter ++;     

                    paddock.cattlesQuantity = items.length;

                    paddockArray.push(paddock);

                    if(counter == paddocks.length){
                        res.send(
                            (err === null) ? { status: 'ok', result: paddockArray } : { status: err }
                        );
                    }
                });
            });
        }
        else{
            res.send(
                (err === null) ? { status: 'ok', result: paddocks } : { status: err }
            );
        }
    });
});

Any idea?

Verify that the paddockId in your animals collection is an ObjectId and not a string. If so, either change your second find statement do use:

{"paddockId":paddock._id.toString()}

I've used the toString on ObjectId through mongoose and pretty sure it's just a passthrough to the native driver ObjectId, but you might need to check.

Note that you can also do this computation server side with a simple aggregation pipeline:

> db.animals.insert([
    { "type" : "cow", "says" : "moo", "paddockId" : 1 },
    { "type" : "horse", "says" : "neigh", "paddockId" : 2 },
    { "type" : "sheep", "says" : "baa", "paddockId" : 3 },
    { "type" : "cow", "says" : "moo", "paddockId" : 1 },
    { "type" : "horse", "says" : "neigh", "paddockId" : 3 }
])
> db.animals.aggregate([
    { "$group" : { "_id" : "$paddockId", "count" : { "$sum" : 1 } } }
])
{ "_id" : 3, "count" : 2 }
{ "_id" : 2, "count" : 1 }
{ "_id" : 1, "count" : 2 }