1

So im working with nodeJS and mongoDB. im having a problem with updating a collection and its been a week since im working on it and it really gives me a headache. so this is my Sample collection

{
  "_id": ObjectId('51299sdfasdf') //this is just a sample Id     
  "names": [
      "NameA",
      "NameB",
      "NameC"   
   ],
  "names_id": [
     ObjectId('asdfasdf1312'),
     ObjectId('12sda123123a'),
     ObjectId('asdf1212123a')
  ],
  "user_alias": "SampleA"
}

then here's my script

var mongoose = require('mongoose');
var model = mongoose.model('Sample');

model.update( { _id: req.query._id }, 
       {
          $set: {
             names: namesArray, //this are set of names ex; ['name1', 'name2']
             names_id: namesIdArray //this are set of ids ex: [ ObjectId('sgsdfgsd'), ObjectId('asdfasdfadf')
          },
       },
       function(err, results) {
           if(err) res.status(500).send(err);
               console.log(results);


           }  
       );

I know I code it right. in the names_id arrays i specify all the object Ids like this mongoose.mongo.ObjectId('id here'). but it doesn't updating anything. here's the result.

{ ok: 1, nModified: 0, n: 1 }

What's the issue here? is this because of the version? my mongoose version is 2.15.8

Please help me out! thank you!

Joshua Fabillar
  • 506
  • 2
  • 9
  • 24

1 Answers1

1

Don't get so frustrate,there is nothing wrong with mongoose, I think your problem is because req.query.id is a string You need to first convert string to ObjectId

Document was not updated because it didn't find any document no other reason just convert _id to object id

Read this to convert I'd to object I'd Convert string to ObjectID in MongoDB

Community
  • 1
  • 1
Parshuram Kalvikatte
  • 1,616
  • 4
  • 20
  • 40