I have a collection where I want to update an array. The schema is as follows:-
var testSchema = new mongoose.Schema({
...,
comments : {
type : [ String ],
required: true
},
...
});
I gather all the ids of the documents and then try to insert a generic comment for the ids that are necessary. The id_array and comment is being passed properly through the post request and is being logged in the console log. The code I am using is as follows:-
console.log(id_array);
console.log(comment);
var bulk = testSchema.collection.initializeOrderedBulkOp();
bulk.find({_id: {$in: id_array}}).update({$push: {comments: comment}});
bulk.execute(function (error, doc) {
var response = {
status : 200,
message : doc
};
if (error) {
console.log('Error');
response.status = 500;
response.message = err;
} else if (!doc) {
console.log(doc);
response.status = 404;
response.message = {
"message" : "Callback failed"
};
}
console.log(doc);
res
.status(response.status)
.json(response.message);
});
After this snippet is executed we get :-
BulkWriteResult {
ok: [Getter],
nInserted: [Getter],
nUpserted: [Getter],
nMatched: [Getter],
nModified: [Getter],
nRemoved: [Getter],
getInsertedIds: [Function],
getUpsertedIds: [Function],
getUpsertedIdAt: [Function],
getRawResponse: [Function],
hasWriteErrors: [Function],
getWriteErrorCount: [Function],
getWriteErrorAt: [Function],
getWriteErrors: [Function],
getLastOp: [Function],
getWriteConcernError: [Function],
toJSON: [Function],
toString: [Function],
isOk: [Function] }
However the comments field is not updated. Where is it that I am going wrong?