简体   繁体   English

由_id查找和更新的Mongoose

[英]Mongoose Find and Update by _id

I was trying to insert a name to my schema's "data_stream_map" array by finding using two parameters, 我试图通过使用两个参数来为我的架构的“data_stream_map”数组插入一个名称,

As follows, 如下,

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;
    });

Nothing in the internet worked with me yet. 互联网上没有任何东西能和我合作。 But when it comes to Robomongo 0.9.0 this works, 但是当涉及到Robomongo 0.9.0时,这是有效的,

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

The mongoose document as follows, 猫鼬文件如下,

{
    "_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" : [ 

            ]
        }
    ]
}

The used mongoose version is "mongoose": "^4.6.5", I'm out of clues, is there anyone can help me to overcome this issue? 使用的猫鼬版本是“mongoose”:“^ 4.6.5”,我没有线索,有没有人可以帮我解决这个问题? I refereed lots of stack overflow questions but its still unresolved. 我审查了很多堆栈溢出问题,但仍然没有解决。

1.) update Robomongo to 1.0, its has updates in the system that are necessary. 1.)将Robomongo更新为1.0,它在系统中有必要的更新。 2.) check out this link i provided, Not sure if your question is a duplicate but this has good answers and i think pertains to your problem, and quick tip you cant update ids in mongodb, you need to delete and create again(explained in the link provided). 2.)查看我提供的这个链接,不确定你的问题是否重复,但这有很好的答案,我认为与你的问题有关,并且快速提示你不能更新mongodb中的id,你需要删除并重新创建(解释)在提供的链接)。 Hope i read the question right, best of luck. 希望我能正确地阅读这个问题,祝你好运。

PS If your just trying to query input, make sure that your files are properly linked ie you are requiring all of your modules properly. PS如果您只是尝试查询输入,请确保您的文件正确链接,即您正确地要求所有模块。 If you have that set mongoose uses find, findOne, findById, where. 如果你有那套mongoose使用find,findOne,findById,where。 Links are provided. 提供链接。 Again hope im answering the question properly, not sure what your trying to achieve. 再次希望我能正确回答这个问题,不确定你想要实现的目标。

How update the _id of one MongoDB Document? 如何更新一个MongoDB文档的_id?

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

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

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM