Home > Software engineering >  how to get the length of object in an array in mongo?
how to get the length of object in an array in mongo?

Time:01-18

my database structure is something like this: model of messages in db each documnet has "receiverNumbers" field with "Object" type:

receiverNumbers: {
  type: [{type: Object}],
  required: true
},

in this object, we have tow properties. "phoneNumber" and "resultSend" witch resultSend is an object that have few properties.

how can I get the length of receiverNumbers of all documents that the "send" property of "resultSend" object is true?

I know that I should use aggregate but I'm confused how.

CodePudding user response:

In MongoDB, you can use the $match stage in the aggregate pipeline to filter the documents that you want to count, and then use the $group stage to group the documents by a specific field and use the $sum operator to calculate the count.

Here's an example of how you can get the length of the receiverNumbers field for all documents where the send property of the resultSend object is true:

db.collection.aggregate([{
$match: {
  "receiverNumbers.resultSend.send": true
}},{
$group: {
  _id: null,
  count: { $sum: { $size: "$receiverNumbers" } }
}}])

It is also possible to add a filter to the match stage to get only the documents where the send property of the resultSend object is true and then use the $size operator to calculate the length of the array.

db.collection.aggregate([{
$match: {
  "receiverNumbers.resultSend.send": true
}},{
$project: {
    count: { $size: "$receiverNumbers" }
}}])

CodePudding user response:

Considering that your input documents look like this :

{
  "_id": "id_1",
  "User": "user_1",
  "TotalNumber": 50,
  "SuccessNumber": 50,
  "__v": 0,
  "receiverNumbers": [
    {
      "phoneNumber": "01 23 45 67 89",
      "resultSend": {
        "senderNumber": "00 00 00 00 00",
        "messageText": "Hello World sent 1",
        "dateTime": "2023-01-18",
        "send": true
      }
    },
    {
      "phoneNumber": "01 23 45 67 89",
      "resultSend": {
        "senderNumber": "00 00 00 00 00",
        "messageText": "Hello World sent 2",
        "dateTime": "2023-01-18",
        "send": true
      }
    },
    {
      "phoneNumber": "01 23 45 67 89",
      "resultSend": {
        "senderNumber": "00 00 00 00 00",
        "messageText": "Hello World not sent 1",
        "dateTime": "2023-01-18",
        "send": false
      }
    },
    {
      "phoneNumber": "01 23 45 67 89",
      "resultSend": {
        "senderNumber": "00 00 00 00 00",
        "messageText": "Hello World sent 3",
        "dateTime": "2023-01-18",
        "send": true
      }
    },
    {
      "phoneNumber": "01 23 45 67 89",
      "resultSend": {
        "senderNumber": "00 00 00 00 00",
        "messageText": "Hello World not sent 2",
        "dateTime": "2023-01-18",
        "send": false
      }
    },
    
  ]
}

And that you want for each document the number of element in receiverNumbers where element.resultSend.send is true.

You could do something like this :

db.collection.aggregate([
  {
    "$addFields": {
      "totalSend": {
        $sum: {
          "$map": {
            "input": "$receiverNumbers",
            "as": "val",
            "in": {
              $cond: [
                "$$val.resultSend.send",
                1,
                0,
                
              ]
            }
          }
        }
      }
    }
  }
])

You can check https://mongoplayground.net/p/crlKjIcmcWR for test.

Let me know if that answers your issue.

CodePudding user response:

You can try something like this:

db.collection.aggregate([
  { $match: { "receiverNumbers.resultSend.send": true } },
  { $project: { length: { $size: "$receiverNumbers" } } },
]);
  • Related