【问题标题】:How to update an array of object in MongoDB using the object key or index value..?如何使用对象键或索引值更新 MongoDB 中的对象数组..?
【发布时间】:2023-02-11 01:40:48
【问题描述】:

如何使用对象键或索引值更新 MongoDB 中的对象数据数组

const taskComplete = (user, pswd, taskId) => {
return db.Todotasks.findOne({ username: user }).then((result) => {
        if (result.password === pswd) {
            db.Todotasks.find({ taskList: taskId }).then((result) => {
                console.log(result.taskId)

            })
 return {
                status: true,
                message: ('successfully Complete tasks'),
                statusCode: 200,
            }
        } else {
            return {
                status: false,
                message: ('Operation Denied'),
                statusCode: 404
            }
        }
    })
}

console.log 未定义

【问题讨论】:

  • 1.不要在截图中提供示例文档。相反,以纯文本形式发布。 2.提供预期的输出

标签: node.js mongodb express mongoose


【解决方案1】:

MongoDB 将数据存储在 BSON 中。

根据specification

Array - 数组的文档是一个普通的 BSON 文档,其键为整数值,从 0 开始并按顺序继续。例如,数组 ['red', 'blue'] 将被编码为文档 {'0': 'red', '1': 'blue'}。键必须按数字升序排列。

这意味着您可以使用点分符号来引用数组中的文档,例如:

{$set: {"tasklist.2.title": "new value"}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    相关资源
    最近更新 更多