Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

query nested array of object mongoose

I would like to find name with alexa in nested object Playground: https://mongoplayground.net/p/rqYQtf0liaX

[
  {
    item: "journal",
    instock: [
      {
        warehouse: "A",
        qty: 5,
        items: null
      },
      {
        warehouse: "C",
        qty: 15,
        items: [
          {
            name: "alexa",
            age: 26
          },
          {
            name: "Shawn",
            age: 26
          }
        ]
      }
    ]
  }
]

What i have tried so far and this returns no document found

db.collection.find({
  "instock.items": {
    $elemMatch: {
      name: "Alexa"
    }
  }
})
like image 732
Indraraj26 Avatar asked Sep 08 '26 20:09

Indraraj26


1 Answers

Mongo is case sensitive.

play

db.collection.find({
  "instock.items": {
    $elemMatch: {
      name: "alexa"
    }
  }
})

If you want to have case-insensitive, use regex or $text. With $regex, you can use $elemMatch.

db.collection.find({
  "instock.items": {
    $elemMatch: {
      name: {
        $regex: "alexa",
        $options: "i"
      }
    }
  }
})
like image 95
Gibbs Avatar answered Sep 10 '26 11:09

Gibbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!