0

I need help getting counts from the array spaces. I need to get the count of the available spaces. I tried multiple queries and also tried to push the counts to the callback object. Like this

props.map( p => {
            var count = 0;
            for(var i = 0; i < p.spaces.length; ++i){
                if(p.spaces[i]['available'] == true)
                    count++;
            }
            p.push(""); //I dont know how to add the counts if this is a valid option
            console.log(count);
        });

    {
    "_id" : ObjectId("593a01b4b9ab7204b0336e60"),
    "name" : "Test1",
    "serie" : "MC-016-04",
    "created_at" : ISODate("2017-06-09T02:02:28.759Z"),
    "spaces" : [ 
        {
            "available" : false,
            "size" : "12",
            "dimensions" : "200",
            "local" : "Test 1",
            "_id" : ObjectId("594434b4aec46311043db2eb")
        }, 
        {
            "available" : true,
            "size" : "36",
            "dimensions" : "1200",
            "name" : "Test 7",
            "_id" : ObjectId("594434f6760a76110efc216e")
        }, 
        {
            "available" : true,
            "size" : "36",
            "dimensions" : "1200",
            "name" : "Test 2",
            "_id" : ObjectId("5944360ff249971142c5dd0f")
        }
     ]
   }

The results should be something like this:

{
        "_id" : ObjectId("593a01b4b9ab7204b0336e60"),
        "name" : "Test1",
        "serie" : "MC-016-04",
        "created_at" : ISODate("2017-06-09T02:02:28.759Z"),
        "spaces" : [ 
            {
                "available" : false,
                "size" : "12",
                "dimensions" : "200",
                "local" : "Test 1",
                "_id" : ObjectId("594434b4aec46311043db2eb")
            }, 
            {
                "available" : true,
                "size" : "36",
                "dimensions" : "1200",
                "name" : "Test 7",
                "_id" : ObjectId("594434f6760a76110efc216e")
            }, 
            {
                "available" : true,
                "size" : "36",
                "dimensions" : "1200",
                "name" : "Test 2",
                "_id" : ObjectId("5944360ff249971142c5dd0f")
            }
         ],
        "availableSpaces" : 2
}
  • What are you asking? How to get the count from each document? Or how to "update" the count into each document? – Neil Lunn Jun 21 '17 at 04:32
  • Related: [MongoDB: count the number of items in an array](https://stackoverflow.com/a/22152786/2313887) And: [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/q/3985214/2313887). When read these basically answer the question. See also [`$size`](https://docs.mongodb.com/manual/reference/operator/aggregation/size/) and [`$filter`](https://docs.mongodb.com/manual/reference/operator/aggregation/filter/) in the core documentation. – Neil Lunn Jun 21 '17 at 04:56

1 Answers1

1

To get data with availableSpaces you can use $size and $filter in aggregate query

db.collectionName.aggregate([
  {"$match": {}}, // match condition here
  {"$project": {
    "name" : 1,
    "serie" : 1,
    "created_at" : 1,
    "spaces": 1,
    "availableSpaces": {
      $size: {
        "$filter": {
          input: "$spaces",
          as: 'space',
          cond: '$$space.available' // for Boolean if not Boolean then you should use like: cond: {$eq: ['$$space.available', 'true']}  
        }
      }
    }
  }}
])
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
  • Actually `cond: '$$space.available'` will suffice since it's already a boolean. – Neil Lunn Jun 21 '17 at 04:39
  • Umm. "Suffice" means *"that's all you need to do"*, so I'm actually advising on how to "shorten" the statement without needing the `$eq` evaluation. Guessing you did not understand that. – Neil Lunn Jun 21 '17 at 04:48
  • Thank you so much. This is exactly what i needed. I'm sorry if my post didn't make sense but i was running on 2 hours of sleep. You nailed it. It now makes sense that you are getting the size of the array after applying the filter. Thank you for your time and patience. I just started working with mongodb. – Jorge Mendoza Jun 21 '17 at 13:01