如何在spring-data mongodb中使用带有arrayFilters的updateOption?

Bha*_*ksi 5 mongodb spring-data-mongodb spring-boot

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

现在,我想转到基于特定“ _id”的文档,对于该文档,我想转到“计划”列表中的几个特定日期(不仅是一个日期,而是多个日期),我想更新状态为“BOOKED”。我浏览了这个链接, How to apply update using Filtered positional operator with arrayFilters but in MongoTemplate class, updateMultimethod does not take the updateOptionparameter. 有人可以帮忙吗

我出来。对于任何建议,我们将不胜感激。谢谢。

注意:我使用的是 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"
    }
Run Code Online (Sandbox Code Playgroud)

小智 6

它很快将在 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));
Run Code Online (Sandbox Code Playgroud)

同时,您应该能够将它与他们的快照 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>
Run Code Online (Sandbox Code Playgroud)


小智 5

如果 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))}
        )));
Run Code Online (Sandbox Code Playgroud)