10

この例を使用して ID の配列に参加しようとしています: https://github.com/rethinkdb/rethinkdb/issues/1533#issuecomment-26112118

テーブルのスニペットを保存します

{ 
  "storeID": "80362c86-94cc-4be3-b2b0-2607901804dd",
  "locations": [
    "5fa96762-f0a9-41f2-a6c1-1335185f193d",
    "80362c86-94cc-4be3-b2b0-2607901804dd"
  ]
}

場所の表のスニペット

{
  "lat": 125.231345,
  "lng": 44.23123,
  "id": "80362c86-94cc-4be3-b2b0-2607901804dd"
}

店舗を選択して、その店舗の場所に参加したいと思います。

ReThinkDB 寄稿者からの元の例:

r.table("blog_posts")
.concat_map(lambda x: x["comment_ids"].map(lambda y: x.merge("comment_id" : y)))
.eq_join("comment_id", r.table("comments"))

私のJSへの変換の試み

r.table("stores")
 .concatMap((function(x){ 
   return x("locations").map((function(y){ 
     return x("locations").add(y);
   })) 
}))
.eqJoin("locations", r.table("locations"))

結果

RqlRuntimeError: Expected type ARRAY but found STRING

4

1 に答える 1

12

concatMap を正しく使用していません。クエリの最初の部分を次のようにします。

r.table("stores")
 .concatMap(function (x) {
   return x("locations");
 })

それを実行してみてください。次のようになります。

["5fa96762-...", "80362c86-...", ...]

次に、これを他のテーブルに結合する必要があります。ID の配列をテーブルに結合するには、次のように eqjoin を使用できます。

array.eqJoin(function (row) { return row; }, table)

詳細はこちら: rql get multiple documents from list of keys rethinkdb in javascript .

すべてをまとめると、次のようになります。

r.table("stores")
 .concatMap(function (x) {
   return x("locations")
 })
 .eqJoin(function (i) { return i; }, r.table("locations"))

ストアからドキュメントを取得するには:

r.table("stores")
 .concatMap(function (x) {
   return x("locations").map(function (loc) {
      return x.merge({locations: loc});
   });
 })
 .eqJoin("locations", r.table("locations"))
于 2014-01-03T19:01:36.613 に答える