Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB collections as FIFO stack

I need to implement the FIFO logic with MongoDB collection:

  1. Pop the first document from MongoDB collection.
  2. Push the document to MongoDB collection and place it as a last document.

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

like image 955
Valentin Avatar asked Dec 08 '25 14:12

Valentin


1 Answers

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.

like image 70
Remon van Vliet Avatar answered Dec 10 '25 22:12

Remon van Vliet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!