partial update or create question (ruby driver)

162 views
Skip to first unread message

Henry

unread,
Jul 16, 2010, 5:19:06 PM7/16/10
to mongodb-user
This has probably been answered before, but I couldn't find the
answer.

I have a collection of data I want to import into MongoDB once an
hour. Given a hash of attributes, I want to update those attributes
in an existing document if it exists, otherwise I want to create a new
document.

Using the ruby driver I can do something like this:

@products.update({'sku' => 'ABCD'}, attribute_hash)

But this only updates existing documents, and will not add new ones.

I can do:

@products.update({'sku' => 'ABCD'}, attribute_hash, :upsert => true)

The upsert option will do an 'insert' if the record does not exist,
however, it does a document replace on any existing documents.

What if I just want to update those attributes that I specify, but
leave the other attributes in the document alone?

Thanks.

Henry.

Kyle Banker

unread,
Jul 16, 2010, 5:24:48 PM7/16/10
to mongod...@googlegroups.com
@products.update({'sku' => 'ABCD'}, {"$set" => {"foo" => "bar"}}, :upsert => true)

On Fri, Jul 16, 2010 at 5:21 PM, Eliot Horowitz <elioth...@gmail.com> wrote:
update( { full object } , { $set : {} } , true )
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Reply all
Reply to author
Forward
0 new messages