I need to implement the FIFO logic with MongoDB collection:
The documents in collection do not have any indexes except auto-generated _id (ObjectId).
I wonder, is it possible to findAndRemove the first document from collection and to guarantee that push and pop operations will perform as FIFO stack atomically?
I know that it is possible to do with an array inside document with atomic push and pop operations, but the main problem is that if I will store all my data inside 1 document's array, it's size will exceed 16MB (the maximum allowed size of MongoDB document)
Thanks in advance, Valentin
If you're accessing your stack from a single machine you can do so using findAndRemove :
db.col.findAndModify({query:{}, sort:{i: -1}, remove:true})
This will return the removed value and remove the document itself atomically per your request. Where "i" is a time sorted field (_id works if it is an ObjectId). If you're using the FIFO stack/collection from multiple instances you will have to ensure somehow that the "i" values are atomically increased across all instances or you will have to live with the collection being FIFO-ish rather than strictly so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With