Mongoose find() return a weird property - node.js

I just learnt mongoose in node.js, but when I use the collection.find(), it return a weird properties like below:
model {
'$__': InternalCache {
strictMode: true,
selected: {},
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: 61d95a65fb65d62fb20ecb88,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: [StateMachine],
pathsToScopes: {},
ownerDocument: undefined,
fullPath: undefined,
emitter: [EventEmitter],
'$options': true
},
isNew: false,
errors: undefined,
_doc: {
tags: [Array],
date: 2022-01-08T09:33:25.205Z,
_id: 61d95a65fb65d62fb20ecb88,
name: 'Angular Course',
author: 'meow',
isPublished: true,
__v: 0
},
'$init': true
}
If I run it in terminal using mongodb instead of mongoose, I got the correct answer like that:
> db.courses.find()
{ "_id" : ObjectId("61d9592205df8e2f7230932f"), "tags" : [ "node", "backend" ], "date" : ISODate("2022-01-08T09:28:02.467Z"), "name" : "Node.js Course", "author" : "meow", "isPublished" : true, "__v" : 0 }
{ "_id" : ObjectId("61d95a65fb65d62fb20ecb88"), "tags" : [ "Angular", "frontend" ], "date" : ISODate("2022-01-08T09:33:25.205Z"), "name" : "Angular Course", "author" : "meow", "isPublished" : true, "__v" : 0 }

Use lean()
Normally, in mongoose, find() returns mongoose document not plain javascript object. Use lean() to returns a plain javascript object.
async function getCourses() {
// Documents returned from queries with the lean option enabled are plain javascript objects, not Mongoose Documents.
// read more: https://mongoosejs.com/docs/api.html#query_Query-lean
const courses = await Course.find().lean();
console.log(courses);
};

Related

Destructuring of the object returned by mongodb query result

Let's say we start a mongodb query statement as the following:
const user = await db.users.findOne(...);
console.log(user);
The result is good.
{
_id: 5f60647c28b90939d0e5fb24,
tenantId: '5e6f7c86e7158b42bf500371',
username: 'aaaa',
email: 'xxxx#yy.com',
createdAt: 2020-09-15T06:51:40.531Z,
updatedAt: 2020-09-15T06:51:40.531Z,
__v: 0
}
Then we use destructuring.
const { _id, username, ...others } = user;
console.log(others);
We get a weird thing:
[
[
'$__',
InternalCache {
strictMode: false,
selected: [Object],
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
saving: undefined,
version: undefined,
getters: {},
_id: 5f60647c28b90939d0e5fb24,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: [StateMachine],
pathsToScopes: {},
cachedRequired: {},
session: undefined,
'$setCalled': Set(0) {},
ownerDocument: undefined,
fullPath: undefined,
emitter: [EventEmitter],
'$options': [Object]
}
],
[ 'isNew', false ],
[ 'errors', undefined ],
[ '$locals', {} ],
[ '$op', null ],
[
'_doc',
{
_id: 5f60647c28b90939d0e5fb24,
tenantId: '5e6f7c86e7158b42bf500371',
username: 'aaaa',
email: 'xxxx#yyy.com',
createdAt: 2020-09-15T06:51:40.531Z,
updatedAt: 2020-09-15T06:51:40.531Z,
__v: 0
}
],
[ '$init', true ]
]
What's going on here? And how to make destructuring work again? There is the same error on Object.entries(others).
There is one workaround, I can stringify it and then parse it back. But this is obviously redundant.
By default, Mongoose queries return an instance of the Mongoose Document class. That's why you get the weird result after destructuring. In your case, you should use .lean()
on your query to get expected result;
this works for me :
const user = UserModel.findOne({...});
const { _id, username, ...others } = user.toObject();

Nodejs spread operator return some unexpected keys in returned object [duplicate]

This question already has answers here:
How do you turn a Mongoose document into a plain object?
(9 answers)
Closed 2 years ago.
There I have res is original object
{
time: 2020-07-26T10:39:38.135Z,
isTransfered: true,
_id: 5f1d5d6b60755e75b48770a6,
receiverAccountNumber: '12345678',
transfererAccountNumber: '11111111',
receiverName: 'Lê Công Tuyền',
transfererName: 'Nguyễn Thanh Tuấn',
amount: 1000000,
content: "test chuyefo'seajfg",
payFeeBy: 'transferer',
type: { name: 'internal', bankCode: 'TUB' },
__v: 0
}
And I got this result (called res2) is returned object using spread operator res2 = {...res} :
{
'$__': InternalCache {
strictMode: true,
selected: {},
// alot of key-value
'$setCalled': Set(0) {},
ownerDocument: undefined,
fullPath: undefined,
emitter: EventEmitter {
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: 0,
[Symbol(kCapture)]: false
},
'$options': { skipId: true, isNew: false, willInit: true }
},
isNew: false,
errors: undefined,
_doc: {
time: 2020-07-26T10:39:38.135Z,
isTransfered: true,
_id: 5f1d5d6b60755e75b48770a6,
receiverAccountNumber: '12345678',
transfererAccountNumber: '11111111',
receiverName: 'Lê Công Tuyền',
transfererName: 'Nguyễn Thanh Tuấn',
amount: 1000000,
content: "test chuyefo'seajfg",
payFeeBy: 'transferer',
type: { name: 'internal', bankCode: 'TUB' },
__v: 0
},
'$locals': {},
'$op': null,
'$init': true
}
I really dont know about this behavior of spread operator, that a lot of new key-value generated and the object I want to get is in _doc key.
Code is run on Nodejs v12 (nvm use 12)
This is probably because the res here is a mongoose document which has these values.
When you are doing a mongoose query by default it returns a document object. In order to receive a plain object use lean(). If you use lean then you won't be getting these unnecessary data when using spread operator.
Schema.findOne(query).lean()
However, if you need a document object from mongoose then in this case you can try the following to get rid of other values you don't need.
let res2 = { ...res.toObject() };

