【问题标题】:how to update embed document in mongoDB with mongoose如何使用 mongoose 更新 mongoDB 中的嵌入文档
【发布时间】:2021-09-09 15:15:06
【问题描述】:

我有类似的课程模型

const courseSchema = new mongoose.Schema({
    name:{
        type: String,
        required:[true,'course must have a name.'],
        unique:true
    },
    duration :Number,
    description :String,
    imageCover :String,
    images:Array,
    price :Number,
    curriculum:
        [
            {
                week:Number,
                description:String,
                total:Number,
                completed:Number,
                progress:Number,
                links: 
                    [
                        {
                            name:String,
                            link:String,
                            img:String,
                            watched:{
                                type:Boolean,
                                default:false
                            }
                            
                        }
                    ]
            }       
            
        ],
    tutors:[
        {
            type: mongoose.Schema.ObjectId,
            ref:'user'
        }
    ]
},
{
    toJSON:{virtuals : true},
    toObject:{virtuals : true}
});

我想向课程中的链接数组添加新对象。客户端将使用星期对象 ID、名称和链接修补请求,在更新处理程序中我正在做这样的事情。

 const cour = await Course.findOneAndUpdate({"caricullum._id":req.params.w},
           {$push:{name:req.body.name,link:req.body.link}},{
            new : true,
        });

w 参数包含课程周 _id

{
    "_id": {
        "$oid": "6138abc106b3ad1d3477b3e2"
    },
    "images": [],
    "tutors": [],
    "name": "c/c++",
    "duration": 8,
    "price": 1299,
    "imageCover": "cool_lion.jpg",
    "description": "",
    "curriculum": [
        {
            "_id": {
                "$oid": "6138abc106b3ad1d3477b3e3"
            },
            "week": 1,
            "description": "introduction to microcontroller and microprocesor",
            "links": [
                {
                    "watched": false,
                    "_id": {
                        "$oid": "6138abc106b3ad1d3477b3e4"
                    },
                    "name": "introduction",
                    "link": "https://www.youtube.com/embed/d0e6ScoS3Sw"
                },
                {
                    "watched": false,
                    "_id": {
                        "$oid": "6138abc106b3ad1d3477b3e5"
                    },
                    "name": "difference between mc and mp",
                    "link": "https://www.youtube.com/embed/dcNk0urQsQM"
                },
                {
                    "watched": false,
                    "_id": {
                        "$oid": "6138abc106b3ad1d3477b3e6"
                    },
                    "name": "building with microcontroller vs boards(arduino uno)",
                    "link": "https://www.youtube.com/embed/IdEcm3GU7TM"
                }
            ]
        },
        {
            "_id": {
                "$oid": "6138abc106b3ad1d3477b3e7"
            },
            "week": 2,
            "description": "introduction to arduino uno",
            "links": [
                {
                    "watched": false,
                    "_id": {
                        "$oid": "6138abc106b3ad1d3477b3e8"
                    },
                    "name": "introduction to arduino uno",
                    "link": "https://www.youtube.com/embed/BiCSW6QR6HA"
                },
                {
                    "watched": false,
                    "_id": {
                        "$oid": "6138abc106b3ad1d3477b3e9"
                    },
                    "name": "IO PINS",
                    "link": "https://www.youtube.com/embed/OZGMLOwHYf8"
                },
                {
                    "watched": false,
                    "_id": {
                        "$oid": "6138abc106b3ad1d3477b3ea"
                    },
                    "name": "setting up arduno uno for programming",
                    "link": "https://www.youtube.com/embed/ELUF8m24sZo"
                }
            ]
        },
        {
            "_id": {
                "$oid": "6138abc106b3ad1d3477b3eb"
            },
            "week": 3,
            "description": "interfacing with different sensors",
            "links": [
                {
                    "watched": false,
                    "_id": {
                        "$oid": "6138abc106b3ad1d3477b3ec"
                    },
                    "name": "LED Blinking(OUTPUT)",
                    "link": "https://www.youtube.com/embed/dnPPoetX0uw"
                },
                {
                    "watched": false,
                    "_id": {
                        "$oid": "6138abc106b3ad1d3477b3ed"
                    },
                    "name": "interfacing with button(INPUT)",
                    "link": "https://www.youtube.com/embed/58Ynhqmvzoc"
                },
                {
                    "watched": false,
                    "_id": {
                        "$oid": "6138abc106b3ad1d3477b3ee"
                    },
                    "name": "16x2 LCD",
                    "link": "https://www.youtube.com/embed/Mr9FQKcrGpA"
                }
            ]
        }
    ],
    "__v": 0
}

如何使用 req.params.w 查询正确的星期文档并将新文档推送到链接数组中

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    使用arrayFilters

    db.collection.update({},
    {
      $push: {
        "curriculum.$[elem].links": {
          link: "a",
          name: "b",
          whatched: "c"
        }
      }
    },
    { new:true,
      arrayFilters: [
        {
          "elem.week": 1
        }
      ]
    })
    

    https://mongoplayground.net/p/nLV9UzbJlsc

    【讨论】:

      猜你喜欢
      • 2011-12-04
      • 2017-01-06
      • 2017-06-17
      • 2014-06-12
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      相关资源
      最近更新 更多