1

Having trouble trying to update the ids field in the document structure:

[
    [0] {
                   "rank" => nil,
             "profile_id" => 3,
        "daily_providers" => [
            [0] {
                "relationships" => [
                  [0] {
                      "relationship_type" => "friend",
                                  "count" => 0
                  },
                  [1] {
                      "relationship_type" => "acquaintance",
                                    "ids" => [],
                                  "count" => 0
                  }
                ],
                "countries" => [
                   [0] {
                         "country_name" => "United States",
                         "count" => 0
                       },

                   [1] {
                         "country_name" => "Great Britain",
                         "count" => 0
                       }
                ],
                "provider_name" => "foo",
                 "date" => 20130912
            },
            [1] {
                "provider_name" => "bar"
            }
        ]
    }
]
Christian Fazzini
  • 19,613
  • 21
  • 110
  • 215

1 Answers1

1

In JavaScript, you can do

r.db('test').table('test').get(3).update(function(doc) {
  return {daily_providers: doc("daily_providers").changeAt(
    0,
    doc("daily_providers").nth(0).merge({
      relationships: doc("daily_providers").nth(0)("relationships").changeAt(
        1,
        doc("daily_providers").nth(0)("relationships").nth(1).merge({
          ids: [1]
        })
      )
    })
  )}
})

Which becomes in Ruby

r.db('test').table('test').get(3).update{ |doc|
  {"daily_providers" => doc["daily_providers"].changeAt(
    0,
    doc["daily_providers"][0].merge({
      "relationships" => doc["daily_providers"][0]["relationships"].changeAt(
        1,
        doc["daily_providers"][0]["relationships"][1].merge({
          ids => [1]
        })
      )
    })
  )}
}

You should probably have another table for the daily providers and do joins. That would make things way more simpler.

neumino
  • 4,342
  • 1
  • 18
  • 17
  • I read a little too fast, I thought the dataset was the same as in http://stackoverflow.com/questions/18763866/how-would-you-use-map-reduce-on-this-document-structuret -- updating... – neumino Sep 13 '13 at 04:39
  • That also requires that I need to know the index number of the embedded array? – Christian Fazzini Sep 13 '13 at 06:43
  • Yes, how else can you know which daily_provider or relationship you want to update? – neumino Sep 13 '13 at 18:58
  • Might not be ideal for documents that change in time. Index numbers may differ from document to document. Knowing the index number might be impossible, without having to parse through the document first – Christian Fazzini Sep 14 '13 at 04:43