1

I have the following document structure.

{ 
    "_id" : ObjectId("585a985f67962d91c0f3a1f6"), 
    "Email" : "[email protected]", 
    "PhoneNumber" : "9999999999", 
    "TwoFactorEnabled" : false, 
    "LockoutEndDateUtc" : null, 
    "LockoutEnabled" : true, 
    "AccessFailedCount" : NumberInt(0), 

    "DisplayName" : "Nilesh Guria", 
    "CreatedOn" : ISODate("2016-12-21T14:57:35.379+0000"), 
    "UpdatedOn" : ISODate("2018-09-17T21:32:16.027+0000"),  
    "Wallet" : {
        "Balance" : 1000,
        "UnbilledUsage": 100
    }
}

I want to add a field called "CustomerCredit" with a default value of 0 inside the "Wallet" field in all such documents. How to achieve this ?

3 Answers 3

2
db.collection("collectionName")
  .updateMany({ "Wallet.CustomerCredit": { $exists: false }},
          { $set:{ "Wallet.CustomerCredit": 0 }});
Sign up to request clarification or add additional context in comments.

Comments

1

From python shell.

db.collection.update_many({"$set": {"Wallet.CustomerCredit": 0}})

You can use update_many since update is now deprecated. From mongoshell

db.collection.updateMany({},{$set: {"Wallet.CustomerCredit": 0}})

Comments

1

Try something like this:

db.collection.aggregate([
    {
        $addFields{
             "Wallet.CustomerCredit": 0
        }
    }
])

See the docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.