【问题标题】:How to add data in nested array in mongoshell如何在 mongo shell 的嵌套数组中添加数据
【发布时间】:2016-06-21 03:39:50
【问题描述】:

我有一个架构:

{
    "id": String,
    "username": String,
    "password": String,
    "email": String,
    "firstName": String,
    "lastName": String,

    "system" : {
            "item" : {type: Number},
            "update_interval" :  { type: Number, max: 99999 },
            "reading" : [
                {
                    "id" :              { type: Number},
                    "adc1"  :           { type: Number, max: 4095 },
                    "adc2"  :           { type: Number, max: 4095 },
                    "pc_datestamp" :Date,
                }
            ]
    }

现在我想向

添加值
"reading" : [
                    {
                        "id" :              { type: Number},
                        "adc1"  :           { type: Number, max: 4095 },
                        "adc2"  :           { type: Number, max: 4095 },
                        "pc_datestamp" :Date,
                    }
                ]

但我不知道我错在哪里我试图从 mongoshell 更新数据但到目前为止没有成功

> db.users.update( {"email" : "test@test.com", "system.item": 1,   }, {"$push": {"system.$.reading": [{"adc1" : "123", "adc2": "1245", "id":"1" }] } })
WriteResult({
    "nMatched" : 0,
    "nUpserted" : 0,
    "nModified" : 0,
    "writeError" : {
        "code" : 16837,
        "errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: system.$.reading"
    }

> db.users.update( {"email" : "test@test.com", "system": {$elemMatch:{ item: 1}}   }, {"$push": {"system.$.reading": {"adc1" : "123", "adc2": "1245", "id":"1" } } })
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

我已将 item 的值设置为 1

> db.users.find( {"email" : "test@test.com", "system.item": 1}   ).pretty()
{
    "_id" : ObjectId("56dd88578ff7fbd714091a4a"),
    "lastName" : "test",
    "firstName" : "test",
    "email" : "test@test.com",
    "password" : "$2a$10$wY9wr9oreza4fBX7CfXym.rPZUPrcesigYIfWd0zbM4dDjBy6k3vy",
    "username" : "test",
    "system" : {
        "item" : 1,
        "reading" : [ ]
    },
    "__v" : 0
}

我关注了

Mongodb $push in nested array

还有这个 Insert data in nested array in mongodb

还有更多问题,但找不到问题所在。

【问题讨论】:

    标签: node.js mongodb mongoose database nosql


    【解决方案1】:

    由于位置 $ 运算符充当与查询文档匹配的第一个元素的占位符,并且 array 字段必须作为查询文档的一部分出现,因此您的更新 操作不满足这些条件,因此它会遭受您遇到的错误命运。在您的查询中,您仅引用不是数组的 "system.item" 字段。

    您可以做的另一个选择是在更新中放弃位置 $ 运算符,只需使用 $addToset 将对象添加到数组仅当它们不存在于集合中时才为数组:

    db.users.update(
        {
            "email": "test@test.com", 
            "system.item": 1
        }, 
        {
            "$addToSet": {
                "system.reading": {
                    "adc1" : "123", 
                    "adc2": "1245", 
                    "id":"1" 
                }
            } 
        }
    )
    

    【讨论】:

    • 谢谢@chridam,这非常有帮助。我还有一个疑问,当我不断添加值时,如何增加“id”的值
    • id 是一个字符串,你不能使用 $inc 更新操作符来增加它,因为它只作用于数值。
    • 我不明白 "id" : { type: Number},这里我定义为 number ,还是类似于 type: [number],有什么办法可以增加 "id"
    猜你喜欢
    • 2022-01-20
    • 2021-07-20
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多