skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. You can try something like this:

    db.collection.aggregate([
      { $match: { "receiverNumbers.resultSend.send": true } },
      { $project: { length: { $size: "$receiverNumbers" } } },
    ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search