Member-only story
Using MongoDB with Mongoose — Queries and Aggregations
3 min readJan 19, 2021
To make MongoDB database manipulation easy, we can use the Mongoose NPM package to make working with MongoDB databases easier.
In this article, we’ll look at how to use Mongoose to manipulate our MongoDB database.
Cursor Timeout
We can set the noCursorTimeout
flag to disable cursor timeout.
For example, we can write:
async function run() {
const { createConnection, Schema } = require('mongoose');
const connection = createConnection('mongodb://localhost:27017/test');
const schema = new Schema({
name: {
first: String,
last: String
},
occupation: String
});
const Person = connection.model('Person', schema);
const person = new Person({
name: {
first: 'james',
last: 'smith'
},
occupation: 'host'
})
await person.save();
const person2 = new Person({
name: {
first: 'jane',
last: 'smith'
},
occupation: 'host'
})
await person2.save();
const cursor = Person.find({ occupation: /host/ })
.cursor()
.addCursorFlag('noCursorTimeout', true);
let doc;
while (doc = await cursor.next()) {
console.log(doc);
}
}
run();
We call the addCursorFlag
to disable the cursor timeout.