6

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"
    }
  }
})

1 Answer 1

11

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"
      }
    }
  }
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.