MongoDB Java Driver:如何使用$cond运算符?

huangapple go评论73阅读模式
英文:

MongoDB Java Driver: How to use $cond operator?

问题

我已经编写了以下的MongoDB更新查询:

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

这个查询会更新product集合中的所有文档:根据当前值设置availability属性的新值。

而我正在尝试使用MongoDB Java驱动程序重写上面的查询:

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

但是我卡在了$cond操作符上,我不知道如何将MongoDB查询中的$cond操作符翻译成Java驱动程序。

您能否提供可能的选项?

英文:

I've written the following MongoDB update query:

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.

And I am trying to rewrite the query above for MongoDB Java driver:

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.

Could you please suggest possible options?

答案1

得分: 2

需要使用不同的变体,需要使用 ClientSession 而不是 MongoClient

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

空文档 是条件。你可以在那里添加任何条件。

你必须提供更新的列表。

[
    {
      $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);

这是我通常的做法。每个数组都是一个 list。每个对象都是一个 Document

旧版本示例

英文:

You need to use different variation which requires ClientSession rather than MongoClient

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

empty doc is the condition. You can add any condition there.

You have to provide the list for the update.

[
    {
      $set: {
        availability: {
          $cond: [
            {
              $eq: [&quot;$availability&quot;, true],
            },
            &quot;Yes&quot;,
            &quot;No&quot;,
          ],
        },
      },
    }
  ]

LinkedList&lt;Object&gt; condList = new LinkedList&lt;Object&gt;();
LinkedList&lt;Object&gt; eqArray = new LinkedList&lt;Object&gt;();
eqArray.add(&quot;$availability&quot;);
eqArray.add(true); 
condList.add(new Document(&quot;$eq&quot;), eqArray);
condList.add(&quot;Yes&quot;);
condList.add(&quot;No&quot;);

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

LinkedList&lt;Document&gt; setUpdateList = new LinkedList&lt;Document&gt;();
setUpdateList.add(setDoc);

This is how I usually does. Every array is a list. Every object is a Document.

Example with older version

huangapple
  • 本文由 发表于 2020年8月7日 14:13:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63296204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定