Code snippets

aggregateWithMatchIdExample.js

Problem: mongoose aggregate $match does not match id

/*Your id variable will be constructed of "string", and not ObjectId value.
Mongoose "autocasts" string values for ObjectId into their correct type in regular queries,
but this does not happen in the aggregation pipeline, as in described in issue #1399:
https://github.com/Automattic/mongoose/issues/1399
Instead you must do the correct casting to type manually:*/
import mongoose from 'mongoose'
id = mongoose.Types.ObjectId(anyModel)


-------------------------------------------
index.js

Problem: What is the best way to get the key of key/value javascript object?

/** There are many roads, these are some of them... :) **/
const myObject = {name: 'Lujan', nickname: 'Lushan'}
for(let key in myObject){
console.log(`Key: ${key}`);
console.log(`Keys value ${myObject[key]}`);
}
//Or
Object.keys(myObject)
.forEach(function eachKey(key) {
console.log(`Key: ${key}`);
console.log(`Keys value ${myObject[key]}`);
});