简体   繁体   English

MongoDB Java 驱动:如何使用$cond算子?

[英]MongoDB Java Driver: How to use $cond operator?

I've written the following MongoDB update query:我编写了以下 MongoDB 更新查询:

db.getCollection("product").update(
  {},
  [
    {
      $set: {
        availability: {
          $cond: [
            {
              $eq: ["$availability", true],
            },
            "Yes",
            "No",
          ],
        },
      },
    }
  ]
);

that updates all documents in product collection: set a new value for availability property based on its current value.更新product集合中的所有文档:根据当前值设置availability属性的新值。

And I am trying to rewrite the query above for MongoDB Java driver:我正在尝试为 MongoDB Java 驱动程序重写上面的查询:

db.getCollection("product")
     .updateMany(new BsonDocument(), Updates.set("available", "YES"));

but I am stuck with $cond operator, I don't know how to translate $cond operator from MongoDB query to Java driver.但我坚持使用$cond运算符,我不知道如何将$cond运算符从 MongoDB 查询转换为 Java 驱动程序。

Could you please suggest possible options?你能建议可能的选择吗?

You need to use different variation which requires ClientSession rather than MongoClient您需要使用需要ClientSession而不是MongoClient的不同变体

UpdateResult updateResult = this.mongoColWrite.
                 updateMany(this.clientSession, new Document(), setUpdateList);

empty doc is the condition. empty doc是条件。 You can add any condition there.您可以在那里添加任何条件。

You have to provide the list for the update.您必须提供更新列表。

[
    {
      $set: {
        availability: {
          $cond: [
            {
              $eq: ["$availability", true],
            },
            "Yes",
            "No",
          ],
        },
      },
    }
  ]

LinkedList<Object> condList = new LinkedList<Object>();
LinkedList<Object> eqArray = new LinkedList<Object>();
eqArray.add("$availability");
eqArray.add(true); 
condList.add(new Document("$eq"), eqArray);
condList.add("Yes");
condList.add("No");

Document availDoc = new Document("$cond", condList)
Document setDoc = new Document("$set", availDoc);

LinkedList<Document> setUpdateList = new LinkedList<Document>();
setUpdateList.add(setDoc);

This is how I usually does.这就是我通常的做法。 Every array is a list .每个数组都是一个list Every object is a Document .每个 object 都是一个Document

Example with older version 旧版本示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在mongodb中使用$ and运算符-Java驱动程序 - How to use $and operator in mongodb - java driver 如何将 $concat 与 $cond 与 MongoDB Java 驱动程序一起使用 - How to use $concat with a $cond with MongoDB Java drivers 使用MongoDB 3.0 Java驱动程序聚合框架时,如何在$ project运算符中使用$ substr表达式 - How do I use the $substr expression in $project operator when using MongoDB 3.0 Java Driver aggregation framework MongoDB 3.2 Java驱动程序:如何使用AggregateOperation - MongoDB 3.2 Java Driver: How To Use AggregateOperation MongoDB Java驱动程序如何在投影中使用过滤器 - Mongodb java driver how to use filter in projection 如何在MongoDB Java驱动程序中使用sureIndex - How to use ensureIndex in MongoDB Java Driver? java代码中如何使用MongoTemplate cond和filter? - How to use MongoTemplate cond and filter in java code? 带有 Java 驱动程序的 MongodDB:如何查找嵌套属性以及如何使用“和”运算符 - MongodDB with Java driver: How to find nested atributes and how to use “and” operator 如何使用MongoDB Java驱动程序对ISODate属性的dayOfYear进行分组? - How to use MongoDB Java driver to group by dayOfYear on ISODate attributes? 如何在Groovy / Grails中使用Mongodb的Java驱动程序 - How to use Mongodb's java driver in groovy/grails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM