简体   繁体   English

如何使用具有 2 个 ID 的 mongoose 推送到嵌套数据?

[英]How to push to nested data using mongoose with 2 ids?

I have this Node push function but it works only on 1 level nested objects I need it to drill deeper in to a second id in the DB model any one with such experience?我有这个节点推送 function 但它仅适用于 1 级嵌套对象我需要它来深入研究 DB model 中的第二个 id 任何有这种经验的人?

 // One level functional using one id finds the user and update const update = await Budget.findOneAndUpdate( { '_id': `${id}` }, { '$push': { [`${sliceLeft}`]: location } }, { _id: true, new: true } ) // Dysfunctional.: how to implement second id targeting to that nested lvl2 deep object let updateDeptArr = await Budget,findOneAndUpdate( { '_id': `${id}` }. //User id { '$push'. { [`${sliceLeft}:$,_id`//Second ID ]: location// Data } }, { _id: true, new: true } )

To use $ operator, the array field must appear as part of the query document.要使用$运算符,数组字段必须作为查询文档的一部分出现。 So your query should be something like:所以你的查询应该是这样的:

let updateDeptArr = await Budget.findOneAndUpdate(
  { 
    '_id': `${id}`,
    [`${sliceLeft}._id`]: YOUR_SECOND_ID
  }, 
  {
    '$push': {
       // Nested array field is dayOutcome refer to OP's comment
       [`${sliceLeft}.$.dayOutcome`]: location
    }
  }...

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

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