开发者

MongoDB: getting ObjectId of last inserted document with multiple, concurrent writers?

Consider the following scenario wi开发者_运维百科th MongoDB:

Three writers (A,B,C) insert a document into the same collection.

A inserts first, followed by B, followed by C.

How can we guarantee A retrieves the ObjectId of the document he inserted and not B's document or C's document? Do we need to serialize the writes (i.e., only permit B to write after A inserts and retrieves the ObjectId), or does MongoDB offer some native functionality for this scenario?

Thanks!

We're on Rails.


the normal pattern here is for the driver to allocate the ObjectId and then you know what it is for the insert even before the server gets it.


You can generate the _id value in your client applications (writers) before inserting the document. This way you don't need to rely on the server generating the ObjectId an retrieving the correct value. Most MongoDB language drivers will do this for you automatically if you leave the _id blank.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