Mongodb - Add documents that have a certain value within an array

Asked

Viewed 278 times

0

Hello, thank you for your attention. If I have left any questions, please let me know.

I found a problem that is driving me crazy. I have the following collection:

{
    "_id": "5d752c544f4f1c0f1c93eb23",
    "address": {
        "coordinates": { "latitude": -21, "longitude": 21 },
        "zip_code": "15775000",
        "state": "são paulo",
        "city": "são paulo",
        "neighborhood": "vila serraria",
        "street": "rua mendez cunha",
        "number_house": "550",
        "createdAt": "2019-09-08T16:29:08.809Z"
    },
    "excluded": false,
    "name": "estacionamento um",
    "cnpj": "24939598479",
    "telephone": { "ddd": 22, "number": "992658386" },
    "parkingSpace": [
        {
            "description": {
                "accessibility": true,
                "covered": false,
                "vehicle_type": false,
                "services": false
            },
            "excluded": false,
            "avalible": true,
            "_id": "5d752cf54f4f1c0f1c93eb27",
            "value": 2.2,
            "name": "vg001",
            "createdAt": "2019-09-08T16:31:49.752Z",
            "updatedAt": "2019-09-09T01:18:09.403Z"
        },
        {
            "description": {
                "accessibility": true,
                "covered": false,
                "vehicle_type": false,
                "services": false
            },
            "excluded": false,
            "avalible": true,
            "_id": "5d75339bc411423a9c14ac52",
            "value": 3.5,
            "name": "vg002",
            "createdAt": "2019-09-08T17:00:11.167Z",
            "updatedAt": "2019-09-08T17:00:11.167Z"
        },
        {
            "description": {
                "accessibility": true,
                "covered": false,
                "vehicle_type": false,
                "services": false
            },
            "excluded": false,
            "avalible": true,
            "_id": "5d75aeb8821ce95a74749c95",
            "value": 2.3,
            "name": "vg003",
            "createdAt": "2019-09-09T01:45:28.772Z",
            "updatedAt": "2019-09-09T01:45:28.772Z"
        },
        {
            "description": {
                "accessibility": true,
                "covered": false,
                "vehicle_type": false,
                "services": false
            },
            "excluded": false,
            "avalible": false,
            "_id": "5d75aec7821ce95a74749c96",
            "value": 3,
            "name": "vg004",
            "createdAt": "2019-09-09T01:45:43.089Z",
            "updatedAt": "2019-09-10T01:15:41.075Z"
        }
    ],
    "qualification": [],
    "createdAt": "2019-09-08T16:29:08.813Z",
    "updatedAt": "2019-09-10T01:15:41.075Z",
    "__v": 1
},
{
    "_id": "5d7706b60d354b72388a38f4",
    "address": {
        "coordinates": { "latitude": -21, "longitude": 21 },
        "zip_code": "15775000",
        "state": "são paulo",
        "city": "são paulo",
        "neighborhood": "vila serraria",
        "street": "rua mendez",
        "number_house": "550",
        "createdAt": "2019-09-10T02:13:10.073Z"
    },
    "excluded": false,
    "name": "estacionamento dois",
    "cnpj": "24939598475",
    "telephone": { "ddd": 17, "number": "991848123" },
    "parkingSpace": [
        {
            "description": {
                "accessibility": true,
                "covered": false,
                "vehicle_type": false,
                "services": false
            },
            "excluded": false,
            "avalible": true,
            "_id": "5d77078a5173bb63bc87b7ca",
            "value": 3,
            "name": "vg004",
            "createdAt": "2019-09-10T02:16:42.335Z",
            "updatedAt": "2019-09-10T02:16:42.335Z"
        }
    ],
    "qualification": [],
    "createdAt": "2019-09-10T02:13:10.079Z",
    "updatedAt": "2019-09-10T02:16:42.335Z",
    "__v": 1
}

Note that there are two documents symbolizing a parking lot, where each has an array, called parkingSpaces, to register parking space documents.

The first document has 4 vacancies not excluded, so, with the property excluded: false and the second, has only one vacancy, which is also not excluded.

I need to add up the amount of parking spaces _id: 5d752c544f4f1c0f1c93eb23, that holds the value false on the property excluded

For now, I’ve come this far:

 const registeredParkingSpaces = await Parking.aggregate([
        { $unwind: '$parkingSpace' },
        { $match: { 'parkingSpace.excluded': false } },
        {
            $group: {
                _id: parking_id,
                total: { $sum: 1 }
            }
        }
 ]);

But I end up returning the amount of the two parking lots, regardless of the _id :/

Upshot:

{
  "message": [
    {
      "_id": "5d752c544f4f1c0f1c93eb23",
      "total": 5
    }
  ]
}

I thank you for your attention.

1 answer

0


I hope it helps..

var parking_id = '5d752c544f4f1c0f1c93eb23';
db.parking.aggregate([
  { 
      $match: { 
          '_id': parking_id

      } 

  },
  {
        $project:{

            parkingTotal:{
                $sum:{$map:{
                    input:'$parkingSpace',
                    as: 'p',
                    in:{
                         $add:{ $cond: { if: '$$p.excluded', then: 0, else: 1 } }
                    }
                }
            }}
        }
    }
]) 

Upshot:

{
  "_id" : "5d752c544f4f1c0f1c93eb23",
   "parkingTotal" : 4
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.