Skip to content

Instantly share code, notes, and snippets.

@ajmas
Last active October 7, 2020 20:13
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 ajmas/249f406ff1d85a8dc8e6a54c6d24175a to your computer and use it in GitHub Desktop.
Save ajmas/249f406ff1d85a8dc8e6a54c6d24175a to your computer and use it in GitHub Desktop.
Testing performance advantage of `$and` in mongodb
/* eslint-disable no-console */
import mongoose from 'mongoose';
let User;
function generateRandomString (length = 6) {
return Math.random().toString(20).substr(2, length)
}
async function createEntries () {
// create two users (will not be unique on multiple runs)
console.log('Creating some users');
let entries = [];
for (let i = 0; i < 10000; i++) {
const name = generateRandomString(10);
entries.push({
name: name,
email: `${name}@abc.def`,
address: {
city: 'Melbourne',
countryCode: 'AU'
}
});
}
await User.insertMany(entries);
entries = [];
for (let i = 0; i < 100000; i++) {
const name = generateRandomString(10);
const user = {
name: name,
email: `${name}@abc.def`,
address: {
city: 'Perth',
countryCode: 'AU'
}
}
await User.create(user);
}
await User.insertMany(entries);
entries = [];
for (let i = 0; i < 100000; i++) {
const name = generateRandomString(10);
const user = {
name: name,
email: `${name}@abc.def`,
address: {
city: 'Brisbane',
countryCode: 'AU'
}
}
await User.create(user);
}
await User.insertMany(entries);
entries = [];
for (let i = 0; i < 100000; i++) {
const name = generateRandomString(10);
const user = {
name: name,
email: `${name}@abc.def`,
address: {
city: 'Auckland',
countryCode: 'NZ'
}
}
await User.create(user);
}
await User.insertMany(entries);
entries = [];
for (let i = 0; i < 100000; i++) {
const name = generateRandomString(10);
const user = {
name: name,
email: `${name}@abc.def`,
address: {
city: 'Lima',
countryCode: 'PE'
}
}
await User.create(user);
}
await User.insertMany(entries);
entries = [];
for (let i = 0; i < 100000; i++) {
const name = generateRandomString(10);
const user = {
name: name,
email: `${name}@abc.def`,
address: {
city: 'Mexico City',
countryCode: 'MX'
}
}
await User.create(user);
}
await User.insertMany(entries);
entries = [];
for (let i = 0; i < 100000; i++) {
const name = generateRandomString(10);
const user = {
name: name,
email: `${name}@abc.def`,
address: {
city: 'Frankfurt',
countryCode: 'DE'
}
}
await User.create(user);
}
await User.insertMany(entries);
await User.create({ name: 'Jane Doe', email: 'jane.done@example.abcdef' });
await User.create({ name: 'Joe Bloggs', email: 'jane.done@example.abcdef' });
}
async function mongooseHelloWorld () {
const url = 'mongodb://localhost/helloworld';
// provide options to avoid a number of deprecation warnings
// details at: https://mongoosejs.com/docs/connections.html
const options = {
'useNewUrlParser': true,
'useCreateIndex': true,
'useFindAndModify': false,
'useUnifiedTopology': true
};
// connect to the database
console.log(`Connecting to the database at ${url}`);
const connection = await mongoose.connect(url, options);
// create a schema, specifying the fields and also
// indicating createdAt/updatedAt fields should be managed
const userSchema = new mongoose.Schema({
name:{
type: String,
required:true
},
email: {
type: String,
required: true
},
address: {
street1: String,
street2: String,
town: {
type: String,
index: true
},
adminDivision: String,
postalCode: String,
countryCode: {
type: String,
index: true
}
}
}, {
timestamps: true
});
// this will use the main connection. If you need to use custom
// connections see: https://mongoosejs.com/docs/models.html
User = mongoose.model('User', userSchema);
// Display indexes
console.log(await connection.models.User.collection.getIndexes());
// comment this out after initial run
await createEntries();
let start = Date.now();
await User.find({
'name': '2hchd61bbj',
'address.town': 'Lima',
'address.countryCode': 'PE'
});
console.log('run time: ', (Date.now() - start));
start = Date.now();
await User.find({
$and: [
{ 'address.countryCode': 'PE' },
{ 'address.town': 'Lima' },
{ 'name': '2hchd61bbj' }
]
});
console.log('run time: ', (Date.now() - start));
}
mongooseHelloWorld()
.then(() => process.exit(0))
.catch(error => {
console.log(error)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment