Skip to content

Instantly share code, notes, and snippets.

@lineus lineus/1_populate.js Secret
Created Mar 6, 2018

Embed
What would you like to do?
Trying out strictQuery
#!/usr/bin/env node
'use strict'
const mongoose = require('../../lib/test_db')
const Schema = mongoose.Schema
const faker = require('faker')
const testSchema = new Schema({
name: String,
someValue: [{
value: String
}]
})
const Test = mongoose.model('tests', testSchema)
const tests = []
for (let i = 0; i < 10; i++) {
tests.push(new Test({
name: faker.fake('{{name.firstName}} {{name.lastName}}'),
someValue: [{
value: faker.hacker.abbreviation()
}]
}))
}
Test.create(tests).then((res) => {
mongoose.connection.close()
return console.log(res.length, 'records created.')
}, (rej) => {
mongoose.connection.close()
return console.error(rej)
})
// Output:
// $./ populate.js
// 10 'records created.'
// $
// mongo shell
// > db.tests.findOne()
// {
// "_id" : ObjectId("5a9e9aafac88dc591949679b"),
// "name" : "Dena Stokes",
// "someValue" : [
// {
// "value": "XSS",
// "_id": ObjectId("5a9e9aafac88dc591949679a")
// }
// ],
// "__v" : 0
// }
// >
#!/usr/bin/env node
'use strict'
const mongoose = require('../../lib/test_db')
mongoose.set('debug', true)
const Schema = mongoose.Schema
const newUserSchema = new Schema({
name: String,
newValue: String
}, { strictQuery: true, strict: true })
const Test = mongoose.model('tests', newUserSchema)
Test.find({ someValue: 1 }).then((res) => {
console.log(res[0])
}, (rej) => {
console.log(rej)
})
// Output:
// $./find.js
// Mongoose: tests.find({}, { fields: {} })
// {
// _id: 5a9e9aafac88dc591949679b,
// name: 'Dena Stokes',
// someValue: [{ value: 'XSS', _id: 5a9e9aafac88dc591949679a }], // I expected this field to be stripped
// __v: 0
// }
// ^ C
// $
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.