7

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"
    }
coder
  • 8,346
  • 16
  • 39
  • 53

3 Answers3

7

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>
jemag
  • 164
  • 1
  • 10
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))}
        )));
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);
Suresh T
  • 67
  • 1
  • 4