Jacobian
Jacobian

Reputation: 10802

Unable to add multiple docs to Couchdb because of identical _ids

I use couchdb module to deal with CouchDb in Python. One strange thing happens when I try to insert many different docs. When I print this docs before they are inserted to the database I see a lot of identical automatically generated _id fields. My own dictionaries even do not have such a key (I mean _id), but still couchdb generetes them automatically and make a lot of clones. I do insertion like this:

print(mydict) # here I see a lot of identical keys for different elements
db.save(mydict)

Upvotes: 0

Views: 673

Answers (2)

h4cc
h4cc

Reputation: 303

You seem to be mistakenly using db.save, because you will not insert two documents when calling it multiple times with the same object. If your data has a _id value, a UPDATE of the document will be done, else a INSERT. These should be no problem with CouchDB generating duplicate IDs, it might be you using the same id over and over againt. In case you want to generate the Ids on the client side, have a look at this part of the documentation: https://github.com/djc/couchdb-python/blob/master/couchdb/client.py#L389-395

Upvotes: 1

Jacobian
Jacobian

Reputation: 10802

It seems as if documents are inserted too fast, then you may get a lot of identical _ids. Instead of default _id value, I now get hard-coded _ids with python ObjectId() function:

mydict['_id'] = str(ObjectId())

That enables me to insert multiple docs "at once"

Upvotes: 0

Related Questions