如何在spring-data mongodb中使用updateOption和arrayFilters?

问题描述 投票:2回答:2

我有一个如Mongodb所示的文档:

现在,我想去一个基于特定“_id”的文档,对于那个文档,想要进入“日程安排”列表,其中针对少数特定日期(不仅是一个日期,而是多个日期),我想要将状态更新为“BOOKED”。我通过此链接,How to apply update using Filtered positional operator with arrayFilters但在MongoTemplate类中,updateMulti方法不接受updateOption参数。有人可以请帮助

我出去了。真的会感激任何建议。谢谢。

注意:我使用的是spring-data版本“2.0.3.RELEASE”,MongoDB驱动程序版本是v3.6.4。

以下是一份文件:

{
      "_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"
    }
mongodb spring-boot spring-data-mongodb
2个回答
2
投票

如果spring-data中没有“updateOption”,那么我们可以使用普通的驱动程序jar。我希望它能解决你的问题。

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

2
投票

它将很快在spring-data-mongodb中提供。见:https://github.com/spring-projects/spring-data-mongodb/pull/656

使用它看起来像:

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

在此期间,您应该能够将其与快照maven存储库一起使用:

<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>
© www.soinside.com 2019 - 2024. All rights reserved.