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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:19:51+00:00 2026-06-12T07:19:51+00:00

I was a bit dumb and created a collection in MongoDB with spaces in

  • 0

I was a bit dumb and created a collection in MongoDB with spaces in the key names:

{
        "_id" : ObjectId("5065f84d00ea10c01e00003f"),
        "DateTime" : ISODate("2012-09-27T15:19:00Z"),
        "Port Name" : "CL3-J",
        "Port Number" : "40",
        "Avg I/O /sec" : "2024.0",
        "array_serial" : "xxxxx"
}

I’m trying to define a function on the mongo shell to rename them:

var metricNames = ["Processor Busy %",
                      "Avg I/O /sec",    
                      "Avg Xfer /sec"];


var renameFields = function(collection) {
  var record = db[collection].findOne({});
  var myMetric;

  for (var key in record) {
    if(metricNames.indexOf(key) !== -1){
      myMetric = metricNames[metricNames.indexOf(key)];
      print(myMetric);
      break;
    } else {
      continue;
    }
  }

  db[collection].update( { }, { $rename : { 
                                            'DateTime'    : 'datetime',
                                            'Port Name'        : 'port_name',
                                            'Port Number'      : 'port_number',
                                            'Adaptor ID'       : 'adaptor_id',
                                            'Processor ID'     : 'processor_id',
                                            'Processor Type'   : 'processor_type',
                                             myMetric          : 'metric' } }, false, true );

}

Depending on the collection specified the metric could possibly be one of the three… the issue is that all of the specified fields except the ‘myMetric’ one are being renamed correctly, the metric field is remaining the original field with spaces.

Any ideas?


…an update.

It seems that collections with the “Avg Xfer /sec” metric are properly renamed to “metric” but collections with “Avg I/O /sec” or “Processor Busy %” are not. I’m not entirely sure why that would be.

  • 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-12T07:19:53+00:00Added an answer on June 12, 2026 at 7:19 am

    It’s because the rename update is looking for a property named 'myMetric' to rename and not one named myMetric‘s value. You need to build up your rename object programmatically:

    var rename = { 
      'DateTime'    : 'datetime',
      'Port Name'        : 'port_name',
      'Port Number'      : 'port_number',
      'Adaptor ID'       : 'adaptor_id',
      'Processor ID'     : 'processor_id',
      'Processor Type'   : 'processor_type'
    };
    rename[myMetric] = 'metric';
    db[collection].update( { }, { $rename : rename }, false, true );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might sound a bit dumb. I always had this impression that web.config should
This might sound a bit dumb but am confused. I know the strlen() would
Ok, I feel a bit dumb having to ask this. There are any number
A bit of a dumb one... but how do I get a variable of
Sorry for the probably dumb question, but i wanted to practice loops a bit
This question is a bit dumb but I have to know it. Is there
I have a question that might seem a bit dumb since I have been
This is a dumb question that's been bugging me for a bit. Why can't
Quick but dumb question. That kind of cascades a little bit. If I create
This may sound like a bit of a dumb question but how do I

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.