2

The PouchDB Manual suggests using Date().toJSON() to generate a new id for each document. However this an result in _id values that are identical.. Does PouchDB have a built in mechanism for dealing with this or should we be using something like eventid?

Specifically the Date().toJson() could produce id values are at the same instant in time like this:

2018-01-26T21:12:15.574Z
2018-01-26T21:12:15.574Z
2018-01-26T21:12:15.575Z

If a document already has an _id of 2018-01-26T21:12:15.574Z and another attempt is made to create a new document with the same _id value 2018-01-26T21:12:15.574Z, will pouchdb throw an exception or somehow change the _id property such that it becomes a unique id? For example 2018-01-26T21:12:15.574Z_1.

Ole
  • 41,793
  • 59
  • 191
  • 359
  • Note for passersby, I think the context of that recommendation — is related to how the bulk API will sort by `_id` ([`allDocs()`](https://pouchdb.com/guides/bulk-operations.html), etc.). – floer32 May 04 '21 at 20:38

1 Answers1

1

Pouchdb/Couchdb auto generates ids for new documents if that's what you are asking. we usually like to combine unique props of the doc like name or location with the date.toJSON to make ids self descriptive. you take one look at the _id and you know what the doc is about

Femi Oni
  • 796
  • 2
  • 9
  • 25
  • Now I'm even more confused :). So if someone uses Date().toJson() to create the _id value, what does pouchdb set the _id value to? – Ole Jan 26 '18 at 21:16
  • if you have set your _id couchdb would not change it. it only set _ids for documents not having the _id props. if you're having problem setting _ids and there's not business reason why you should do it yourself leave and let couchdb handle it for you – Femi Oni Jan 26 '18 at 21:20
  • I see - I updated the question a bit to make it clearer - let me know if I can clearify further - thanks. – Ole Jan 26 '18 at 21:22
  • So does pouchdb throw an exception when the same id is used over again to create a new document or does it alter the id? – Ole Jan 26 '18 at 21:31
  • no exception if you used existing _id, that's an updated. it will check for and try to update _rev, if it's missing in the new doc or not up-to-date then it will throw error for invalid ref but not for id because it will see it as an update not a new insert – Femi Oni Jan 26 '18 at 21:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163981/discussion-between-femi-oni-and-ole). – Femi Oni Jan 26 '18 at 21:41