JWT improper decoding with mongoose data

Here is the data that i am encoding
{ _id: 5880c2562f109c2e17489155,
password: '$2a$10$1TGM/Nnoii/ERt5YZFqaROJA0176bXw5wn7fF9B7.DrikVcW/Va4e',
verified: false,
__v: 0 }
and the data that i get from decoding using jsonwebtoken.
{ '$__':
{ strictMode: true,
getters: {},
wasPopulated: false,
activePaths: { paths: [Object], states: [Object], stateNames: [Object] },
emitter: { domain: null, _events: {}, _eventsCount: 0, _maxListeners: 0 } },
isNew: false,
_doc:
{ __v: 0,
verified: false,
password: '$2a$10$1TGM/Nnoii/ERt5YZFqaROJA0176bXw5wn7fF9B7.DrikVcW/Va4e',
_id: '5880c2562f109c2e17489155' },
_pres:
{ '$__original_save': [ null, null ],
'$__original_validate': [ null ],
'$__original_remove': [ null ] },
_posts:
{ '$__original_save': [],
'$__original_validate': [],
'$__original_remove': [] },
iat: 1484834592 }
If you notice the docs i should be able to access the decoded password field using decoded.password but from this case i have to use decoded._doc.password. Is this happening somehow because i am directly passing in the mongoose object into jwt or the output is fine and i should access the data by adding _doc. The relevant code is
module.exports['generateToken'] = (data)=>{
return new Promise((fullfill,reject)=>{
console.log(data.user);
var token = jwt.sign(data.user,'shhhhhh');
fullfill(token);
});
}
module.exports['decodeToken'] = (token)=>{
return new Promise((fullfill,reject)=>{
jwt.verify(token,'shhhhhh',(err,decoded)=>{
if(err)
reject(err);
console.log(decoded);
fullfill(token);
});
});
}
data.user is the document that i got from mongoose query findOne.
Is this happening somehow because i am directly passing in the mongoose object into jwt
Yes, instances of mongoose models have quite complicated structure inside. And _doc is a reference to an internal document.
To avoid accessing by ._doc, you should encode document converted to a plain object:
module.exports['generateToken'] = (data)=>{
return new Promise((fullfill,reject)=>{
console.log(data.user);
var token = jwt.sign(data.user.toObject(),'shhhhhh');
fullfill(token);
});
}

Keystone.js / mongoose virtual fields lean record

