mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Mongoose returns weird object

Open ghost opened this issue 8 years ago • 5 comments

Mongoose v5.0.2 After using the find method it returns an object like this { "$__": { "strictMode": true, "selected": {}, "getters": {}, "_id": { "_bsontype": "ObjectID", "id": { "0": 90, "1": 111, "2": 10, "3": 129, "4": 118, "5": 190, "6": 92, "7": 11, "8": 116, "9": 21, "10": 108, "11": 7 } }, "wasPopulated": false, "activePaths": { "paths": { "password": "init", "email": "init", "name": "init", "_id": "init", "__v": "init" }, "states": { "ignore": {}, "default": {}, "init": { "_id": true, "name": true, "password": true, "email": true, "__v": true }, "modify": {}, "require": {} }, "stateNames": [ "require", "modify", "init", "default", "ignore" ] }, "pathsToScopes": {}, "emitter": { "domain": null, "_events": {}, "_eventsCount": 0, "_maxListeners": 0 }, "$options": true }, "isNew": false, "_doc": { "_id": { "_bsontype": "ObjectID", "id": { "0": 90, "1": 111, "2": 10, "3": 129, "4": 118, "5": 190, "6": 92, "7": 11, "8": 116, "9": 21, "10": 108, "11": 7 } }, "name": "melvin", "password": "test", "email": "test", "__v": 0 }, "$init": true }

How to get rid of other property? and just return the name, password email and id.

ghost avatar Jan 29 '18 11:01 ghost

Mongoose documents are pretty sophisticated objects, but you should be able to just call doc.toObject() to convert it to a POJO. How are you printing this output?

vkarpov15 avatar Jan 30 '18 23:01 vkarpov15

yeah we can use the doc.toObject().

How are you printing this output?

I found another issue similar on how I print this. I use routing controllers and I guess it is because of the class-transformer. here's the link https://github.com/typestack/routing-controllers/issues/153

ghost avatar Jan 31 '18 08:01 ghost

So what's the takeaway here? Can you use doc.toObject() or is there another issue here?

vkarpov15 avatar Feb 01 '18 00:02 vkarpov15

So what's the takeaway here?

hmm it is not an issue of mongoose, I think we can close this now. Thanks @vkarpov15

Can you use doc.toObject() or is there another issue here?

yup we can use doc.toObject(), no other issue here. we can close this now

ghost avatar Feb 01 '18 01:02 ghost

https://mongoosejs.com/docs/tutorials/lean.html worked for me

upugo-dev avatar Jul 23 '19 08:07 upugo-dev