Find subarryas in arrays data

{
  "botID": 2003633449,
  "userid": [
    {
      "_id": {
        "$oid": "6234a83f9938ad0f78e5e24d"
      },
      "tgid": 923886862,
      "exam_year": 2023,
      "stream": "maths",
      "phone_no": null,
      "datetime": 1647618111906,
      "pendingpdf": [
        {
          "_id": {
            "$oid": "6234a85d9938ad0f78e5e253"
          },
          "requestid": "923886862007001",
          "filename": "One",
          "fileid": "BQACAgUAAxkBAAJFm2I0qFjHzh9CYWzN2zGcQkIMZpO1AAK4BQACI4mBVP87Va014mKqIwQ",
          "bid": 7,
          "botssendpdf_messageid": 17822,
          "buttonpath": "Pdf Location = nine",
          "submitdatetime": 1647618141031
        },
        {
          "_id": {
            "$oid": "6234a8d7354e484568a437cb"
          },
          "requestid": "923886862009001",
          "filename": "Jukks",
          "fileid": "BQACAgUAAxkBAAJFq2I0qMnLU5WIL_79DYl253RlrcdiAAJCBgACnQSYVZqPhGMUj0WNIwQ",
          "bid": 9,
          "botssendpdf_messageid": 17838,
          "buttonpath": "Pdf Location = nine > rum",
          "submitdatetime": 1647618263459
        },
        {
          "_id": {
            "$oid": "6234aa01c9f2fa1114c29c7c"
          },
          "requestid": "9238868620010001",
          "filename": "Buv",
          "fileid": "BQACAgUAAxkBAAJFvGI0qfY5i0n4AAFhlxn8YS3CSHHXpgACQgYAAp0EmFWaj4RjFI9FjSME",
          "bid": 10,
          "botssendpdf_messageid": 17855,
          "buttonpath": "Pdf Location = nine > rum > eeee",
          "submitdatetime": 1647618561129
        },
        {
          "_id": {
            "$oid": "6234c418c5c6693ab84f1d42"
          },
          "requestid": "923886862009001",
          "filename": "www",
          "fileid": "BQACAgUAAxkBAAJGU2I0xBJ4kIxDD_wXKP9aOayAcqPfAAJEBAACbJCAVOPMLu8qKcd8IwQ",
          "bid": 9,
          "botssendpdf_messageid": 18006,
          "buttonpath": "Pdf Location = nine > rum",
          "submitdatetime": 1647625240367
        },
        {
          "_id": {
            "$oid": "6234c465c5c6693ab84f1d6d"
          },
          "requestid": "9238868620010001",
          "filename": "ddd",
          "fileid": "BQACAgUAAxkBAAJGeGI0xGBoB_3pyUlsozMoll38KL3LAALDBgACQLKJVZid9i3_J4ovIwQ",
          "bid": 10,
          "botssendpdf_messageid": 18043,
          "buttonpath": "Pdf Location = nine > rum > eeee",
          "submitdatetime": 1647625317087
        }
      ],
      "approvedpdf": []
    }
  ]
  }
}

The above dict is my mongodb document .

I need to get data using below condition .

"botId" = 2003633449 and
"tgid" = 923886862 and
"bid" = 7

Any mongodb query for it?

I need mongoshell command for it. Any method to do it?

I tried $elemnetmatch , $and operators using do this. But results is null. Anyone can give example to above 3 conditions using get data from above my mongodb document .

1 Like

Hi,

You can do it like this:

db.collection.find({
  "botID": 2003633449,
  "userid.tgid": 923886862,
  "userid.pendingpdf.bid": 7
})

Working example

1 Like

"userid.pendingpdf.bid": 7 condition not working

So you don’t want to return whole document, but also to filter an array?

i need to return below output

[ { "_id": ObjectId("5a934e000102030405000000"), "botID": 2.0036349e+07, "userid": [ { "_id": ObjectId("6234a83f9938ad0f78e5e24d"), "approvedpdf": [], "datetime": 1.647618111906e+12, "exam_year": 20230, "pendingpdf": [ { "_id": ObjectId("6234a85d9938ad0f78e5e253"), "bid": 7, "botssendpdf_messageid": 17822, "buttonpath": "Pdf Location = nine", "fileid": "BQACAgUAAxkBAAJFm2I0qFjHzh9CYWzN2zGcQkIMZpO1AAK4BQACI4mBVP87Va014mKqIwQ", "filename": "One", "requestid": "923886862007001", "submitdatetime": 1.647618141031e+12 } ], "phone_no": null, "stream": "marrs", "tgid": 92362 } ] } ]

