条件更新多个字段

时间:2016-06-24 06:21:55

标签: rethinkdb

我是rethinkdb的新手,我正在尝试编写一个更新查询首选upsert,如果它们不存在或者它们的值小于我想要设置的新值,它会将值设置为多个字段。以下是我用mongodb做的​​方式

 collection.updateOne(new BasicDBObject(BookKeeperEvent.tenantPropertyName, bookKeeper.getTenantId()).append(BookKeeperEvent.timeLayerPropertyName, bookKeeper.getTimeLayer()),
            new BasicDBObject("$max", new BasicDBObject(BookKeeperEvent.latestFullDataPropertyName, bookKeeper.getLatestFullData())
                    .append(BookKeeperEvent.latestRawDataPropertyName, bookKeeper.getLatestRawData())
                    .append(BookKeeperEvent.latestHealthPropertyName, bookKeeper.getLatestHealth())
                    .append(BookKeeperEvent.lastUpdatePropertyName, bookKeeper.getLastUpdate())
                    .append(BookKeeperEvent.firstFullDataPropertyName, bookKeeper.getFirstFullData()))
                    .append("$setOnInsert", new BasicDBObject(BookKeeperEvent.tenantPropertyName, bookKeeper.getTenantId()).append(BookKeeperEvent.timeLayerPropertyName, bookKeeper.getTimeLayer())),
            new UpdateOptions().upsert(true))

此代码仅在它们为null时设置tenantId和timeLayer,并且仅在其他字段为null或值小于我在查询中设置的值时设置其他字段。 有没有办法在rethinkdb中做同样的事情?怎么样?

1 个答案:

答案 0 :(得分:0)

我认为以下查询与我在mongo中所做的相同,但它失败了。

r.table('book_keeper').filter({tenantId: '123', timeLayer: 4}).coerceTo('array').do(function (matches) {

返回r.branch(     matches.isEmpty()     r.table(' book_keeper')。insert({tenantId:' 123',timeLayer:4,latestRawData:100,id:4}),     r.table(' book_keeper')。get(4).update(function(doc){       返回r.branch(       doc.not(doc.hasFields(' firstFullData&#39))或(DOC(' firstFullData'。)LT(100)),         {firstFullData:100},       空值)     }     )) })

该摘要是:

e: Expected 1 argument but found 2 in:
r.table("book_keeper").filter({"tenantId": "123", "timeLayer": 4}).coerceTo("array").do(function(var_193) { return r.branch(var_193.isEmpty(), r.table("book_keeper").insert({"tenantId": "123", "timeLayer": 4, "latestRawData": 100, "id": 4}), r.table("book_keeper").get(4).update(function(var_194) { return r.branch(var_194.not(var_194.hasFields("firstFullData")).or(var_194("firstFullData").lt(100)), {"firstFullData": 100}, null); })); })

我该如何解决?

谢谢,

丹妮拉