How to write a mongo jpa query to search by inner document id?

Viewed 40

I am new to mongo DB with JPA. I have a document object like below:

{
'_id': ObjectId("A"),
customer:[
    0: {
        id: 21,
        order:
            {
              id:23, 
              orderName:'orderName1'
            }
       
    },
    1: {
        id: 22,
        order:
            {
               id:12,
               orderName:'Electronics'
            }
    },
]
}

Here I want to get all the orders with orderName starting with "Ele"(example). I've tried:

  1. @Query(value="{'order.orderName':{'$regex':'?0','$options':'i'}}")
  2. @Query("{'order':{'orderName':{'$regex':'?0','$options':'i'}}}")

None of these has worked for me.

Please suggest how to do this.

1 Answers

Demo - https://mongoplayground.net/p/k8Au7Bnmkvg

$regex

db.collection.find({
  "customer.order.orderName": {
    "$regex": "Ele.+",
    "$options": "i"
  }
})

you're trying to query order.orderName but as per your schema it's inside the customer object so use customer.order.orderName

Related