i need to return below output

You can do it with Aggregation Framework:

  • $match - To match all documents by botID field
  • $set with $filter - To filter userid array by tgid items property
  • $set with map and $filter - To filter nested pendingpdf array by bid items property
db.collection.aggregate([
  {
    "$match": {
      "botID": 2003633449
    }
  },
  {
    "$set": {
      "userid": {
        "$filter": {
          "input": "$userid",
          "cond": {
            "$eq": [
              "$$this.tgid",
              923886862
            ]
          }
        }
      }
    }
  },
  {
    "$set": {
      "userid": {
        "$map": {
          "input": "$userid",
          "in": {
            "_id": "$$this._id",
            "tgid": "$$this.tgid",
            "exam_year": "$$this.exam_year",
            "stream": "$$this.stream",
            "phone_no": "$$this.phone_no",
            "datetime": "$$this.datetime",
            "approvedpdf": "$$this.approvedpdf",
            "pendingpdf": {
              "$filter": {
                "input": "$$this.pendingpdf",
                "as": "pdf",
                "cond": {
                  "$eq": [
                    "$$pdf.bid",
                    7
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
])

Working example

1 Like

Thank You Very much

1 Like

I need to delete nested array object using below condition .

"botId" = 2003633449 and
"tgid" = 923886862 and
"requestid": "923886862007001"

I need Delete Below Object
{ "_id": { "$oid": "6234a85d9938ad0f78e5e253" }, "requestid": "923886862007001", "filename": "One", "fileid": "BQACAgUAAxkBAAJFm2I0qFjHzh9CYWzN2zGcQkIMZpO1AAK4BQACI4mBVP87Va014mKqIwQ", "bid": 7, "botssendpdf_messageid": 17822, "buttonpath": "Pdf Location = nine", "submitdatetime": 1647618141031 }

Any mongodb query for it?

can use findoneandupdate method?

@NeNaD can You Again Help me :cry:

Hi,

You can do it like this:

db.collection.update({
  "botID": 2003633449
},
[
  {
    "$set": {
      "userid": {
        "$map": {
          "input": "$userid",
          "as": "user",
          "in": {
            "$cond": {
              "if": {
                "$eq": [
                  923886862,
                  "$$user.tgid"
                ]
              },
              "then": {
                "_id": "$$user._id",
                "tgid": "$$user.tgid",
                "exam_year": "$$user.exam_year",
                "stream": "$$user.stream",
                "phone_no": "$$user.phone_no",
                "datetime": "$$user.datetime",
                "approvedpdf": "$$user.approvedpdf",
                "pendingpdf": {
                  "$filter": {
                    "input": "$$user.pendingpdf",
                    "as": "pdf",
                    "cond": {
                      "$ne": [
                        "$$pdf.requestid",
                        "923886862007001"
                      ]
                    }
                  }
                }
              },
              "else": "$user"
            }
          }
        }
      }
    }
  }
])

Working example

Kindly Check This

Hi,

There was a typo in previous version. Instead of $user it should be $$user.

db.collection.update({
  "botID": 2003633449
},
[
  {
    "$set": {
      "userid": {
        "$map": {
          "input": "$userid",
          "as": "user",
          "in": {
            "$cond": {
              "if": {
                "$eq": [
                  923886862,
                  "$$user.tgid"
                ]
              },
              "then": {
                "_id": "$$user._id",
                "tgid": "$$user.tgid",
                "exam_year": "$$user.exam_year",
                "stream": "$$user.stream",
                "phone_no": "$$user.phone_no",
                "datetime": "$$user.datetime",
                "approvedpdf": "$$user.approvedpdf",
                "pendingpdf": {
                  "$filter": {
                    "input": "$$user.pendingpdf",
                    "as": "pdf",
                    "cond": {
                      "$ne": [
                        "$$pdf.requestid",
                        "923886862007001"
                      ]
                    }
                  }
                }
              },
              "else": "$$user"
            }
          }
        }
      }
    }
  }
])

Working example

1 Like

Thank You very much

can You Tell Me How To Learn This Queries? Any resource link to like this advance queries

Hi,

I think you can check some official MongoDB courses.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.