Reputation: 2467
I have this schema
var ArticleSchema = new Schema({
user_id: { type: Schema.ObjectId, ref: "User"},
title: String,
description: String,
content: String,
visible: Boolean,
saved: Boolean,
deleted: {type: Boolean, default: false },
permalink: String,
created_at: { type: Date, default: Date.now },
edited_at: { type: Date, default: Date.now },
tags : [],
comments: {
comment: String,
name: String,
email: String,
date: Date,
deleted: Boolean,
}
});
I want to update the deleted field into the comments array for a specific array.
Here is how I'm doing it.
this.model.update(
{
_id: data.articleId
},
{
$set: { 'comments.$.deleted' : true }
},
function(err, doc){
console.info(doc);
console.info(err);
callback(doc);
});
};
Nothing happens, I tried directly on mongo console and it works.
But not in mongoose. For any reason I got in console.info(), null and 0 as results. The document is never updated.
If I try to update other value not nested it works.
Upvotes: 1
Views: 72
Reputation: 103445
You could try the following:
this.model.update(
{
_id: data.articleId
},
{
$set: { 'comments.deleted' : true }
},
function(err, doc){
console.info(doc);
console.info(err);
callback(doc);
});
};
Your code is not working because the comments field is not an array but an embedded sub-document and the $
positional operator only identifies an element in an array to update without explicitly specifying the position of the element in the array.
Upvotes: 3