Rename field in mongoose [duplicate]












0
















This question already has an answer here:




  • How can I rename a field for all documents in MongoDB?

    7 answers




I have two JSON objects, In each, there is a firstname field. I want to rename firstname to name, also want to import the existing firstname values to name, using mongoose.



Schema:



const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const user = new Schema({
firstname:{ type:String },
lastname:{ type:String },
username:{ type:String },
password:{ type:String },
user_type:{ type:String },
})

module.exports = mongoose.model('user', user)


Data Sample:



_id: "5bf5fbef16a06667027eecc2", firstname: "Hareesh", lastname: "Pitchikala", username: "123", password: "123", user_type: "employer", __v: 0









share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 20:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • How can I rename a field for all documents in MongoDB?

      7 answers




    I have two JSON objects, In each, there is a firstname field. I want to rename firstname to name, also want to import the existing firstname values to name, using mongoose.



    Schema:



    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    const user = new Schema({
    firstname:{ type:String },
    lastname:{ type:String },
    username:{ type:String },
    password:{ type:String },
    user_type:{ type:String },
    })

    module.exports = mongoose.model('user', user)


    Data Sample:



    _id: "5bf5fbef16a06667027eecc2", firstname: "Hareesh", lastname: "Pitchikala", username: "123", password: "123", user_type: "employer", __v: 0









    share|improve this question















    marked as duplicate by Neil Lunn mongodb
    Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 23 '18 at 20:21


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • How can I rename a field for all documents in MongoDB?

        7 answers




      I have two JSON objects, In each, there is a firstname field. I want to rename firstname to name, also want to import the existing firstname values to name, using mongoose.



      Schema:



      const mongoose = require('mongoose');
      const Schema = mongoose.Schema;
      const user = new Schema({
      firstname:{ type:String },
      lastname:{ type:String },
      username:{ type:String },
      password:{ type:String },
      user_type:{ type:String },
      })

      module.exports = mongoose.model('user', user)


      Data Sample:



      _id: "5bf5fbef16a06667027eecc2", firstname: "Hareesh", lastname: "Pitchikala", username: "123", password: "123", user_type: "employer", __v: 0









      share|improve this question

















      This question already has an answer here:




      • How can I rename a field for all documents in MongoDB?

        7 answers




      I have two JSON objects, In each, there is a firstname field. I want to rename firstname to name, also want to import the existing firstname values to name, using mongoose.



      Schema:



      const mongoose = require('mongoose');
      const Schema = mongoose.Schema;
      const user = new Schema({
      firstname:{ type:String },
      lastname:{ type:String },
      username:{ type:String },
      password:{ type:String },
      user_type:{ type:String },
      })

      module.exports = mongoose.model('user', user)


      Data Sample:



      _id: "5bf5fbef16a06667027eecc2", firstname: "Hareesh", lastname: "Pitchikala", username: "123", password: "123", user_type: "employer", __v: 0




      This question already has an answer here:




      • How can I rename a field for all documents in MongoDB?

        7 answers








      node.js database mongodb rest






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 13:49









      Andrejs Cainikovs

      18.4k25373




      18.4k25373










      asked Nov 23 '18 at 12:57









      HareeshHareesh

      107




      107




      marked as duplicate by Neil Lunn mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 23 '18 at 20:21


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Neil Lunn mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 23 '18 at 20:21


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          0














          First, you'll need to add name to your schema so it becomes:



          const user = new Schema({
          name:{ type:String },
          firstname:{ type:String }, //keep this for now
          lastname:{ type:String },
          username:{ type:String },
          password:{ type:String },
          user_type:{ type:String },
          });


          Now, in your app code somewhere, you'll need to run this:



          User.update({}, { $rename: { firstname: 'name' } }, { multi: true }, function(err, blocks) {
          if(err) { throw err; }
          console.log('done!');
          });


          Now, you can remove firstname from your schema if you wish.






          share|improve this answer































            -1














            Documents can be updated as below:



            db.collection.updateMany({},{ $rename: { "firstname": "name" } } );





            share|improve this answer
























            • this isn't using mongoose - which is what the OP requested.

              – Alex
              Nov 23 '18 at 13:52


















            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            First, you'll need to add name to your schema so it becomes:



            const user = new Schema({
            name:{ type:String },
            firstname:{ type:String }, //keep this for now
            lastname:{ type:String },
            username:{ type:String },
            password:{ type:String },
            user_type:{ type:String },
            });


            Now, in your app code somewhere, you'll need to run this:



            User.update({}, { $rename: { firstname: 'name' } }, { multi: true }, function(err, blocks) {
            if(err) { throw err; }
            console.log('done!');
            });


            Now, you can remove firstname from your schema if you wish.






            share|improve this answer




























              0














              First, you'll need to add name to your schema so it becomes:



              const user = new Schema({
              name:{ type:String },
              firstname:{ type:String }, //keep this for now
              lastname:{ type:String },
              username:{ type:String },
              password:{ type:String },
              user_type:{ type:String },
              });


              Now, in your app code somewhere, you'll need to run this:



              User.update({}, { $rename: { firstname: 'name' } }, { multi: true }, function(err, blocks) {
              if(err) { throw err; }
              console.log('done!');
              });


              Now, you can remove firstname from your schema if you wish.






              share|improve this answer


























                0












                0








                0







                First, you'll need to add name to your schema so it becomes:



                const user = new Schema({
                name:{ type:String },
                firstname:{ type:String }, //keep this for now
                lastname:{ type:String },
                username:{ type:String },
                password:{ type:String },
                user_type:{ type:String },
                });


                Now, in your app code somewhere, you'll need to run this:



                User.update({}, { $rename: { firstname: 'name' } }, { multi: true }, function(err, blocks) {
                if(err) { throw err; }
                console.log('done!');
                });


                Now, you can remove firstname from your schema if you wish.






                share|improve this answer













                First, you'll need to add name to your schema so it becomes:



                const user = new Schema({
                name:{ type:String },
                firstname:{ type:String }, //keep this for now
                lastname:{ type:String },
                username:{ type:String },
                password:{ type:String },
                user_type:{ type:String },
                });


                Now, in your app code somewhere, you'll need to run this:



                User.update({}, { $rename: { firstname: 'name' } }, { multi: true }, function(err, blocks) {
                if(err) { throw err; }
                console.log('done!');
                });


                Now, you can remove firstname from your schema if you wish.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 13:39









                AlexAlex

                20.5k38161288




                20.5k38161288

























                    -1














                    Documents can be updated as below:



                    db.collection.updateMany({},{ $rename: { "firstname": "name" } } );





                    share|improve this answer
























                    • this isn't using mongoose - which is what the OP requested.

                      – Alex
                      Nov 23 '18 at 13:52
















                    -1














                    Documents can be updated as below:



                    db.collection.updateMany({},{ $rename: { "firstname": "name" } } );





                    share|improve this answer
























                    • this isn't using mongoose - which is what the OP requested.

                      – Alex
                      Nov 23 '18 at 13:52














                    -1












                    -1








                    -1







                    Documents can be updated as below:



                    db.collection.updateMany({},{ $rename: { "firstname": "name" } } );





                    share|improve this answer













                    Documents can be updated as below:



                    db.collection.updateMany({},{ $rename: { "firstname": "name" } } );






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '18 at 13:43









                    kRiZkRiZ

                    321310




                    321310













                    • this isn't using mongoose - which is what the OP requested.

                      – Alex
                      Nov 23 '18 at 13:52



















                    • this isn't using mongoose - which is what the OP requested.

                      – Alex
                      Nov 23 '18 at 13:52

















                    this isn't using mongoose - which is what the OP requested.

                    – Alex
                    Nov 23 '18 at 13:52





                    this isn't using mongoose - which is what the OP requested.

                    – Alex
                    Nov 23 '18 at 13:52



                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python