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

  • Home
  • SEARCH
  • 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 9025557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:18:05+00:00 2026-06-16T06:18:05+00:00

I have a collection in my MongoDB database that had Mongoid::Versioning enabled for it

  • 0

I have a collection in my MongoDB database that had Mongoid::Versioning enabled for it quite sometime ago. Unfortunately, it made some of my documents extremely large in size. I see some that are over 711K. This makes for expensive disk i/o and expensive read/write times. I am looking for a solution to go through this collection (which has almost 2 million documents), and remove all mongoid versioning, safely if possible. From what I can tell, Mongoid just stores the versions in an array attribute named just that, versions. If there is a way to gank it from all of my documents in a way that won’t completely make the database unusable (in terms of performance while I do an entire disk scan + write/update), that would be great.

  • 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-16T06:18:06+00:00Added an answer on June 16, 2026 at 6:18 am

    There is a lot of ways to handle this situation. I’ve tried this a couple of different ways, and for an trial of ten thousand records they have similar processing times. I’ve tried another and found it much worse. I’ll attach them here in case it helps.

    Here I am working on the hypothesis that batching the process up will help alleviate the impact on your database.

    The first method would be to do finds on the collection, with a limit to handle a batch.

    var batchsize = 50
    var c = db.collection.count()
    for(x=0;x<Math.floor(c/batchsize);x++){
        db.collection.find({versions: {$exists:true}}).limit(batchsize).forEach(function(cur){
            db.collection.update({_id:cur._id},{$unset:{versions:""}})
        })
    }
    

    The issue here will be the collection scans that will be required on every new batch. The limit will help with the impact, but it is still costly on the collection.

    A second method would be to fill an array with the _ids of all the documents which have a versions array, then iterate through the array and update:

    var arr = db.collection.find({versions:{$exists:true}},{_id:1}).toArray()
    while(arr.length>0){
        for(x=0;x<batchsize;x++){
            var curId = arr.pop();
            db.collection.update(curId,{$unset:{versions:""}})
        }
    }
    

    This will mean an initial full collection scan, but after this point it is all iterating through the array and updating in batches.

    I’ve tried a third method, where I work through the collection finding an _id greater than the previous one and updating, but found this to be much more expensive (even though it was able to use the index on _id). I’m adding it here in case it is useful.

    var curid = db.collection.find({_id:{$gt:MinKey}},{_id:1}).sort({_id:1}).limit(1).next()._id;
    while(curid < MaxKey){
        db.collection.update({_id:curid},{$unset:{versions:""}});
        curid = db.collection.find({_id:{$gt:curid}},{_id:1}).sort({_id:1}).limit(1).next()._id;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a one-shot Node script that makes some changes to a MongoDB database
Let's assume that I have a collection in mongodb, where all of its documents
I have a MongoDB collection with data that was not saved through my Derby
I have a collection of articles in MongoDB that has the following structure: {
I have an open source project that deals with mongodb database. I am trying
I have MongoDB.BsonDocument, i want to convert that BsonDocument to List of collection, how
In a MongoDB database, I have a collection of items, and each item stores
I have a database in MongoDb that contains two collections: 'categories' and 'articles'. I'm
I have simple MongoDB collection and I am using SLIM micro-framework to develop a
I have 10000 documents in one MongoDB collection. I'd like to update all the

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.