Skip to content

Instantly share code, notes, and snippets.

@lineus lineus/foureight84.js Secret
Last active Jul 5, 2018

Embed
What would you like to do?
_mongooseOptions in Query objects holds the populate paths
#!/usr/bin/env node
'use strict';
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;
const otherSchema = new Schema({
name: String
});
const schema = new Schema({
name: String,
other: {
type: Schema.Types.ObjectId,
ref: 'other'
},
});
schema.pre('findOne', function() {
console.log(Object.keys(this._mongooseOptions.populate));
});
const Other = mongoose.model('other', otherSchema);
const Test = mongoose.model('test', schema);
const other = new Other({ name: 'some other'});
const test = new Test({ name: 'testing123', other: other._id });
async function run() {
await conn.dropDatabase();
await other.save();
await test.save();
let found = await Test.findOne().populate('other');
return conn.close();
}
run();
$: ./foureight84.js
[ 'other' ]
$:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.