Problem updating a subdocument with Mongoose [duplicate]
This question already has an answer here:
How to Update Multiple Array Elements in mongodb
10 answers
How do I update Array Elements matching criteria in a MongoDB document?
1 answer
I need to update a subdocument in my mongoDB with Mongoose. This is my schema:
{
"_id" : ObjectId("5bf66d78de019add1b8f96fe"),
"duration" : {
"start" : ISODate("2018-09-20T07:48:56.874Z"),
"end" : ISODate("2018-11-20T08:48:56.874Z")
},
"status" : "inreview",
"title" : "Good title",
"webData" : [
{
"_id" : ObjectId("5bf67288449273df45629b07"),
"webCode" : "be_mx",
"title" : "Title test MX",
"subtitle" : "Subtitle test MX",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
},
{
"_id" : ObjectId("5bf66d7dde019add1b8f970c"),
"webCode" : "be_ar",
"title" : "Title test AR",
"subtitle" : "Subtitle test AR",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
}
],
}
In my update function I receive the id of the Scholarship, the name and the new value of the field that I want to edit and the filter webCode that says what subdocument I need to edit.
I'm trying to do that with this query:
Scholarship.update({
'_id': id,
}, {
$set: {
'webData.$[i]': {
[fieldName]: newValue
}
}
}, {
multi: true,
arrayFilters: [{ "i.webCode": webCode }]
});
But when I execute that the subdocument of this scholarship disapear and it creates another subdocument with the new field. For example, if I pass the id, the fieldName "title", the newValue to "New title" and the "webCode" is "be_mx", this is the result of my query:
{
"_id" : ObjectId("5bf66d78de019add1b8f96fe"),
"duration" : {
"start" : ISODate("2018-09-20T07:48:56.874Z"),
"end" : ISODate("2018-11-20T08:48:56.874Z")
},
"status" : "inreview",
"title" : "Good title",
"webData" : [
{
"_id" : ObjectId("5bf672884492dfsdfe4353"),
"title" : "New title",
"spins" : ,
"categories" : ,
"comments" : ,
},
{
"_id" : ObjectId("5bf66d7dde019add1b8f970c"),
"webCode" : "be_ar",
"title" : "Title test AR",
"subtitle" : "Subtitle test AR",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
}
],
}
If I activate the mongoose debug I have this query:
scholarships.update({ _id: ObjectId("5bf66d78de019add1b8f96fe") }, { '$set': { 'webData.$[i]': { _id: ObjectId("5bf67288449273df45629b07"), title: 'New', spins: , categories: , comments: } } }, { multi: true, arrayFilters: [ { 'i.webCode': 'be_mx' } ] })
Why happends that? And why my mongoose debug show that it is sending the spins, categories and comments array when I only want to edit the title?
UPDATED:
I had some error in my code, this is the correct syntax:
Scholarship.update({
'_id': id,
}, {
$set: {
[`webData.$[i].${fieldName}`]: newValue
}
}, {
multi: true,
arrayFilters: [{ "i.webCode": webCode }]
});
node.js mongodb mongoose
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 22 '18 at 9:34
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 to Update Multiple Array Elements in mongodb
10 answers
How do I update Array Elements matching criteria in a MongoDB document?
1 answer
I need to update a subdocument in my mongoDB with Mongoose. This is my schema:
{
"_id" : ObjectId("5bf66d78de019add1b8f96fe"),
"duration" : {
"start" : ISODate("2018-09-20T07:48:56.874Z"),
"end" : ISODate("2018-11-20T08:48:56.874Z")
},
"status" : "inreview",
"title" : "Good title",
"webData" : [
{
"_id" : ObjectId("5bf67288449273df45629b07"),
"webCode" : "be_mx",
"title" : "Title test MX",
"subtitle" : "Subtitle test MX",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
},
{
"_id" : ObjectId("5bf66d7dde019add1b8f970c"),
"webCode" : "be_ar",
"title" : "Title test AR",
"subtitle" : "Subtitle test AR",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
}
],
}
In my update function I receive the id of the Scholarship, the name and the new value of the field that I want to edit and the filter webCode that says what subdocument I need to edit.
I'm trying to do that with this query:
Scholarship.update({
'_id': id,
}, {
$set: {
'webData.$[i]': {
[fieldName]: newValue
}
}
}, {
multi: true,
arrayFilters: [{ "i.webCode": webCode }]
});
But when I execute that the subdocument of this scholarship disapear and it creates another subdocument with the new field. For example, if I pass the id, the fieldName "title", the newValue to "New title" and the "webCode" is "be_mx", this is the result of my query:
{
"_id" : ObjectId("5bf66d78de019add1b8f96fe"),
"duration" : {
"start" : ISODate("2018-09-20T07:48:56.874Z"),
"end" : ISODate("2018-11-20T08:48:56.874Z")
},
"status" : "inreview",
"title" : "Good title",
"webData" : [
{
"_id" : ObjectId("5bf672884492dfsdfe4353"),
"title" : "New title",
"spins" : ,
"categories" : ,
"comments" : ,
},
{
"_id" : ObjectId("5bf66d7dde019add1b8f970c"),
"webCode" : "be_ar",
"title" : "Title test AR",
"subtitle" : "Subtitle test AR",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
}
],
}
If I activate the mongoose debug I have this query:
scholarships.update({ _id: ObjectId("5bf66d78de019add1b8f96fe") }, { '$set': { 'webData.$[i]': { _id: ObjectId("5bf67288449273df45629b07"), title: 'New', spins: , categories: , comments: } } }, { multi: true, arrayFilters: [ { 'i.webCode': 'be_mx' } ] })
Why happends that? And why my mongoose debug show that it is sending the spins, categories and comments array when I only want to edit the title?
UPDATED:
I had some error in my code, this is the correct syntax:
Scholarship.update({
'_id': id,
}, {
$set: {
[`webData.$[i].${fieldName}`]: newValue
}
}, {
multi: true,
arrayFilters: [{ "i.webCode": webCode }]
});
node.js mongodb mongoose
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 22 '18 at 9:34
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.
The syntax is wrong. You mean$set: { [`webData.$[i].${fieldName}`]: newValue }as there is a difference between the dot notation path and just supplying a new object after the matched identifier. The linked answers show correct syntax examples. You also don't need the "positional filtered" form for a "single" match as you presumably have here. So read the answers applicable to "single" updates as well
– Neil Lunn
Nov 22 '18 at 9:38
It works! Thank you very much @NeilLunn
– Noelia Romero
Nov 22 '18 at 9:49
add a comment |
This question already has an answer here:
How to Update Multiple Array Elements in mongodb
10 answers
How do I update Array Elements matching criteria in a MongoDB document?
1 answer
I need to update a subdocument in my mongoDB with Mongoose. This is my schema:
{
"_id" : ObjectId("5bf66d78de019add1b8f96fe"),
"duration" : {
"start" : ISODate("2018-09-20T07:48:56.874Z"),
"end" : ISODate("2018-11-20T08:48:56.874Z")
},
"status" : "inreview",
"title" : "Good title",
"webData" : [
{
"_id" : ObjectId("5bf67288449273df45629b07"),
"webCode" : "be_mx",
"title" : "Title test MX",
"subtitle" : "Subtitle test MX",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
},
{
"_id" : ObjectId("5bf66d7dde019add1b8f970c"),
"webCode" : "be_ar",
"title" : "Title test AR",
"subtitle" : "Subtitle test AR",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
}
],
}
In my update function I receive the id of the Scholarship, the name and the new value of the field that I want to edit and the filter webCode that says what subdocument I need to edit.
I'm trying to do that with this query:
Scholarship.update({
'_id': id,
}, {
$set: {
'webData.$[i]': {
[fieldName]: newValue
}
}
}, {
multi: true,
arrayFilters: [{ "i.webCode": webCode }]
});
But when I execute that the subdocument of this scholarship disapear and it creates another subdocument with the new field. For example, if I pass the id, the fieldName "title", the newValue to "New title" and the "webCode" is "be_mx", this is the result of my query:
{
"_id" : ObjectId("5bf66d78de019add1b8f96fe"),
"duration" : {
"start" : ISODate("2018-09-20T07:48:56.874Z"),
"end" : ISODate("2018-11-20T08:48:56.874Z")
},
"status" : "inreview",
"title" : "Good title",
"webData" : [
{
"_id" : ObjectId("5bf672884492dfsdfe4353"),
"title" : "New title",
"spins" : ,
"categories" : ,
"comments" : ,
},
{
"_id" : ObjectId("5bf66d7dde019add1b8f970c"),
"webCode" : "be_ar",
"title" : "Title test AR",
"subtitle" : "Subtitle test AR",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
}
],
}
If I activate the mongoose debug I have this query:
scholarships.update({ _id: ObjectId("5bf66d78de019add1b8f96fe") }, { '$set': { 'webData.$[i]': { _id: ObjectId("5bf67288449273df45629b07"), title: 'New', spins: , categories: , comments: } } }, { multi: true, arrayFilters: [ { 'i.webCode': 'be_mx' } ] })
Why happends that? And why my mongoose debug show that it is sending the spins, categories and comments array when I only want to edit the title?
UPDATED:
I had some error in my code, this is the correct syntax:
Scholarship.update({
'_id': id,
}, {
$set: {
[`webData.$[i].${fieldName}`]: newValue
}
}, {
multi: true,
arrayFilters: [{ "i.webCode": webCode }]
});
node.js mongodb mongoose
This question already has an answer here:
How to Update Multiple Array Elements in mongodb
10 answers
How do I update Array Elements matching criteria in a MongoDB document?
1 answer
I need to update a subdocument in my mongoDB with Mongoose. This is my schema:
{
"_id" : ObjectId("5bf66d78de019add1b8f96fe"),
"duration" : {
"start" : ISODate("2018-09-20T07:48:56.874Z"),
"end" : ISODate("2018-11-20T08:48:56.874Z")
},
"status" : "inreview",
"title" : "Good title",
"webData" : [
{
"_id" : ObjectId("5bf67288449273df45629b07"),
"webCode" : "be_mx",
"title" : "Title test MX",
"subtitle" : "Subtitle test MX",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
},
{
"_id" : ObjectId("5bf66d7dde019add1b8f970c"),
"webCode" : "be_ar",
"title" : "Title test AR",
"subtitle" : "Subtitle test AR",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
}
],
}
In my update function I receive the id of the Scholarship, the name and the new value of the field that I want to edit and the filter webCode that says what subdocument I need to edit.
I'm trying to do that with this query:
Scholarship.update({
'_id': id,
}, {
$set: {
'webData.$[i]': {
[fieldName]: newValue
}
}
}, {
multi: true,
arrayFilters: [{ "i.webCode": webCode }]
});
But when I execute that the subdocument of this scholarship disapear and it creates another subdocument with the new field. For example, if I pass the id, the fieldName "title", the newValue to "New title" and the "webCode" is "be_mx", this is the result of my query:
{
"_id" : ObjectId("5bf66d78de019add1b8f96fe"),
"duration" : {
"start" : ISODate("2018-09-20T07:48:56.874Z"),
"end" : ISODate("2018-11-20T08:48:56.874Z")
},
"status" : "inreview",
"title" : "Good title",
"webData" : [
{
"_id" : ObjectId("5bf672884492dfsdfe4353"),
"title" : "New title",
"spins" : ,
"categories" : ,
"comments" : ,
},
{
"_id" : ObjectId("5bf66d7dde019add1b8f970c"),
"webCode" : "be_ar",
"title" : "Title test AR",
"subtitle" : "Subtitle test AR",
"spins" : ,
"categories" : ,
"comments" : ,
"publishDate" : ISODate("2018-11-21T08:49:01.047Z")
}
],
}
If I activate the mongoose debug I have this query:
scholarships.update({ _id: ObjectId("5bf66d78de019add1b8f96fe") }, { '$set': { 'webData.$[i]': { _id: ObjectId("5bf67288449273df45629b07"), title: 'New', spins: , categories: , comments: } } }, { multi: true, arrayFilters: [ { 'i.webCode': 'be_mx' } ] })
Why happends that? And why my mongoose debug show that it is sending the spins, categories and comments array when I only want to edit the title?
UPDATED:
I had some error in my code, this is the correct syntax:
Scholarship.update({
'_id': id,
}, {
$set: {
[`webData.$[i].${fieldName}`]: newValue
}
}, {
multi: true,
arrayFilters: [{ "i.webCode": webCode }]
});
This question already has an answer here:
How to Update Multiple Array Elements in mongodb
10 answers
How do I update Array Elements matching criteria in a MongoDB document?
1 answer
node.js mongodb mongoose
node.js mongodb mongoose
edited Nov 22 '18 at 9:52
Noelia Romero
asked Nov 22 '18 at 9:32
Noelia RomeroNoelia Romero
186
186
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 22 '18 at 9:34
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 22 '18 at 9:34
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.
The syntax is wrong. You mean$set: { [`webData.$[i].${fieldName}`]: newValue }as there is a difference between the dot notation path and just supplying a new object after the matched identifier. The linked answers show correct syntax examples. You also don't need the "positional filtered" form for a "single" match as you presumably have here. So read the answers applicable to "single" updates as well
– Neil Lunn
Nov 22 '18 at 9:38
It works! Thank you very much @NeilLunn
– Noelia Romero
Nov 22 '18 at 9:49
add a comment |
The syntax is wrong. You mean$set: { [`webData.$[i].${fieldName}`]: newValue }as there is a difference between the dot notation path and just supplying a new object after the matched identifier. The linked answers show correct syntax examples. You also don't need the "positional filtered" form for a "single" match as you presumably have here. So read the answers applicable to "single" updates as well
– Neil Lunn
Nov 22 '18 at 9:38
It works! Thank you very much @NeilLunn
– Noelia Romero
Nov 22 '18 at 9:49
The syntax is wrong. You mean
$set: { [`webData.$[i].${fieldName}`]: newValue } as there is a difference between the dot notation path and just supplying a new object after the matched identifier. The linked answers show correct syntax examples. You also don't need the "positional filtered" form for a "single" match as you presumably have here. So read the answers applicable to "single" updates as well– Neil Lunn
Nov 22 '18 at 9:38
The syntax is wrong. You mean
$set: { [`webData.$[i].${fieldName}`]: newValue } as there is a difference between the dot notation path and just supplying a new object after the matched identifier. The linked answers show correct syntax examples. You also don't need the "positional filtered" form for a "single" match as you presumably have here. So read the answers applicable to "single" updates as well– Neil Lunn
Nov 22 '18 at 9:38
It works! Thank you very much @NeilLunn
– Noelia Romero
Nov 22 '18 at 9:49
It works! Thank you very much @NeilLunn
– Noelia Romero
Nov 22 '18 at 9:49
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes

The syntax is wrong. You mean
$set: { [`webData.$[i].${fieldName}`]: newValue }as there is a difference between the dot notation path and just supplying a new object after the matched identifier. The linked answers show correct syntax examples. You also don't need the "positional filtered" form for a "single" match as you presumably have here. So read the answers applicable to "single" updates as well– Neil Lunn
Nov 22 '18 at 9:38
It works! Thank you very much @NeilLunn
– Noelia Romero
Nov 22 '18 at 9:49