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
  • Are you sure that `req.query._id` is defined? Also, there is no Mongoose version 2.15.8. – robertklep Nov 05 '16 at 09:01
  • yes its defined. and about the version. when i Type it on my git npm mongoose -v. 2.15.8. this will show up – Joshua Fabillar Nov 05 '16 at 09:11
  • I don't think there is a problem with Mongoose or MongoDB. Can you trying printing the value for `req.query.id`? I think the reason it is not updating it because it does not have record with that specific ID. – Samay Nov 05 '16 at 09:12
  • @JoshuaFabillar that's the version of `npm` :D For the Mongoose version, use `npm ls mongoose`. – robertklep Nov 05 '16 at 09:13
  • @JoshuaFabillar just to be sure: the collection that Mongoose will be using is called `samples`. I mention this because it won't be the first time that people think that the name of the model (`Sample`) is also the name of the collection being used. – robertklep Nov 05 '16 at 09:30
  • @robertklep. Oh sorry about that. yes this is just a sample :P. @Samay. the req.query._id has a value.`744112df269d36c1c9c69fc`. thats the value of _id. – Joshua Fabillar Nov 05 '16 at 09:40
  • alright. so here's the version of my mongoose. `mongoose@4.3.3`. thank you @robertklep – Joshua Fabillar Nov 05 '16 at 09:44
  • @JoshuaFabillar `744112df269d36c1c9c69fc` has a length of 23, but a valid `ObjectId` should have length 24. – robertklep Nov 05 '16 at 10:32
  • @robertklep. hmm interesting. but that Id is generated by Mongodb. – Joshua Fabillar Nov 05 '16 at 14:26
  • @JoshuaFabillar are you sure? Because it's not valid. – robertklep Nov 05 '16 at 14:30
  • @JoshuaFabillar but atleast you can give a try and convert Id(string) to objectid and check just to give a try – Parshuram Kalvikatte Nov 05 '16 at 15:08

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