0

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?

Sourajyoti Bose
  • 83
  • 2
  • 10
  • Where is `id_array` coming from?. Also "bulk" is not what you seem to think it is in this context. Simply using `.updateMany()` does what you are asking here. You only ever need "bulk" when you send a "batch of requests". This is just a single request, and the API methods actually call this method underneath for you. – Neil Lunn Apr 21 '18 at 11:03
  • I am sending the a set of ids concatenated through a post request, which I split and form the id_array using this logic: var ids = req.body.ids.split(","); var id_array = []; for (var i=0;i – Sourajyoti Bose Apr 21 '18 at 11:05
  • 1
    So they are still "strings". You need to cast them as `ObjectId` values, which I presume they actually are in the collection under `_id`. Also `console.log(JSON.stringify(doc, undefined, 2))` on the response from the update will actually show you the serialized values of things like `nMatched` and `nModified`, so you can tell if anything is actually matched or modified, naturally. – Neil Lunn Apr 21 '18 at 11:06
  • 1
    In short, just call `testschema.updateMany({ _id: {$in: id_array} },{ $push: { comments: comment} })` and let mongoose do the casting work for you. Like I said before, this is not what "bulk" is for. Cross that bridge when you actually need it. Which is not now. – Neil Lunn Apr 21 '18 at 11:11
  • Can you give an instance where it qualifies to be a bulk? I am confused. Because if I have around 1000 ids to update shouldn't that qualify as a bulk update ? – Sourajyoti Bose Apr 21 '18 at 11:37
  • You're head is thinking "bulk" so you go looking for the method. But it's not what it means. If you want to know what it really is, then have a look around this very site. I think I might have written an answer or two that explained things already. – Neil Lunn Apr 21 '18 at 11:39

0 Answers0