Reputation: 1301
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.
postId
is formated like for example "5c150b855999681f7423aacb"
Upvotes: 0
Views: 54
Reputation: 474
User.findOneAndUpdate({
_id: mongoose.Types.ObjectId('YOUR_DATA_ID')
},
{ name: currentName,
{ $pull: { posts: mongoose.Types.ObjectId(postId) } }
);
Upvotes: 1