2016-06-24 63 views
0

我是新的rethinkdb,我想寫一個更新查詢首選upsert它將設置值爲幾個字段,如果他們不存在或他們的值小於我想要的新值設置。下面是我的MongoDB所採取的方式Conditinal更新的幾個字段

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)) 

此代碼設置tenantId和timeLayer只有當他們是空的,而對於其他領域,只有當他們都爲空或值小於我在設置的值查詢。 有沒有辦法在rethinkdb中做同樣的事情?如何?

感謝, 丹妮拉

+0

我覺得下面的查詢就相當於我在蒙戈沒有,但它失敗。 – Daniela

回答

0

我覺得下面的查詢就相當於我在蒙戈沒有,但它失敗。 。

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

返回r.branch( matches.isEmpty(), r.table( 'book_keeper')插入({tenantId: '123',timeLayer:4,latestRawData:100,ID:4} ), r.table('book_keeper')。get(4).update(function(doc){ return r.branch( doc.not(doc.hasFields('firstFullData'))。or(doc(' 。firstFullData')LT(100)), {firstFullData:100}, 空) } )) })

的excption是:

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); })); }) 

我該如何解決?

感謝,

丹妮拉