0

Input

{
  "names": [
    {
      "id": 1,
      "name": "abc"
    },
    {
      "id": 2,
      "name": "cde"
    }
  ]
}

Here I want to get as below based Id :2 in Java,

{
  "names": [
    {
      "id": 2,
      "name": "cde"
    }
  ]
}

i am using the query like this but its returning all the documents. I'm looking for the document which is id : 2

Query query = new Query();
query.addCriteria(Criteria.where("names").elemMatch(Criteria.where("id").is("2")));

Muralidhar
  • 113
  • 1
  • 12
  • welcome to stack overflow. Please go through the link on how to ask effective questions. https://stackoverflow.com/help/how-to-ask – pvpkiran Dec 15 '17 at 17:49
  • 3
    Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – s7vr Dec 15 '17 at 17:52
  • Look for `$filter` operator implementation in spring – Ajit Soman Dec 15 '17 at 17:58
  • how are you querying mongo? are you using spring data ? Add more details – pvpkiran Dec 15 '17 at 17:59

0 Answers0