Skip to content

Instantly share code, notes, and snippets.

@jwerre
Last active April 15, 2021 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwerre/ef447dc1d60a48865c8574dff73d7a69 to your computer and use it in GitHub Desktop.
Save jwerre/ef447dc1d60a48865c8574dff73d7a69 to your computer and use it in GitHub Desktop.
Mongoose Select Bug
#!/usr/bin/env node
const {inspect} = require('util');
const mongoose = require('mongoose');
const DB_NAME = 'selectTest';
const LANGUGAGES = [
{
name: 'Danish',
code: 'da',
rtl: false,
published: true
},
{
name: 'Italian',
code: 'it',
rtl: false,
published: true
},
{
name: 'German',
code: 'de',
rtl: false,
published: true
},
{
name: 'Persian',
code: 'fa',
rtl: true,
published: true
},
{
name: 'Hebrew',
code: 'he',
rtl: true,
published: true
},
{
name: 'Polish',
code: 'pl',
rtl: false,
published: true
},
{
name: 'Czech',
code: 'cs',
rtl: false,
published: true
},
{
name: 'Norwegian',
code: 'no',
rtl: false,
published: true
},
{
name: 'Arabic',
code: 'ar',
rtl: true,
published: true
},
{
name: 'Dutch',
code: 'nl',
rtl: false,
published: true
},
{
name: 'Swedish',
code: 'sv',
rtl: false,
published: true
},
{
name: 'English',
code: 'en',
rtl: false,
published: true
},
{
name: 'Russian',
code: 'ru',
rtl: false,
published: true
},
{
name: 'Spanish',
code: 'es',
rtl: false,
published: true
},
{
name: 'French',
code: 'fr',
rtl: false,
published: true
},
{
name: 'Ukrainian',
code: 'uk',
rtl: false,
published: true
},
{
name: 'Korean',
code: 'ko',
rtl: false,
published: true
},
{
name: 'Hindi',
code: 'hi',
rtl: false,
published: true
},
{
published: true,
rtl: false,
name: 'Thai',
code: 'th',
"__v": 0
},
{
name: 'Japanese',
code: 'ja',
rtl: false,
published: true
}
];
( async () => {
console.log('Mongoose Version: ', mongoose.version);
// 1. Connect to DB
try {
await mongoose.connect( `mongodb://127.0.0.1:27017/${DB_NAME}`, { useNewUrlParser: true, useUnifiedTopology: true});
} catch (err) {
return Promise.reject(err);
}
// 2. Setup collection and data
const schema = new mongoose.Schema({
name: String,
code: String,
rtl: Boolean,
published: Boolean,
});
const Language = mongoose.model('language', schema);
for (let lang of LANGUGAGES) {
const language = new Language(lang);
try {
await Language.create(language);
} catch (err) {
return Promise.reject(err);
}
}
// 3. Run query
// when select is run with string values MongoDB 4.4 replaces the values
// with the select strings. This works fine in MongoDB 4.2. Passing Numbers
// instead of strings works fine as well.
const result = await Language.find()
.select({
code: '1',
name: '<script>alert("Boo!");</script>',
});
// 4. Cleanup
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
return result;
})()
.then( (result) => {
console.log('Document Retrived:');
console.log( inspect(result, {depth:10, colors:true}) );
process.exit(0);
})
.catch( (e) => {
console.error(e);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment