Why does updating/finding exact documents fail when the document has an embedded document?

26 views
Skip to first unread message

Dan

unread,
Apr 17, 2012, 2:59:36 PM4/17/12
to mongodb-user
I'm using pymongo for reference.

So if I have a document like this:

{
'name': 'bill',
'color': 'blue',
'subdocument': {
'title': 'Untitled',
'content': 'Hello World',
}
}

If I find this document doing db.users.find()[0] and then pass it as
an argument to update, Mongo will not find the document to update
unless I remove the subdocument. Passing the found document to
find_one also fails unless the subdocument is removed.

I need to specify all the fields for the sake of optimized locking.
How can I easily retrieve the document, update it, and pass the
original fields as requirements for not having been changed?

Scott Hernandez

unread,
Apr 17, 2012, 3:07:45 PM4/17/12
to mongod...@googlegroups.com
In general using a single version/counter field for optimistic locking
works better and reduced network latency and traffic, as well as can
be made more efficient with indexes (let alone the cpu overhead of
comparing every field under heavy concurrency).

http://code.google.com/p/morphia/wiki/MongoNewsletterArticleDec2010

Are you using a mapper with python? Some of them have things like this built in.

> --
> 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.
>

Dan

unread,
Apr 18, 2012, 10:08:34 AM4/18/12
to mongodb-user
Thank you! This is such a simple solution, I don't know why I didn't
think of it. I will add a version field that I increment on every
update.

And no, I don't use a mapper with python (that I know of). I'll
google the term, but any extra info you could provide would be
appreciated.

Thanks again for the version idea.

Scott Hernandez

unread,
Apr 19, 2012, 1:52:55 PM4/19/12
to mongod...@googlegroups.com
http://api.mongodb.org/python/current/tools.html#orm-like-layers

In python this is pretty easy to do yourself with your own classes/code.

Reply all
Reply to author
Forward
0 new messages