由_id查找和更新的Mongoose

时间:2017-06-15 13:10:26

标签: javascript node.js mongodb mongoose mongoose-schema

我试图通过使用两个参数来为我的架构的“data_stream_map”数组插入一个名称,

如下,

var query = {
        '_id': new ObjectId("594261a0ea2d89001c851424"),
        'inputs.name':  "name1"
    };

    return WFWorkflowModel.findOneAndUpdate(query, {$addToSet: {'inputs.$.data_stream_map': "121_name1"}}).then(function (result) {
        return true;
    }, function (error) {
        console.error("WFEditor micro service - update dataStream.");
        return error;
    });

互联网上没有任何东西可以和我合作。但是当谈到Robomongo 0.9.0时,这是有效的,

db.getCollection('wfcomponents').findOneAndUpdate({
        _id:  ObjectId("594261a0ea2d89001c851424"),
        'inputs.name':  "name1"
    }, {$addToSet: {'inputs.$.data_stream_map': "120_name1"}})

猫鼬文件如下,

{
    "_id" : ObjectId("594261a0ea2d89001c851424"),
    "key" : "task",
    "description" : "",
    "__v" : 0,
    "updated" : ISODate("2017-06-12T07:08:58.462Z"),
    "created" : ISODate("2017-06-12T07:08:44.079Z"),
    "gridLocation" : {
        "y" : 1,
        "x" : 7
    },
    "inputs" : [ 
        {
            "name" : "name1",
            "data_stream_map" : [ 

            ]
        }
    ]
}

用过的猫鼬版本是“猫鼬”:“^ 4.6.5”,我没有线索,有没有人可以帮我解决这个问题?我审查了很多堆栈溢出问题,但仍然没有解决。

2 个答案:

答案 0 :(得分:0)

1。)将Robomongo更新为1.0,它在系统中有必要的更新。 2.)查看我提供的这个链接,不确定你的问题是否重复,但这有很好的答案,我认为与你的问题有关,并且快速提示你不能更新mongodb中的id,你需要删除并重新创建(解释)在提供的链接)。希望我能正确地阅读这个问题,祝你好运。

P.S。如果您只是尝试查询输入,请确保您的文件正确链接,即您正确地要求所有模块。如果你有那套mongoose使用find,findOne,findById,where。提供链接。再次希望我能正确回答这个问题,不确定你想要实现的目标。

How update the _id of one MongoDB Document?

http://mongoosejs.com/docs/models.html

http://mongoosejs.com/docs/queries.html

答案 1 :(得分:0)

Model.findOneAndUpdate(query,update,{new:true}).exec(function(err,docs){
        console.log(docs)
})
//your code is correct , just lost the options{new:true},which will return the updated doc but not the found doc
相关问题