1

I have some very large documents. Due to embedados and arrays fields. An example follows:

{
  "_id" => BSON::ObjectId('5654b4b3204528cfa0000001'),
  "rating" => 0,
  "status" => :ok,
  "foo" => '...',
  "bar" => '...',
  # a few fields
  "group_ids" =>  [
    BSON::ObjectId('55ba5d5420452896a8000007'),
    BSON::ObjectId('5602bf4c2045289107000001')
  ],
  "events": [
    {
      "_id" => BSON::ObjectId('5654b4b3204528cfa0000002'),
      "field_a" => '...',
      "field_b" => '...',
    }
    # more than 200 entries
  ]
}

There may be hundreds of events and no more than ten group_ids for example. For my application I need some fields, but I must NOT return events(it is very slow).

In version 3 Mongoid I filtered the necessary fields, performing my actions and alter some of these fields returned:

entry = Model.without(:events).first
# Change some fields, for example:
entry.rating = 3
entry.status = :error

entry.save
# Mongoid 4: raise Mongoid::Errors::ReadonlyDocument

But Mongoid 4: "Documents now loaded from criteria using #only or #without will now raise an error When Attempting to save".

I can not work it out with atomic operations, because my model has callbacks (after_save, before_update) that depend on previous states of fields (ActiveModel::Dirty) like status_changed?, group_ids_change.

They can help me with any suggestions?

Iago
  • 308
  • 3
  • 9

0 Answers0