I'm trying to produce a lean record for a REST API that include virtual fields.
The official documentation for how to implement virtual fields for Mongoose:
http://mongoosejs.com/docs/guide.html
My model:
var keystone = require('keystone')
, Types = keystone.Field.Types
, list = new keystone.List('Vendors');
list.add({
name : {
first: {type : Types.Text}
, last: {type : Types.Text}
}
});
list.schema.virtual('name.full').get(function() {
return this.name.first + ' ' + this.name.last;
});
list.register();
Now, let's query the model:
var keystone = require('keystone'),
vendors = keystone.list('Vendors');
vendors.model.find()
.exec(function(err, doc){
console.log(doc)
});
Virtual field name.full is not here:
[ { _id: 563acf280f2b2dfd4f59bcf3,
__v: 0,
name: { first: 'Walter', last: 'White' } }]
But if we do this:
vendors.model.find()
.exec(function(err, doc){
console.log(doc.name.full); // "Walter White"
});
Then the virtual shows.
I guess the reason is that when I do a console.log(doc) the Mongoose document.toString() method is invoked which does not include virtuals by default. Fair enough. That's understandable.
To include the virtuals in any of the conversion methods you have to go:
doc.toString({virtuals: true})
doc.toObject({virtuals: true})
doc.toJSON({virtuals: true})
However, this includes keys I don't want for my REST API to pump out to my users:
{ _id: 563acf280f2b2dfd4f59bcf3,
__v: 0,
name: { first: 'Walter', last: 'White', full: 'Walter White' },
_: { name: { last: [Object], first: [Object] } },
list:
List {
options:
{ schema: [Object],
noedit: false,
nocreate: false,
nodelete: false,
autocreate: false,
sortable: false,
hidden: false,
track: false,
inherits: false,
searchFields: '__name__',
defaultSort: '__default__',
defaultColumns: '__name__',
label: 'Vendors' },
key: 'Vendors',
path: 'vendors',
schema:
Schema {
paths: [Object],
subpaths: {},
virtuals: [Object],
nested: [Object],
inherits: {},
callQueue: [],
_indexes: [],
methods: [Object],
statics: {},
tree: [Object],
_requiredpaths: [],
discriminatorMapping: undefined,
_indexedpaths: undefined,
options: [Object] },
schemaFields: [ [Object] ],
uiElements: [ [Object], [Object] ],
underscoreMethods: { name: [Object] },
fields: { 'name.first': [Object], 'name.last': [Object] },
fieldTypes: { text: true },
relationships: {},
mappings:
{ name: null,
createdBy: null,
createdOn: null,
modifiedBy: null,
modifiedOn: null },
model:
{ [Function: model]
base: [Object],
modelName: 'Vendors',
model: [Function: model],
db: [Object],
discriminators: undefined,
schema: [Object],
options: undefined,
collection: [Object] } },
id: '563acf280f2b2dfd4f59bcf3' }
I can always of course just delete the unwanted keys, but this doesn't seem quite right:
vendors.model.findOne()
.exec(function(err, doc){
var c = doc.toObject({virtuals: true});
delete c.list;
delete c._;
console.log(c)
});
This produces what I need:
{ _id: 563acf280f2b2dfd4f59bcf3,
__v: 0,
name: { first: 'Walter', last: 'White', full: 'Walter White' },
id: '563acf280f2b2dfd4f59bcf3' }
Is there not a better way of getting a lean record?
I think you want the select method.. something like this:
vendors.model.findOne()
.select('_id __v name').
.exec(function(err, doc){
console.log(c)
});
Also personally I prefer setting virtuals: true on the schema rather than the document, but depends on use case I guess.
One solution would be to use a module like Lodash (or Underscore) which allows you pick a whitelist of property names:
vendors.model.findOne()
.exec(function(err, doc){
var c = _.pick(doc, ['id', 'name.first', 'name.last', 'name.full']);
console.log(c)
});
Given your use-case of serving this data via REST API, I think explicitly defining a whitelist of property names is safer. You could even define a virtual property on your schema which returns the predefined whitelist:
list.schema.virtual('whitelist').get(function() {
return ['id', 'name.first', 'name.last', 'name.full'];
});
and use it in multiple places, or have different versions of your whitelist, all managed at the model layer.

Model.findOne not returning docs but returning a wrapper object

I have defined a Model with mongoose like this:
var mongoose = require("mongoose")
var Schema = mongoose.Schema
var userObject = Object.create({
alias: String,
email: String,
password: String,
updated: {
type: Date,
default: Date.now
}
})
var userSchema = new Schema(userObject, {strict: false})
var User = mongoose.model('User', userSchema)
module.exports = User
Then I created a user that I can perfectly find through mongo console like this:
db.users.findOne({ email: "coco#coco.com" });
{
"_id" : ObjectId("55e97420d82ebdea3497afc7"),
"password" : "caff3a46ebe640e5b4175a26f11105bf7e18be76",
"gravatar" : "a4bfba4352aeadf620acb1468337fa49",
"email" : "coco#coco.com",
"alias" : "coco",
"updated" : ISODate("2015-09-04T10:36:16.059Z"),
"apps" : [ ],
"__v" : 0
}
However, when I try to access this object through a node.js with mongoose, the object a retrieve is not such doc, but a wrapper:
This piece of code...
// Find the user for which the login queries
var User = require('../models/User')
User.findOne({ email: mail }, function(err, doc) {
if (err) throw err
if (doc) {
console.dir(doc)
if(doc.password == pass) // Passwords won't match
Produces this output from console.dir(doc)...
{ '$__':
{ strictMode: false,
selected: undefined,
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: undefined,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: { paths: [Object], states: [Object], stateNames: [Object] },
ownerDocument: undefined,
fullPath: undefined,
emitter: { domain: null, _events: {}, _maxListeners: 0 } },
isNew: false,
errors: undefined,
_doc:
{ __v: 0,
apps: [],
updated: Fri Sep 04 2015 12:36:16 GMT+0200 (CEST),
alias: 'coco',
email: 'coco#coco.com',
gravatar: 'a4bfba4352aeadf620acb1468337fa49',
password: 'caff3a46ebe640e5b4175a26f11105bf7e18be76',
_id: { _bsontype: 'ObjectID', id: 'Uét Ø.½ê4¯Ç' } },
'$__original_validate': { [Function] numAsyncPres: 0 },
validate: [Function: wrappedPointCut],
_pres: { '$__original_validate': [ [Object] ] },
_posts: { '$__original_validate': [] } }
Therefore, passwords won't match because doc.password is undefined.
Why is this caused?
That's exactly the purpose of mongoose, wrapping mongo objects. It's what provides the ability to call mongoose methods on your documents. If you'd like the simple object, you can call .toObject() or use a lean query if you don't plan on using any mongoose magic on it at all. That being said, the equality check should still hold as doc.password returns doc._doc.password.

Resources