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 8194701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:54:43+00:00 2026-06-07T04:54:43+00:00

We have made a change in our data model and we would like to

  • 0

We have made a change in our data model and we would like to apply it to all documents in one of our collections:

{
  "id":60,
  "measurement":{
    "steps":1274.0
  },
  "date":"2012-05-15T00:00:00Z"
}

to:

{  
  "id":60,
  "measurement":{
      "distance":{
          "steps":1274.0}
      },
  "date":"2012-05-15T00:00:00Z"
}

Essentially, we want to further nest the field steps, placing it under the distance field.

As for measurement.step, we would like to convert measurement.miles to measurement.distance.miles and measurement.minutes to measurement.time.minutes.

Any thoughts and/or suggestions would be appreciated.

  • 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-07T04:54:45+00:00Added an answer on June 7, 2026 at 4:54 am

    Assuming you’re asking how to script the schema change, which wasn’t quite clear in the question: I would do something like this, unless you have more cases for the document structure, or mixed cases:

    // find all the measurement documents with steps
    db.coll.find({"measurement.steps":{$exists:true}}).forEach(function(doc) {      
      // create a new distance subdoc with the steps
      doc.measurement.distance = {steps:doc.measurement.steps};
      // delete the old steps subdoc
      delete doc.measurement.steps;
      // save the document
      db.coll.save(doc);
    });
    // find all the measurement documents with miles
    db.coll.find({"measurement.miles":{$exists:true}}).forEach(function(doc) {
      // create a new distance subdoc with the miles
      doc.measurement.distance = {miles:doc.measurement.miles};
      delete doc.measurement.miles;
      db.coll.save(doc);
    });
    // find all the measurement documents with minutes
    db.coll.find({"measurement.minutes":{$exists:true}}).forEach(function(doc) {
      // create a new time subdoc with the minutes
      doc.measurement.time = {minutes:doc.measurement.minutes};
      delete doc.measurement.minutes;
      db.coll.save(doc);
    });
    

    You could pretty easily do the equivalent in the language/driver of your choice to ensure types, but it is probably faster to do in the shell. Hope it helps.

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

Sidebar

Related Questions

Post-release, I have made one small change to one form in our development site
We have a data model that has some requirements. I would like to find
I have a slight problem... we made a change to our url structure the
I have made a minor change to an Excel macro, and although it worked
I have a change tracking framework that tracks changed made to domain objects on
I have made some changes in one perforce client, but haven't submitted them. I
We have made substantial changes to our physical DB, now as it is the
I have a weekly script that moves data from our live database and puts
All - I have recently upgraded our production environment from Sql Server Reporting Services
I made a script to get JSON data from a file on our server

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.