Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8669411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:28:05+00:00 2026-06-12T18:28:05+00:00

I need to implement the FIFO logic with MongoDB collection: Pop the first document

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:28:06+00:00Added an answer on June 12, 2026 at 6:28 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement a FIFO queue for messages on a game server so
I need to implement portable code, but I do not know how to deal
I need to implement AI for game based on fuzzy logic. I need to
I need to implement a basic FIFO queue of objects. Whats the best built-in
I need to implement the following query in SQL Server: select * from table1
I need to implement a tool that runs programs from SQL Server database when
I need to implement a flexible module to handle Local and Push notifications in
I need to implement the producer/consumer pattern around a fixed-size FIFO queue. I think
I need to implement a program to join an image from one column to
I need to implement an efficient excel-like app. I'm looking for a data structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.