2017-06-15 122 views
1

我是想用两个参数找到要插入一个名字我的架构的“data_stream_map”阵列更新,猫鼬查找和_id

如下,

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”,我m出了什么线索,有没有人可以帮助我解决这个问题?我提到了很多堆栈溢出问题,但仍然没有解决。

+1

问题是什么?这看起来很好。您正在使用'.findOneAndUpdate()'而不返回文档,因此您只需使用'.update()'。那是文件不返回的问题吗? –

+0

没问题,它不会将名称插入到data_stream_map。 – d7k

+0

尝试更新()以及但没有帮助。 – d7k

回答

0

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

P.S.如果您只是试图查询输入内容,请确保您的文件已正确链接,即您需要正确地使用所有模块。如果你有猫鼬使用find,findOne,findById,在哪里。提供链接。再次希望即时回答这个问题,不知道你想达到什么目的。

How update the _id of one MongoDB Document?

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

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

+0

是的,当然会尝试这些。 – d7k

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 
+2

尽管此代码可能会回答问题,但提供有关如何解决问题和/或为何解决问题的其他上下文可以提高答案的长期价值。 – Badacadabra