2 Ways to Limit the Documents Returned in MongoDB

When you run queries In MongoDB, you have the option of limiting the documents returned. This is where you specify a maximum number of documents to be returned by the operation.

For example, if an operation would normally return 2,000 documents, but you specify a limit of 1,000, then only 1,000 documents are returned. However, if the operation would only return say, 500 documents, then all 500 are returned (because they don’t breach the 1,000 document limit).

This is a bit like using the TOP clause in SQL Server, or the LIMIT clause in MySQL, MariaDB, PostgreSQL, and SQLite.

Read more

4 Ways to Delete a Document in MongoDB

In MongoDB, if you need to delete a document in the mongo shell, you can use:

  • The db.collection.remove() method
  • The delete command
  • The db.collection.deleteOne() method
  • The db.collection.deleteMany() method

This article provides examples of each.

Read more

Find Out if a Query uses an Index in MongoDB

In MongoDB, you can use the cursor.explain() method or the db.collection.explain() method to determine whether or not a query uses an index.

These methods enable you to view the query plan for the query, which includes whether or not it uses an index.

Read more

2 Ways to Delete a Database in MongoDB

If you no longer need a database in MongoDB you can delete it.

Technically speaking, the term normally used for deleting a database is drop. To drop a database is to delete it.

In any case, below are two ways you can drop/delete a database in the mongo shell.

Read more