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

后端 未结 2 2016
误落风尘
误落风尘 2021-01-03 01:35

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 \"schedu

相关标签:
2条回答
  • 2021-01-03 01:43

    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>
    
    0 讨论(0)
  • 2021-01-03 01:57

    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 讨论(0)
提交回复
热议问题