通过mongoose更新mongo中的嵌套数组中的特定值

我有一些下面的模式(没有_id) –

{uid: String, inbox:[{msgid:String, someval:String}] } 

现在,在请求中,我得到了这个msgid,并在下面的这个mongoose查询中使用它 –

  my_model.findOne({'inbox.msgid':'msgidvaluexyz'} , function(err, doc) { console.log(doc); return !0; }) 

现在,问题是,我收到具有特定信息的整个文档以及收件箱中的其他邮件 –

  Output- {uid:'xyz', inbox:[ {msgid:,someval}, {msgid:'our queried msgid',someval}, //required sub array {msgid:,someval}, ] } 

现在我可以使用什么查询来获取特定的子数组,因为文档收件箱太大而无法循环。

使用$位置select运算符使返回的文档只包含匹配的inbox元素:

 my_model.findOne({'inbox.msgid':'msgidvaluexyz'} , {'inbox.$': 1} , function(err, doc) { console.log(doc); return !0; }) 

如果我正确理解你的问题:

 // find each person with a last name matching 'Ghost' var query = Person.findOne({ 'name.last': 'Ghost' }); // selecting the `name` and `occupation` fields query.select('name occupation'); 

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

你可以select你想要回来的领域。 您只能获得inbox数组或除此之外的所有内容。