8

I have a document as shown below in Mongodb:

Now, I want to go to a document based on specific "_id" and for that document, want to go to "schedule" list in which for the few specific dates (not only one date, but more than one), I want to update the status as "BOOKED".I went through this link, How to apply update using Filtered positional operator with arrayFilters but in MongoTemplate class, updateMulti method does NOT take the updateOption parameter. Can someone please help

Me out. really will be appreciated for any suggestion. Thanks.

Note: I am using spring-data version "2.0.3.RELEASE", MongoDB driver version is v3.6.4.

Below is a document:

{
      "_id": "x1",
      "timeZone": "America/Los_Angeles",
      "schedule": [
        {
          "Date": "2018-07-10T00:00:00.000Z",
          "status": "AVAILABLE"
        },
        {
          "Date": "2018-07-10T00:30:00.000Z",
          "status": "AVAILABLE"
        },
        {
          "Date": "2018-07-10T08:00:00.000Z",
          "status": "AVAILABLE"
        }
      ],
      "_class": "com.scheduler.persistance.model.Calendar"
    }

3 Answers 3

8

It will soon be available in spring-data-mongodb. See : https://github.com/spring-projects/spring-data-mongodb/pull/656

Using it will look like :

new Update()
.set("grades.$[element]", 100)
.filterArray(Criteria.where("element").gte(100));

In the meantime, you should be able to use it with their snapshot maven repository:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-mongodb</artifactId>
  <version>2.2.0.DATAMONGO-2215-SNAPSHOT</version>
</dependency>

<repository>
  <id>spring-libs-snapshot</id>
  <name>Spring Snapshot Repository</name>
  <url>https://repo.spring.io/libs-snapshot</url>
</repository>
Sign up to request clarification or add additional context in comments.

Comments

5

If there is no "updateOption" in spring-data, then we can use plain driver jar. I hope it will solve your problem.

MongoDatabase db = client.getDatabase("test");

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z');
format.setTimeZone("EST");

List<Date> dateList = Arrays.asList(new Date[]
                                {format.parse("2018-07-10T00:30:00.000Z")}
                               );

db.getCollection("test").updateMany(new Document("_id", "x1"),
        new Documen("$set", new Document("schedule.$[elem].status", "booked")),
        new UpdateOptions().arrayFilters(Arrays.asList(new Document[]
            {new Document("elem.Date", new Document("$in", dateList))}
        )));

1 Comment

Hi Ravi, I am more looking into the spring data solution. I appreciate your response. Thanks.
0

// Assuming your collection is based out of class by name Test.class

Query query = new Query();
query.addCriteria(Criteria.where("_id").is("someID");
Update update = new Update();
update.set("schedule.$[myRec].status", "BOOKED" );
update.filterArray("myRec.Date", dateValue);
FindAndModifyOptions options = new FindAndModifyOptions().returnNew(true);
mongoTemplate.findAndModify(query, update, options, Test.class);

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.