How to use updateOption with arrayFilters in spring-data mongodb?

只愿长相守 提交于 2019-12-12 08:56:11

问题


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"
    }

回答1:


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>



回答2:


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))}
        )));


来源:https://stackoverflow.com/questions/51225487/how-to-use-updateoption-with-arrayfilters-in-spring-data-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!