簡體   English   中英

mongodb:從嵌套的引用數組中拉出一項

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

我有一個嵌套的引用數組

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

我想從該數組中刪除一個引用,我認為這很容易使用

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

這和像

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

或使用findOneAndUpdate都對我不起作用。

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