Update data to MongoDB

12 views
Skip to first unread message

Natapol Pornputtapong

unread,
Feb 26, 2014, 5:14:05 PM2/26/14
to mongo...@googlegroups.com
Hi,

I use MongoModel to create a specific database interface. When I edit some properties of object and save, an data duplication error is raised. It seem like save method trying to create new document and put to database, not to update. I tried to work around this problem for a while. I found that update method that called from create_or_update is exactly the same as create method. If the update method is correct, how actually I can update objects to MongoDB? 

Thank you in advance.
/Natapol

Sam Pohlenz

unread,
Feb 27, 2014, 10:58:10 PM2/27/14
to mongo...@googlegroups.com
Hi Natapol,

MongoModel uses upserts [1] for saving documents, so seeing only update calls is correct.

It sounds like maybe you have a unique index defined on the collection causing the duplication error?
If not, please post the logs of the MongoDB calls being made.

Regards,
Sam

Natapol Pornputtapong

unread,
Mar 6, 2014, 1:54:57 PM3/6/14
to mongo...@googlegroups.com
Hi Sam,

Thank you for your answer.

Yes, There are unique indexes defined in the class. However, I still do not have any idea to update the modified objects. So, Do I need to create a specific method to update them?

Sorry that I have sent you a private reply. I used wrong reply.

Best,
Natapol

Sam Pohlenz

unread,
Mar 7, 2014, 1:36:19 AM3/7/14
to mongo...@googlegroups.com
Hi Natapol,

The #save method is used for both creating and updating documents, so
something like the below code will update an existing document:

article = Article.find(id)
article.title = "New Title"
article.save

If this is raising an exception relating to duplicate data, then it
indicates that some other document in the collection already has that
value, violating the uniqueness index.

Regards,
Sam
Reply all
Reply to author
Forward
0 new messages