All cheatsheetsOpen MongoDB editor

MongoDB Online Editor

Practice MongoDB queries online for free using mongo shell syntax. CodeCompiler runs a MongoDB-compatible query engine in your browser: insert, find with query operators and projections, update operators, delete and full aggregation pipelines. Each run starts with an empty database.

Insert documents

db.books.insertMany([
  { title: "Clean Code", year: 2008, tags: ["craft"] },
  { title: "SICP", year: 1985, tags: ["lisp", "classic"] }
]);

Find with operators and projection

db.books.find({ year: { $lt: 2000 } }, { _id: 0, title: 1 });
db.books.find({ tags: "classic" }).sort({ year: -1 }).limit(5);

Update and delete

db.books.updateOne({ title: "SICP" }, { $set: { edition: 2 }, $push: { tags: "mit" } });
db.books.deleteMany({ year: { $lt: 1990 } });

Aggregation pipeline

db.books.aggregate([
  { $unwind: "$tags" },
  { $group: { _id: "$tags", count: { $sum: 1 } } },
  { $sort: { count: -1 } }
]);

Printing

Every statement's result is printed automatically, like in mongosh. Use print() or printjson() for custom output.