简体   繁体   English

如何在 mongoDB 中添加、更新、删除嵌套对象

[英]How can i add, update, delete nested objects in mongoDB

I'm new to mongoDB and backend stuffs.我是 mongoDB 和后端的新手。 I've a nested MongoDB model schema.我有一个嵌套的 MongoDB 模型架构。 I need to update, delete and add in that nested objects/array.我需要更新、删除和添加嵌套对象/数组。 How can I do that ?我怎样才能做到这一点 ?

How can I delete/add/update item from columns array and resume array.如何从列数组和恢复数组中删除/添加/更新项目。

const userSchema = new Schema({
    name: {
        type: String,
        required: true,
    },
    email: {
        type: String,
        required: true,
    },
    password: {
        type: String,
    },
    columns: [
        {
            title: {
                type: String,
                required: true,
            },
            resumes: [
                {
                    name: {
                        type: String,
                        required: true,
                    },
                    resume: {
                        type: String,
                        required: true,
                    },
                },
            ],
        },
    ],
});

const Users = mongoose.model('Users', userSchema);

This is my schema这是我的架构

To add new item to the columns array:将新项目添加到列数组:

let newData = {title: "New Title", resumes: []}    
let userData = await User.findByIdAndUpdate(userID, {
  $push: {
    columns: newData,
  },
});

To add new item to the resumes array inside columns:要将新项目添加到列内的 resumes 数组:

let newData = {name: "New Name", resume: "New Resume"}
let userData = await User.updateOne(
  {
    _id: mongoose.Types.ObjectId("60f54774761788cd80f56xxx"),
    "columns._id": mongoose.Types.ObjectId("60f54774761788cd80f56xxx"),
  },
  {
    $push: { "columns.$.resumes": newData },
  }
);

Similarly, you can use $pull operator to remove an object from the array, and $(update) to update the element inside the array同样,您可以使用$pull运算符从数组中删除一个对象,并使用$(update)更新数组内的元素

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ZCCADCDEDB567ABAE643E15DCF0974E503Z 更新 MongoDb 中的嵌套对象 - How to update nested objects in MongoDb with Mongoose 如何将对象推送到 mongoDB 和 expressjs 中的嵌套对象数组 - How can I push an object to a nested array of objects in mongoDB and expressjs 更新 MongoDB 中的嵌套数组对象 - Update nested array objects in MongoDB 如何使用 node.js 删除 MongoDB 中嵌套对象数组中的 object - How to delete an object in nested array of objects in MongoDB with node js 请问如何更新嵌套的 mongodb 嵌入文档,我有这样的文档结构 - Please how can i update a nested mongodb embedded document, I have a document structure like this 使用节点更新 mongodb 中的嵌套对象数组 - Update nested array of objects in mongodb with node 如何检查存在并更新 mongodb/mongoose 中嵌套数组中的对象? - How can I check existance and update an object in nested array in mongodb/mongoose? 如何在 mongodb 中涉及数组或其他类型的嵌入或嵌套文档的更新操作中使用变量? - How can I use variables in an update operation in mongodb that involves an array or another type of embedded or nested document? 如何复制整个 JavaScript object 以更新包含嵌套 arrays 和子文档的 MongoDB/Mongoose 文档? - How can I copy an entire JavaScript object to update a MongoDB/Mongoose document, that contains nested arrays and subdocuments? 如何在 MongoDB 中将对象与其值合并 - How can i merge objects with their values in MongoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM