How can I update one field of an array when given another array containing the state of the field to update?
By : user6762139
Date : March 29 2020, 07:55 AM
will help you If we know that the indexes and answers are the same (otherwise we can sort them). A simple for loop can handle this. code :
for(var i=0;i<answers.length;i++){
answers[i].correct = reply[i].correct;
}
|
jQuery Update Hidden Field Based on Whether or Not Another Field's Value is In Array
By : Waqar Khan
Date : March 29 2020, 07:55 AM
this one helps. Your code does appear to be working properly. Here is a working example: JSFIDDLEI used this basic markup for the test: code :
<label>Zipcode:</label><input id="zip" type="text" /><br />
<label>Result</label><input id="campaign" type="text" />
jQuery(function(){
});
<script>
jQuery(function(){
jQuery('#zip').blur(function () {
var brzips = ['70815', '70895', '70835'];
var shrzips = ['71109', '71148', '71152'];
var dalzips = ['75172', '75241', '75134'];
var litzips = ['72209', '72219', '72214'];
if (jQuery.inArray(jQuery('#zip').val(), brzips) != -1) {
jQuery("#campaign").val("CAMPAIGN ONE");
} else if (jQuery.inArray(jQuery('#zip').val(), shrzips) != -1) {
jQuery("#campaign").val("CAMPAIGN TWO");
} else if (jQuery.inArray(jQuery('#zip').val(), dalzips) != -1) {
jQuery("#campaign").val("CAMPAIGN THREE");
} else if (jQuery.inArray(jQuery('#zip').val(), litzips) != -1) {
jQuery("#campaign").val("CAMPAIGN FOUR");
} else {
jQuery("#campaign").val("OUT OF AREA");
}
});
});
</script>
|
Using $addToSet to update an array field using another array field
By : user3600303
Date : March 29 2020, 07:55 AM
I wish this help you db.projects.find actually returns a cursor, which you definitely don't want to add to your set. Since you know ahead of time that you will be only finding one value, you can get the properties out of the cursor specifically by using .next().admin -- but remember that this will only work with the first value returned from .find. Otherwise, I think you will have to use a loop. $addToSet will also add the array as a whole, so instead you have to append multiple values using $each code :
db.runCommand({
findAndModify: 'sites',
query: {'title': 'Company Three Site'},
update: {
$addToSet: {
"admins": {
$each: db.projects.find(
{"title": "ABC"},
{"_id": 0, "admins": 1}
).next().admins
}
}
}
})
|
MongoDB - Update field in an array object based on nested array's field value
By : user3611529
Date : March 29 2020, 07:55 AM
I hope this helps . I am trying to update a field inside array of objects, where field in nested array is equal to a value. , Please try this : code :
db.Product.findOneAndUpdate(
{ _id: 123 },
{
$set: {
'variations.$[item].valueList.$[nameField].value': 'newRed',
'variations.$[item].picture': 'newURL' // item is each object in variations which is being checked in arrayFilters.
}
},
{
arrayFilters: [{ 'item.valueList.value': 'oldRed' }, { 'nameField.value': 'oldRed' }],
new: true
}
)
{
"_id" : 123,
"variations" : [
{
"id" : 1,
"picture" : "https://example.picture.com",
"valueList" : [
{
"name" : "color",
"value" : "oldRed"
},
{
"name" : "size",
"value" : "M"
},
{
"name" : "color",
"value" : "oldRed"
}
]
},
{
"id" : 2,
"picture" : "https://example.picture.com",
"valueList" : [
{
"name" : "color",
"value" : "black"
},
{
"name" : "size",
"value" : "M"
}
]
},
{
"id" : 3,
"picture" : "https://example3.picture.com",
"valueList" : [
{
"name" : "color",
"value" : "oldRed"
},
{
"name" : "size",
"value" : "M"
}
]
}
]
}
/* 1 */
{
"_id" : 123,
"variations" : [
{
"id" : 1,
"picture" : "newURL",
"valueList" : [
{
"name" : "color",
"value" : "newRed"
},
{
"name" : "size",
"value" : "M"
},
{
"name" : "color",
"value" : "newRed"
}
]
},
{
"id" : 2,
"picture" : "https://example.picture.com",
"valueList" : [
{
"name" : "color",
"value" : "black"
},
{
"name" : "size",
"value" : "M"
}
]
},
{
"id" : 3,
"picture" : "newURL",
"valueList" : [
{
"name" : "color",
"value" : "newRed"
},
{
"name" : "size",
"value" : "M"
}
]
}
]
}
|
MongoDB: How to update field of array element depending on other field?
By : Midas74
Date : March 29 2020, 07:55 AM
With these it helps I am trying to update data in my MongoDB (4.2) by using update with a pipeline. The difficulty is that I want to add a field to an array element depending on another field. Also I want to do this for each element. I know I could do by Javascript for each but I was wondering if there is a better way. So this is what I want to do as an example , This will do the required update. code :
db.test.update(
{ _id : ObjectId("555555555") },
[
{
$set: {
messages: {
$map: {
input: "$messages",
in: {
$mergeObjects: [
"$$this",
{ "author": {
$mergeObjects: [
"$$this.author",
{ newId: { $concat: [ "000", "$$this.author.userId"] } }
]
} }
]
}
}
}
}
}
],
{ multi: true, writeConcern: {w: 0} }
)
|