mongoose更新嵌套文档无法正常工作

时间:2017-06-01 09:09:30

标签: javascript node.js mongodb mongoose

我试图弄清楚我做错了什么,moongose没有更新我的文件。仅用于测试id是硬编码的。

的node.js

var id1 = "592fd471fedd311d5c76a024";
var id2 = "592fd4ad608e001d79938ba8";

Workshop.update({
        _id: id1,
        'themen._id': id2
    }, {
        '$set': {
            'themen.$.risikolandschaft': ["John", "Doe"],
        }
    },
    function(err, body) {
        if (err) {
            console.log(err)
        } else {
            console.log(body);
        }
    }
);

这里我的数据是从MongoDB Compass复制的

{
"_id" : ObjectId("592fd471fedd311d5c76a024"),
"clientid" : "592cff8794738f0347609666",
"bezeichnung" : "Workshop 1.5.2017 / 10:46",
"themen" : [ 
    {
        "thema" : {
            "__v" : 0,
            "bezeichnung" : "Sturm",
            "beschreibung" : "Text",
            "_id" : "59255757b1485d0ad2a6924f"
        },
        "risikolandschaft" : [ "one", "two", "three"],
        "date" : "2017-06-01T08:47:41.944Z",
        "_id" : ObjectId("592fd4ad608e001d79938ba8")
    }
], ...

来自正文的日志

{ n: 0, nModified: 0, ok: 1 }

用于向“themen”子文档添加新项目我使用:

var new_thema = {
        _id: mongoose.Types.ObjectId(),
        date : req.body.date,
        risikolandschaft : req.body.risikolandschaft,
        thema : req.body.thema };

这里是mongoose架构

var WorkshopSchema = mongoose.Schema({
    clientid: {
        type: String,
        required: true
    },
    bezeichnung: {
        type: String,
        required: true
    },
    stammdaten : [],
    date : Date,
    themen : []

});

并且“themen”没有子模式 “mongoose”:“^ 4.8.6”

0 个答案:

没有答案
相关问题