2

以下は、github.com/rethinkdbの問題で私が尋ねた質問で、彼らが答えたものです。rethinkdb コミュニティの利益のためにここに投稿しています

1.以下のデータから最大日付をフィルタリングする方法:

[   
{"TimeStamp": Fri Oct 11 2013 05:51:12 GMT+00:00},
{"TimeStamp": Thu Oct 10 2013 15:41:09 GMT+00:00},
{"TimeStamp": Thu Oct 10 2013 15:44:04 GMT+00:00}
]

2.計算フィールドの作成方法は? rethinkdb にこのようなデータがあります

[
{id: 1, sales: 1000, discount: 0.1},
{id: 2, sales: 2000, discount: 0.2},
{id: 3, sales:3000, discount: 0.1}
]

以下のようにどのように変換できますか:

[{id: 1, sales: 1000, discount: 0.1, discAmt: 100, netSales: 900},
{id: 2, sales: 2000, discount: 0.2, discAmt: 400, netSales: 1600},
{id: 3, sales: 3000, discount: 0.1, discAmt: 300, netSales: 2700}
]

上記から割引フィールドを削除するには?

4

1 に答える 1

6

1.最大日付

最大タイムスタンプについては、最後のタイムスタンプを取得したいだけの場合は、次のことができます

r.table("foo").map( r.row("TimeStamp") ).reduce( function(left, right) {
   return r.branch( left.gt(right),
    left,
    right
})

タイムスタンプが最大のドキュメントが必要な場合は、次のことができます

r.table("foo").orderBy(r.desc("TimeStamp")).limit(1)

これによりテーブル全体がソートされるため、TimeStamp にインデックスを作成して使用することをお勧めします。

r.table("foo").orderBy({index: "TimeStamp"}).limit(1)

2.計算フィールド

r.db("books").table("sales").update( function(row){ 
  var discAmt = row("sales").mul(row("discount")); 
    return {
    discAmt: discAmt,
    netSales: row("sales").sub(discAmt)
 }
})
.run()

3.フィールドの削除

 r.db("books").table("sales").replace( function(row) {
     return row.unpick("discount");
 })
 .run()

これが私のような rethinkdb スターターの助けになることを願っています。

ありがとう

于 2013-10-12T06:13:27.247 に答える