0

Restaurant is a Mongoose model. I tried to change things in place and it doesn't work:

  Restaurant.find({}, function(err, results) {
    for (var i = 0; i < results.length; i++) {
      delete results[i].__v;
  }

I think results[i] is a model object. I tried to see check the property descriptors with the following and it's says undefined:

console.log(JSON.stringify(Object.getOwnPropertyDescriptor(results[i], '__v')));

Why doesn't delete on the object work? And how come I can't see the property descriptor?

EDIT: So it's a Mongoose document. But what in Javascript parlance can an object such as a Mongoose document be based on something else other than a Javascript object? Some wrapper base on internal C code or something?

huggie
  • 17,587
  • 27
  • 82
  • 139

1 Answers1

3

results isn’t a Javascript Object and then you can’t use remove

You can Convert in Javascript Object with results.toObject() doc

or you can do Restaurant.find({},{lean:true}doc

and then you can do delete results[i].__v;

Barno
  • 3,271
  • 5
  • 28
  • 58
  • If it's not implemented by a JS object, what is it? – huggie Jul 30 '14 at 15:20
  • It's [Mongoose Document](http://mongoosejs.com/docs/api.html#document-js) – Barno Jul 30 '14 at 15:37
  • Documents returned from queries with the lean option enabled are plain javascript objects, not MongooseDocuments. – Barno Jul 30 '14 at 16:47
  • Yes thanks to you I get it. Though in Javascript parlance, what can be an Mongoose Document an object of if it's not also a JS object? Every object in JS is a kind of JS object according to what I know. How do you make something that's not? – huggie Jul 31 '14 at 02:19
  • {lean:true} doesn't work. It still doesn't allow me to add or delete fields. – huggie Jul 31 '14 at 02:52
  • find().lean().exec() does work though. – huggie Jul 31 '14 at 02:55