Mongoose: co słychać w „_doc”?

Wygląda na to, że Mongoose robi coś naprawdę fajnego wewnętrznie.

1 var Foo = new mongoose.model('Foo', new mongoose.Schema({a: String, b: Number}));
2 var foo = new Foo({a: 'test; b: 42}); 
3 var obj = {c: 1};
4 foo.goo = obj;                  // simple object assignment. obj should be 
                                  //   passed by reference to foo.goo. recall goo
                                  //   is not defined in the Foo model schema

5 console.log(foo.goo === obj);   // comparison directly after the assignment
    // => false, doesn't behave like normal JS object

Zasadniczo za każdym razem, gdy próbujesz radzić sobie z właściwościami modelu Mongoose, które nie są a) zdefiniowane w schemacie modelu lub b) zdefiniowane jako ten sam typ (tablica, obj, ..) ... model nawet nie zachowuj się jak normalny obiekt JavaScript.

Linia przełączania4 dofoo._doc.goo = obj tworzy wyjście konsolitrue.

edytować: próba odtworzenia dziwności

Przykład 1:

 // Customer has a property 'name', but no property 'text'
 // I do this because I need to transform my data slightly before sending it
 // to client.
 models.Customer.find({}, function(err, data) {
     for (var i=0, len=data.length; i<len; ++i) {
        data[i] = data[i]._doc;            // if I don't do this, returned data
                                           // has no 'text' property
        data[i].text = data[i].name;       
    }
    res.json({success: err, response:data});
});

questionAnswers(2)

yourAnswerToTheQuestion