javascript - How to add a new key:value pair to existing sub object of a mongoDB document - Stack Overflow

I am trying to add a new key:value pair to an existing object of mongoDB document, but no steps are hel

I am trying to add a new key:value pair to an existing object of mongoDB document, but no steps are helping me

I tried $each, $push $addtoset but i understood those are for arrays then i tried $det but it is updating the existing key:value pair with new key:value pair

Here is my document

    {
    test:"abc",
    test2:"cdf",
    test3:{ 1:"one"}
    }

if you observer test3 key in the above document i have already 1:"one" now i want to add new key value in the same object

Like

    {
    test:"abc",
    test2:"cdf",
    test3:{ 1:"one", 2:"two", 3:"three"}
    }

is it possible in mongoDB?

Here is the mongo Query

            let val = parseInt(DYNAMICVALUE)
              var changfeemaildot = (req.session.email).replace(/\./g, '#')
              var seld = {
                _id: ObjectId(rx[0]._id)
              };
              var seldu = {

                $set:{
                emails: {
                   [changfeemaildot]: val
                }
              }
            };

              var collection = 
              connection.get().collection('problems');
              collection.update(seld, seldu, function (err, rail) {
              });

I am trying to add a new key:value pair to an existing object of mongoDB document, but no steps are helping me

I tried $each, $push $addtoset but i understood those are for arrays then i tried $det but it is updating the existing key:value pair with new key:value pair

Here is my document

    {
    test:"abc",
    test2:"cdf",
    test3:{ 1:"one"}
    }

if you observer test3 key in the above document i have already 1:"one" now i want to add new key value in the same object

Like

    {
    test:"abc",
    test2:"cdf",
    test3:{ 1:"one", 2:"two", 3:"three"}
    }

is it possible in mongoDB?

Here is the mongo Query

            let val = parseInt(DYNAMICVALUE)
              var changfeemaildot = (req.session.email).replace(/\./g, '#')
              var seld = {
                _id: ObjectId(rx[0]._id)
              };
              var seldu = {

                $set:{
                emails: {
                   [changfeemaildot]: val
                }
              }
            };

              var collection = 
              connection.get().collection('problems');
              collection.update(seld, seldu, function (err, rail) {
              });
Share Improve this question edited Dec 24, 2018 at 21:43 asked Dec 24, 2018 at 20:56 user4743671user4743671
Add a ment  | 

2 Answers 2

Reset to default 6

You can use $set. So your code can be something like this

db.collection.update({<your_condition>}, {$set: {"test3.2": "two", "test3.3": "three"}});

In your case, it will be something like this

var seldu = {$set: {["emails." + changfeemaildot]: val}}

You can use $set with findOneAndUpdate. So your code can be something like this

  const { Types, connection } = require("mongoose");

  const productList = await connection.collection('products').find({}).toArray()
    productList.forEach(async function(myDoc) {
        await connection.collection('products').
        updateOne({ productId: Types.ObjectId(myDoc.productId) }, {
            $set: {
                productDisplayId: 'anything you want'
            }
        });
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744261303a4565647.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信