【问题标题】:mongojs update nested array element in mongodbmongojs 更新 mongodb 中的嵌套数组元素
【发布时间】:2017-12-09 14:35:10
【问题描述】:

我正在尝试更新文档的嵌套字段,但不知道为什么它没有成功。请帮忙。

我的文档结构是这样的:

{
    "_id": {
        "$oid": "594d173df36d285a33da90cf"
    },    
    "available": 1,
    "brand": [
        {
            "_id": {
                "$oid": "59db10918bbe2316281d58d1"
            },
            "name": "test",            
            "createdby": "***************",
            "createdon": "Mon Oct 09 2017 11:30:42 GMT+0530 (India Standard Time)",
            "feedbacks": [
                {
                    "_id": {
                        "$oid": "5a1039c25443604328ecf398"
                    },                    
                    "feedback": "hello",
                    "date": "Sat Nov 18 2017 19:16:36 GMT+0530 (India Standard Time)",
                    "reportedby": []
                },
                {
                    "_id": {
                        "$oid": "5a1039c25498504328ecf398"
                    },                    
                    "feedback": "hello",
                    "date": "Sat Nov 18 2017 19:16:36 GMT+0530 (India Standard Time)",
                    "reportedby": []
                }
            ]
        }
    ]
}

我的查询是这样的:

db.products.update({
                    '_id': mongojs.ObjectId(594d173df36d285a33da90cf),
                    'brand._id': mongojs.ObjectId(59db10918bbe2316281d58d1),
                    'brand.feedbacks._id': mongojs.ObjectId(5a1039c25498504328ecf398),
                }, {
                    $addToSet: { 'brand.feedbacks.reportedby': skillSet.userId }
                },
                function(err, products) {

                })

错误是这样的: { n: 0, n修改:0, 操作时间: { ts: Timestamp { bsontype: 'Timestamp', low: 1, high_: 1512829477 }, 吨:5}, 选举ID:7fffffff0000000000000005, 好的:1 }

【问题讨论】:

标签: node.js mongodb mongojs


【解决方案1】:

你可以试试这个查询:

{brand:{$addToSet:{'feedbacks.reportedly':'skillset.userId}}}

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 2020-03-16
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 2022-01-22
    • 2017-01-05
    • 2018-10-06
    • 2022-06-11
    相关资源
    最近更新 更多