skip to Main Content

I have this document:

{
  "id" : "001",
  "items" : {
    "persons" : [ 
        {
            "city" : "London",
            "color" : "red",
            "status" : "1"
        }, 
        {
            "city" : "Paris",
            "color" : "blue",
            "status" : "0"
        }
     ],
    "animals" : [ 
        {
            "city" : "Luton",
            "color" : "red",
            "status" : "1"
        }, 
        {
            "city" : "London",
            "color" : "red",
            "status" : "0"
        }
     ]
}

In java, or at least direcly in mongo, I need to update the status of persons and animals to a new value for each item in those arrays who meets the criteria of city and/or color from input.
Status is obligatory, city and color are not obligatory.

Ex:

input city=London, color=red and status=9 will result in:

{
  "id" : "001",
  "items" : {
    "persons" : [ 
        {
            "city" : "London",
            "color" : "red",
            "status" : "9"
        }, 
        {
            "city" : "Paris",
            "color" : "blue",
            "status" : "0"
        }
     ],
    "animals" : [ 
        {
            "city" : "Luton",
            "color" : "red",
            "status" : "1"
        }, 
        {
            "city" : "London",
            "color" : "red",
            "status" : "9"
        }
     ]
}

input color=red and status=7 will result in:

{
  "id" : "001",
  "items" : {
    "persons" : [ 
        {
            "city" : "London",
            "color" : "red",
            "status" : "7"
        }, 
        {
            "city" : "Paris",
            "color" : "blue",
            "status" : "0"
        }
     ],
    "animals" : [ 
        {
            "city" : "Luton",
            "color" : "red",
            "status" : "7"
        }, 
        {
            "city" : "London",
            "color" : "red",
            "status" : "7"
        }
     ]
}

I tried a lot of things, but nothing works. If somebody can came with a solution is great, but I will be satisfied with a piece of advice to guide me to solve this matter.

2

Answers


  1. From your question i just understood that you want to update status of those 2 objects
    persons, animals by criteria of or.

    Query query = Query.query(new Criteria().orOperator(
                   Criteria.where("items.persons.city").is("your value"),
                           Criteria.where("items.persons.color").is("your value"),
                           Criteria.where("items.animals.city").is("your value"),
                           Criteria.where("items.animals.color").is("your value")
           ));
           Update update = new Update().set("items.$.status", "9");
    mongoTemplate.updateFirst(query, update, "Your class name");
    
    Login or Signup to reply.
  2. I hope it will help you

    {
    $or: [
    {
      "items.persons.color": "red"
    },
    {
      "items.animals.color": "red"
    }
    ]
    },
    {
    $set: {
    "items.persons.$[elem].status": "7",
    "items.animals.$[elem].status": "7"
    }
    },
    {
     arrayFilters: [
    {
      "elem.color": "red"
    }
    ]
    })
    

    for only color
    ex- https://mongoplayground.net/p/UTLAVWQ884V

    for both color and city
    ex- https://mongoplayground.net/p/qRdi2c1VDQg

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search