node.js - 如何在mongo shell中的嵌套数组中添加数据

标签 node.js mongodb mongoose database nosql

我有一个架构:

{
    "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

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

最佳答案

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

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

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

关于node.js - 如何在mongo shell中的嵌套数组中添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35846271/

相关文章:

c# - MongoDb Upsert死锁

mongodb - MongoDB 中的子查询

node.js - 在 MongooseJS 对象数组中查找特定属性

node.js - 防止快速中间件针对同一父路径执行

node.js - Windows Azure 移动服务调度程序执行超时

node.js - 每个用户在哪里存储 SHA256 key 对

node.js - 当我们在nodejs中使用mongoose从mongodb中选择复杂对象时,有什么方法可以重命名路径吗?

node.js - 发出 socket.io 和 sailsjs (Twitter API)

MongoDB Go 驱动程序在不应该查看本地主机的情况下

node.js - TypeError : this. add 不是 Node js应用程序中的函数