Update Field in Array of Objects MongoDb [duplicate]
This question already has an answer here:
How do I update Array Elements matching criteria in a MongoDB document?
1 answer
I have an object that looks like the following:
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : [
{
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc",
"sync" : false
},
{
"address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
"sync" : false
}
]
}
I am trying to update the sync field for specific object in the array. So for example if I update the sync field for the object at index 0
or address tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc
by doing the following it yields:
> db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": true}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : [
{
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
"sync" : true
},
{
"address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
"sync" : false
}
]
}
It works as expected, but if I perform the same update command to switch back sync:false
, it gives me a malformed object, while deleting the rest of the array:
> db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": false}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : {
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
"sync" : true,
"0" : {
"sync" : false
}
}
}
mongodb
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 15 '18 at 5:48
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 do I update Array Elements matching criteria in a MongoDB document?
1 answer
I have an object that looks like the following:
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : [
{
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc",
"sync" : false
},
{
"address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
"sync" : false
}
]
}
I am trying to update the sync field for specific object in the array. So for example if I update the sync field for the object at index 0
or address tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc
by doing the following it yields:
> db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": true}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : [
{
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
"sync" : true
},
{
"address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
"sync" : false
}
]
}
It works as expected, but if I perform the same update command to switch back sync:false
, it gives me a malformed object, while deleting the rest of the array:
> db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": false}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : {
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
"sync" : true,
"0" : {
"sync" : false
}
}
}
mongodb
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 15 '18 at 5:48
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 do I update Array Elements matching criteria in a MongoDB document?
1 answer
I have an object that looks like the following:
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : [
{
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc",
"sync" : false
},
{
"address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
"sync" : false
}
]
}
I am trying to update the sync field for specific object in the array. So for example if I update the sync field for the object at index 0
or address tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc
by doing the following it yields:
> db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": true}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : [
{
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
"sync" : true
},
{
"address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
"sync" : false
}
]
}
It works as expected, but if I perform the same update command to switch back sync:false
, it gives me a malformed object, while deleting the rest of the array:
> db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": false}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : {
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
"sync" : true,
"0" : {
"sync" : false
}
}
}
mongodb
This question already has an answer here:
How do I update Array Elements matching criteria in a MongoDB document?
1 answer
I have an object that looks like the following:
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : [
{
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc",
"sync" : false
},
{
"address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
"sync" : false
}
]
}
I am trying to update the sync field for specific object in the array. So for example if I update the sync field for the object at index 0
or address tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc
by doing the following it yields:
> db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": true}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : [
{
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
"sync" : true
},
{
"address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
"sync" : false
}
]
}
It works as expected, but if I perform the same update command to switch back sync:false
, it gives me a malformed object, while deleting the rest of the array:
> db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": false}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : {
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
"sync" : true,
"0" : {
"sync" : false
}
}
}
This question already has an answer here:
How do I update Array Elements matching criteria in a MongoDB document?
1 answer
mongodb
mongodb
asked Nov 15 '18 at 2:25
DefinitelyNotAGoatDefinitelyNotAGoat
1011
1011
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 15 '18 at 5:48
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 15 '18 at 5:48
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 |
1 Answer
1
active
oldest
votes
You can update specific array item, if item match your case using $ operator
db.delegate_list.update({"delegates.address":"tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"}, {$set:{"delegates.$.sync": false}})
It will update the sync filed which address is "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"
only, and not going to add any extra field.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can update specific array item, if item match your case using $ operator
db.delegate_list.update({"delegates.address":"tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"}, {$set:{"delegates.$.sync": false}})
It will update the sync filed which address is "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"
only, and not going to add any extra field.
add a comment |
You can update specific array item, if item match your case using $ operator
db.delegate_list.update({"delegates.address":"tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"}, {$set:{"delegates.$.sync": false}})
It will update the sync filed which address is "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"
only, and not going to add any extra field.
add a comment |
You can update specific array item, if item match your case using $ operator
db.delegate_list.update({"delegates.address":"tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"}, {$set:{"delegates.$.sync": false}})
It will update the sync filed which address is "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"
only, and not going to add any extra field.
You can update specific array item, if item match your case using $ operator
db.delegate_list.update({"delegates.address":"tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"}, {$set:{"delegates.$.sync": false}})
It will update the sync filed which address is "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"
only, and not going to add any extra field.
answered Nov 15 '18 at 4:12
Biplab MalakarBiplab Malakar
42837
42837
add a comment |
add a comment |