Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在mongodb中已经存在的嵌套文档中插入字段?_Mongodb_Mongodb Query - Fatal编程技术网

如何在mongodb中已经存在的嵌套文档中插入字段?

如何在mongodb中已经存在的嵌套文档中插入字段?,mongodb,mongodb-query,Mongodb,Mongodb Query,我有以下文档结构 { "_id" : ObjectId("585a985f67962d91c0f3a1f6"), "Email" : "abc@gmail.com", "PhoneNumber" : "9999999999", "TwoFactorEnabled" : false, "LockoutEndDateUtc" : null, "LockoutEnabled" : true, "AccessFailedCount"

我有以下文档结构

{ 
    "_id" : ObjectId("585a985f67962d91c0f3a1f6"), 
    "Email" : "abc@gmail.com", 
    "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
    }
}

我想在所有此类文档的“Wallet”字段中添加一个名为“CustomerCredit”的字段,默认值为0。如何做到这一点?

尝试以下方法:

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

请参见

尝试以下方法:

db.collection.aggregate([
    {
        $addFields{
             "Wallet.CustomerCredit": 0
        }
    }
])
请参阅python shell中的

db.collection.update_many({"$set": {"Wallet.CustomerCredit": 0}})
您可以使用update\u many,因为update现在已不推荐使用。 来自mongoshell

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

db.collection.update_many({"$set": {"Wallet.CustomerCredit": 0}})
您可以使用update\u many,因为update现在已不推荐使用。 来自mongoshell

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