Rename field in mongoose [duplicate]
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
node.js database mongodb rest
marked as duplicate by Neil Lunn
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.
add a comment |
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
node.js database mongodb rest
marked as duplicate by Neil Lunn
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.
add a comment |
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
node.js database mongodb rest
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
node.js database mongodb rest
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
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
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.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
Documents can be updated as below:
db.collection.updateMany({},{ $rename: { "firstname": "name" } } );
this isn'tusing mongoose
- which is what the OP requested.
– Alex
Nov 23 '18 at 13:52
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '18 at 13:39
AlexAlex
20.5k38161288
20.5k38161288
add a comment |
add a comment |
Documents can be updated as below:
db.collection.updateMany({},{ $rename: { "firstname": "name" } } );
this isn'tusing mongoose
- which is what the OP requested.
– Alex
Nov 23 '18 at 13:52
add a comment |
Documents can be updated as below:
db.collection.updateMany({},{ $rename: { "firstname": "name" } } );
this isn'tusing mongoose
- which is what the OP requested.
– Alex
Nov 23 '18 at 13:52
add a comment |
Documents can be updated as below:
db.collection.updateMany({},{ $rename: { "firstname": "name" } } );
Documents can be updated as below:
db.collection.updateMany({},{ $rename: { "firstname": "name" } } );
answered Nov 23 '18 at 13:43
kRiZkRiZ
321310
321310
this isn'tusing mongoose
- which is what the OP requested.
– Alex
Nov 23 '18 at 13:52
add a comment |
this isn'tusing 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
add a comment |