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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:22:51+00:00 2026-05-29T13:22:51+00:00

Assuming I have a collection in MongoDB with 5000 records, each containing something similar

  • 0

Assuming I have a collection in MongoDB with 5000 records, each containing something similar to:

{
"occupation":"Doctor",
"name": {
   "first":"Jimmy",
   "additional":"Smith"
}

Is there an easy way to rename the field “additional” to “last” in all documents? I saw the $rename operator in the documentation but I’m not really clear on how to specify a subfield.

  • 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-05-29T13:22:52+00:00Added an answer on May 29, 2026 at 1:22 pm

    You can use:

    db.foo.update({}, {
        $rename: {
            "name.additional": "name.last"
        }
    }, false, true);
    

    Or to just update the docs which contain the property:

    db.foo.update({
        "name.additional": {
            $exists: true
        }
    }, {
        $rename: {
            "name.additional": "name.last"
        }
    }, false, true);
    

    The false, true in the method above are: { upsert:false, multi:true }. You need the multi:true to update all your records.

    Or you can use the former way:

    remap = function (x) {
        if (x.additional) {
            db.foo.update({
                _id: x._id
            }, {
                $set: {
                    "name.last": x.name.additional
                }, $unset: {
                    "name.additional": 1
                }
            });
        }
    }
        
    db.foo.find().forEach(remap);
    

    In MongoDB 3.2 you can also use

    db.students.updateMany({}, { 
        $rename: { 
            "oldname": "newname" 
        } 
    })
    

    The general syntax of this is

    db.collection.updateMany(filter, update, options)
    

    https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/

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

Sidebar

Related Questions

Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
I have a collection I'm unable to drop, I'm assuming that the - in
Say I have a TabControl that is presenting a collection of Foo objects (each
Using Java, assuming v1.6. I have a collection where the unique index is a
I have a collection which returns a model for each date within a given
We have a collection of log data, where each document in the collection is
I need to implement a prioritised collection. Assuming we have the following three values
Assuming I have some data in the form of Customer1 Name Address Order1 ID
I have a ListBox bound to an ObservableCollection. The items in the collection each
Assuming I have a column called A and I want to check if A

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.