0

I have the following schema:

unique_doc_id: {
   title: title1,
   desc: desc
   listField: [listElement1, listElement2]
   mapField: {someKey: someValue}
}

I have a microService1 which hosts an API used to take inputs for above document…then notifies microService2 using an unordered queue which is supposed to update this document in the DB.

I want to maintain field-level timestamps for concurrent updates on same field for the following scenario: Let’s say someone calls API at T1 with title1, at T2 (T1 + 1ms) with title2…but the updates reach microService2 out of order i.e. the T1 update reaches AFTER T2 update. i.e. let’s say the write of title2 happens first, so, when the title1 update reaches microService2, that write should fail. I beleive this can be done with field level timestamps.

Does mongoDB have support for adding version/timestamp at field level ??

Isn't this a common problem ?

Any doc links / example code chunks would help.

Thanks!

  • Assuming all timestamp clocks are synchronized, you could check that the update timestamp is later than the document timestamp. – rickhg12hs Aug 06 '23 at 08:47
  • sorry, I didnt get you. I'm asking if there's a default versioning/timestamp capability at a field level, not at the document level. Will improve the question's wording to better imply the same. – user10501730 Aug 06 '23 at 18:39
  • There may be some ODM middleware to ease handling field-level timestamps, or you could use something like `"title": {"text": "Some title text", "updatedAt": BSON Date/timestamp}`. – rickhg12hs Aug 06 '23 at 20:08
  • Makes sense. From your experience, should this not be a common problem for mongo users ? Or is it just me because i have two separate services so I cant have locking mechanisms in place; which is very intentional as I want to keep my API logic very light. – user10501730 Aug 06 '23 at 20:22
  • For me, not needing to handle locks is a great convenience. MongoDB server handles all the [locking mechanisms](https://www.mongodb.com/docs/manual/faq/concurrency/) I need. – rickhg12hs Aug 06 '23 at 20:33

0 Answers0