Skip to content

Instantly share code, notes, and snippets.

@lineus lineus/nsuchy2.js Secret
Last active Apr 6, 2018

Embed
What would you like to do?
.skip() effects the cursor not the query
#!/usr/bin/env node
'use strict'
const assert = require('assert')
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/test')
const Schema = mongoose.Schema
const schema = new Schema({
name: String
})
const Test = mongoose.model('test', schema)
const tests = []
for (let i = 0; i < 1000; i++) {
tests.push(new Test({ name: `test${i}` }))
}
async function run () {
let good = 0
let bad = 0
let expected = 0
await Test.remove({})
await Test.create(tests)
let firstDoc = await Test.findOne({}).exec()
for (let i = 0; i < 10000; i++) {
let rand = Math.floor(Math.random() * 1000)
if (rand === 0) {
expected++
}
let cond = { name: firstDoc.name }
let res = await Test.findOne(cond).skip(rand).exec()
if (res) {
good++
} else {
bad++
}
}
console.log(`good: ${good} | bad: ${bad}`)
assert.strictEqual(expected, good)
return mongoose.connection.close()
}
run().catch(console.error)
gitter: while true; do ./nsuchy2.js ;done
good: 5 | bad: 9995
good: 12 | bad: 9988
good: 11 | bad: 9989
good: 3 | bad: 9997
good: 8 | bad: 9992
good: 7 | bad: 9993
good: 11 | bad: 9989
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.