1

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.

AshanD
  • 527
  • 5
  • 22
  • 1
    What is the issue? This looks fine. You are using `.findOneAndUpdate()` without returning the document so you could just use `.update()` . Is that the problem, that the document does not return? – Neil Lunn Jun 15 '17 at 13:30
  • no the problem is it doesnt insert the name to the data_stream_map. – AshanD Jun 15 '17 at 13:32
  • tried update() as well but no help. – AshanD Jun 15 '17 at 13:33
  • 1
    Well as you can see you are using the exact same arguments, so something else is different. Check you are connecting to the same database namespace and collection. Mongoose models "pluralize" collection names. I would suggest you are pointing to a different collection and should specify explicitly as in `mongoose.model('WFWorkflowModel', WFWorkflowModelSchema, 'wfcomponents')` or whatever you actually called it, where the "third" argument there overrides the default choice of collection name. So explicitly name it. – Neil Lunn Jun 15 '17 at 13:35
  • okay sure ill try – AshanD Jun 15 '17 at 13:47

2 Answers2

0

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.

P.S. If your just trying to query input, make sure that your files are properly linked i.e. 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

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
xhmm
  • 1
  • 2
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 15 '17 at 15:09