logo
down
shadow

how can i update field in array?


how can i update field in array?

By : dgreenday
Date : January 11 2021, 03:34 PM
seems to work fine Since the object you are attempting to change a value on is an array, you need to specify an index. In your sample document, there are 2 objects in the array, so your index is either 0 or 1.
If you wanted to update the first item in the array, your query would look like this: $set: { "courseDetail.contentDetail.0.isDelete" : false }
code :


Share : facebook icon twitter icon
How can I update one field of an array when given another array containing the state of the field to update?

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

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: JSFIDDLE
I 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

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

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?

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} }
)
Related Posts Related Posts :
  • Cannot import @tensorflow/tfjs-node in nodejs
  • firebase cloud functions doesn't execute the whole function?
  • How to implement like query in mongodb
  • Firebase deep increment of a field in a data base
  • How to count total time from start date and end date in mongoose?
  • Invoke function only after middleware is done with its task
  • How to send an object to .marko template and render its properties dynamically, in a table
  • Mongoose check if user owns document before updating
  • Another Web process failed to bind to $PORT within 60 seconds of launch
  • trouble with parsing in JSON
  • In express, how do I route to a custom error template when a condition is met?
  • Express middleware ignores next(new Error()) and continues execution of the code
  • Is it a good practice to start http server on one port for Cluster forks for 1-3k requests per second?
  • Why am I getting the error "Identifier ... has already been declared"?
  • Is it a bad idea to use async/await in a Node/Express server?
  • The json data i am putting into a pug template is putting '<' at the beginning of a string and '>' at the end. why
  • Finish entire loop / forEach before next command
  • mongodb query to exclude only from text index
  • Port forwarding not working for Nodejs application
  • Dialogflow: throw new Error(`@grpc/grpc-js only works on Node ${supportedNodeVersions}`)
  • In new React version, how to fix TypeError: styled_components__WEBPACK_IMPORTED_MODULE_3__.default.dev?
  • How to add delay before/during test (using supertest & jest) while waiting for dynamodb event to occur
  • change JSON.stringify to what?
  • CosmosDB MongoDB adapter projection not working
  • Express router calls the wrong route
  • How to join table that contains no data yet with sqlite
  • How to filter by string parameter , web3 2.0.0-alpha.1 Solidity events?
  • How to find document.activeElement in Puppeteer
  • Postgresql JSONB | nested arrays gives ERROR aggregate function calls cannot be nested
  • 404 error when deploying angular sample app in production mode
  • Setting up environment variable in nodeJs
  • How to get the view definition in SQL server using NodeJS through code
  • how to give user specific file access in express js
  • passwordHash field in admin.auth.UserRecord interface of Firebase Admin SDK
  • Mongoose get updated document after instance.update()
  • how to extract JWT when authorize in Nestjs
  • Will buying a non sleep dyno on Heroku allow me to upload files without loosing them?
  • Why Pug isn't showing any message when I do res.redirect after req.flash?
  • insert node_modules assets into my project
  • Mongoose - Why might model.find() trigger JSON TypeError in basic server?
  • where is the GET/POST syntax in this code?
  • How to fix 'Failed at the est-ggjr@0.1.0 start script'-error
  • Nodejs server on my PC not receiving POST request from AWS instance
  • Errors with running code that works on another computer
  • How to correctly send HTTPS request to Server using auto-generated SSL keys?
  • how do I maintain time offsets in moment?
  • add a value for the status parameter?
  • Calling C function from returned pointer in NodeJS
  • Using Node.js on StackBlitz?
  • Export multiple modules from NPM package
  • Where and when to use Firebase admin.app().delete()?
  • Why node.js always says I'm connected to my MongoDB even if the "URI" is fake or wrong?
  • Postgresql jsonb -> invalid reference to FROM-clause entry for table "mt"
  • How can I send Apollo Server errors to Sentry?
  • Design pattern to solve event mess in Electron apps with ipcMain and ipcRenderer
  • What im doing wrong?
  • Why do I get a UserCodeSyntaxError when I have no syntax error in my code?
  • I am getting the following error while creating the ember application through cmd
  • TSLoader + Babel Polyfill : You may need an additional loader to handle the result of these loaders while running npm ru
  • i can't receive any answer from Watson Discovery
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org