Mongodb“无法在元素中创建字段'x'”

Joh*_*ohn 8 mongodb

我正在尝试将交易插入到交易列表中。

这是文件:

{
    "_id" : ObjectId("5cb26787c3eb6c8229a92954"),
    "first_name" : "testing",
    "last_name" : "testing",
    "ssn" : "123456789",
    "address" : "123 testing st",
    "account" : {
        "account_number" : 32362897,
        "last_access_timestamp" : "2019-04-13 18:49:43",
        "account_type" : [
            {
                "checking" : {
                    "balance" : 0,
                    "transaction" : [ ]
                }
            },
            {
                "saving" : {
                    "balance" : 0,
                    "transaction" : [ ]
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

ssn 应该是唯一的,所以我将其用作匹配标准。这是我用来插入检查交易列表的查询:

db.customer.update({'ssn': '123456789'},
                    {'$push': {'account.account_type.checking.transaction': {
                         'amount': 120,
                         'transaction_type': 'deposit',
                         'timestamp': '11:11:11'
                     }}})
Run Code Online (Sandbox Code Playgroud)

预期的结果是:

{
    "_id" : ObjectId("5cb26787c3eb6c8229a92954"),
    "first_name" : "testing",
    "last_name" : "testing",
    "ssn" : "123456789",
    "address" : "123 testing st",
    "account" : {
        "account_number" : 32362897,
        "last_access_timestamp" : "2019-04-13 18:49:43",
        "account_type" : [
            {
                "checking" : {
                    "balance" : 0,
                    "transaction" : [{
                         "amount": 120,
                         "transaction_type": "deposit",
                         "timestamp": "11:11:11"
                     } ]
                }
            },
            {
                "saving" : {
                    "balance" : 0,
                    "transaction" : [ ]
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误消息:

WriteResult({
    "nMatched" : 0,
    "nUpserted" : 0,
    "nModified" : 0,
    "writeError" : {
        "code" : 28,
        "errmsg" : "Cannot create field 'checking' in element {account_type: [ { checking: { balance: 0, transaction: [] } }, { saving: { balance: 0, transaction: [] } } ]}"
    }
})
Run Code Online (Sandbox Code Playgroud)