简体   繁体   English

mongodb:从嵌套的引用数组中拉出一项

[英]mongodb: pull one item from nested array of references

I have a nested array of references 我有一个嵌套的引用数组

const userSchema = new Schema(
  {
    _id: Schema.Types.ObjectId,
    name: String,
   posts: [{ type: Schema.Types.ObjectId, ref: "Post" }]
  }
);

from that array I want to remove one reference, I assumed this would be easy using 我想从该数组中删除一个引用,我认为这很容易使用

 User.update({ name: currentName}, { $pull: { posts: postId }});

this and variations like 这和像

 User.update(
    { name: currentName},
    { $pull: { posts: mongoose.Types.ObjectId(postId) } }
  );

or using findOneAndUpdate all didn't work for me. 或使用findOneAndUpdate都对我不起作用。

postId is formated like for example "5c150b855999681f7423aacb" postId的格式类似于"5c150b855999681f7423aacb"

User.findOneAndUpdate({
_id: mongoose.Types.ObjectId('YOUR_DATA_ID')
},
{ name: currentName,
{ $pull: { posts: mongoose.Types.ObjectId(postId) } }
);

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

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