Queries

Mongoose models provide several static helper functions for CRUD operations. Each of these functions returns a mongoose Query object.

A mongoose query can be executed in one of two ways. First, if you pass in a callback function, Mongoose will execute the query asynchronously and pass the results to the callback.

A query also has a .then() function, and thus can be used as a promise.

Executing

When executing a query with a callback function, you specify your query as a JSON document. The JSON document’s syntax is the same as the MongoDB shell.

  1. const Person = mongoose.model('Person', yourSchema);
  2. // find each person with a last name matching 'Ghost', selecting the `name` and `occupation` fields
  3. Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) {
  4. if (err) return handleError(err);
  5. // Prints "Space Ghost is a talk show host".
  6. console.log('%s %s is a %s.', person.name.first, person.name.last,
  7. person.occupation);
  8. });

Mongoose executed the query and passed the results to callback. All callbacks in Mongoose use the pattern: callback(error, result). If an error occurs executing the query, the error parameter will contain an error document, and result will be null. If the query is successful, the error parameter will be null, and the result will be populated with the results of the query.

Anywhere a callback is passed to a query in Mongoose, the callback follows the pattern callback(error, results). What results is depends on the operation: For findOne() it is a potentially-null single document, find() a list of documents, count() the number of documents, update() the number of documents affected, etc. The API docs for Models provide more detail on what is passed to the callbacks.

Now let’s look at what happens when no callback is passed:

  1. // find each person with a last name matching 'Ghost'
  2. const query = Person.findOne({ 'name.last': 'Ghost' });
  3. // selecting the `name` and `occupation` fields
  4. query.select('name occupation');
  5. // execute the query at a later time
  6. query.exec(function (err, person) {
  7. if (err) return handleError(err);
  8. // Prints "Space Ghost is a talk show host."
  9. console.log('%s %s is a %s.', person.name.first, person.name.last,
  10. person.occupation);
  11. });

In the above code, the query variable is of type Query. A Query enables you to build up a query using chaining syntax, rather than specifying a JSON object. The below 2 examples are equivalent.

  1. // With a JSON doc
  2. Person.
  3. find({
  4. occupation: /host/,
  5. 'name.last': 'Ghost',
  6. age: { $gt: 17, $lt: 66 },
  7. likes: { $in: ['vaporizing', 'talking'] }
  8. }).
  9. limit(10).
  10. sort({ occupation: -1 }).
  11. select({ name: 1, occupation: 1 }).
  12. exec(callback);
  13. // Using query builder
  14. Person.
  15. find({ occupation: /host/ }).
  16. where('name.last').equals('Ghost').
  17. where('age').gt(17).lt(66).
  18. where('likes').in(['vaporizing', 'talking']).
  19. limit(10).
  20. sort('-occupation').
  21. select('name occupation').
  22. exec(callback);

A full list of Query helper functions can be found in the API docs.

Queries are Not Promises

Mongoose queries are not promises. They have a .then() function for co and async/await as a convenience. However, unlike promises, calling a query’s .then() can execute the query multiple times.

For example, the below code will execute 3 updateMany() calls, one because of the callback, and two because .then() is called twice.

  1. const q = MyModel.updateMany({}, { isDeleted: true }, function() {
  2. console.log('Update 1');
  3. });
  4. q.then(() => console.log('Update 2'));
  5. q.then(() => console.log('Update 3'));

Don’t mix using callbacks and promises with queries, or you may end up with duplicate operations. That’s because passing a callback to a query function immediately executes the query, and calling then() executes the query again.

Mixing promises and callbacks can lead to duplicate entries in arrays. For example, the below code inserts 2 entries into the tags array, not just 1.

  1. const BlogPost = mongoose.model('BlogPost', new Schema({
  2. title: String,
  3. tags: [String]
  4. }));
  5. // Because there's both `await` **and** a callback, this `updateOne()` executes twice
  6. // and thus pushes the same string into `tags` twice.
  7. const update = { $push: { tags: ['javascript'] } };
  8. await BlogPost.updateOne({ title: 'Introduction to Promises' }, update, (err, res) => {
  9. console.log(res);
  10. });

References to other documents

There are no joins in MongoDB but sometimes we still want references to documents in other collections. This is where population comes in. Read more about how to include documents from other collections in your query results here.

Streaming

You can stream query results from MongoDB. You need to call the Query#cursor() function to return an instance of QueryCursor.

  1. const cursor = Person.find({ occupation: /host/ }).cursor();
  2. for (let doc = await cursor.next(); doc != null; doc = await cursor.next()) {
  3. console.log(doc); // Prints documents one at a time
  4. }

Iterating through a Mongoose query using async iterators also creates a cursor.

  1. for await (const doc of Person.find()) {
  2. console.log(doc); // Prints documents one at a time
  3. }

Cursors are subject to cursor timeouts. By default, MongoDB will close your cursor after 10 minutes and subsequent next() calls will result in a MongoServerError: cursor id 123 not found error. To override this, set the noCursorTimeout option on your cursor.

  1. // MongoDB won't automatically close this cursor after 10 minutes.
  2. const cursor = Person.find().cursor().addCursorFlag('noCursorTimeout', true);

However, cursors can still time out because of session idle timeouts. So even a cursor with noCursorTimeout set will still time out after 30 minutes of inactivity. You can read more about working around session idle timeouts in the MongoDB documentation.

Versus Aggregation

Aggregation can do many of the same things that queries can. For example, below is how you can use aggregate() to find docs where name.last = 'Ghost':

  1. const docs = await Person.aggregate([{ $match: { 'name.last': 'Ghost' } }]);

However, just because you can use aggregate() doesn’t mean you should. In general, you should use queries where possible, and only use aggregate() when you absolutely need to.

Unlike query results, Mongoose does not hydrate() aggregation results. Aggregation results are always POJOs, not Mongoose documents.

  1. const docs = await Person.aggregate([{ $match: { 'name.last': 'Ghost' } }]);
  2. docs[0] instanceof mongoose.Document; // false

Also, unlike query filters, Mongoose also doesn’t cast aggregation pipelines. That means you’re responsible for ensuring the values you pass in to an aggregation pipeline have the correct type.

  1. const doc = await Person.findOne();
  2. const idString = doc._id.toString();
  3. // Finds the `Person`, because Mongoose casts `idString` to an ObjectId
  4. const queryRes = await Person.findOne({ _id: idString });
  5. // Does **not** find the `Person`, because Mongoose doesn't cast aggregation
  6. // pipelines.
  7. const aggRes = await Person.aggregate([{ $match: { _id: idString } }])

Next Up

Now that we’ve covered Queries, let’s take a look at Validation.