简体   繁体   中英

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,

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,

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? 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. 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). 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. If you have that set mongoose uses 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?

http://mongoosejs.com/docs/models.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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