Query documents

Find documents with find and MongoDB's query operators.

Find documents with find and MongoDB's query operators. Any node takes the query and spreads it over the ranges it touches.

Requirements

  • A connection with a user that has read on the database.

Find by several fields

JavaScript
db.orders.find({ status: "paid", total: { $gte: 100 } });

Combine conditions

JavaScript
db.orders.find({ $or: [{ status: "pending" }, { total: { $lt: 10 } }] });

Find one

JavaScript
db.orders.findOne({ customer: "Ana Ruiz" });

Check

db.orders.countDocuments({ status: "paid" }) returns the number of paid documents, the same ones the first query returns when it has no total condition.

Common problems

  • The query is slow because no index covers its filter. explain says which plan it used.

See also