How do i search for a document in mongodb?

Docs HomeMongoDB Manual

On this page

  • Select All Documents in a Collection
  • Specify Equality Condition
  • Specify Conditions Using Query Operators
  • Specify AND Conditions
  • Specify OR Conditions
  • Additional Query Tutorials
  • Behavior


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.


This operation corresponds to the following SQL statement:

The following example selects from the inventory collection all documents where the status equals "D":

This operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "D"

The following example retrieves all documents from the inventory collection where status equals either "A" or "D":

Note

Although you can express this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status in ("A", "D")

Refer to the Query and Projection Operators document for the complete list of MongoDB query operators.

A compound query can specify conditions for more than one field in the collection's documents. Implicitly, a logical AND conjunction connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions.

The following example retrieves all documents in the inventory collection where the status equals "A" and qty is less than ($lt) 30:

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" AND qty < 30

See comparison operators for other MongoDB comparison operators.

Using the $or operator, you can specify a compound query that joins each clause with a logical OR conjunction so that the query selects the documents in the collection that match at least one condition.

The following example retrieves all documents in the collection where the status equals "A" or qty is less than ($lt) 30:

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" OR qty < 30

Note

In the following example, the compound query document selects all documents in the collection where the status equals "A" and either qty is less than ($lt) 30 or item starts with the character p:

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")

Note

MongoDB supports regular expressions $regex queries to perform string pattern matches.

For additional query examples, see:

  • Query on Embedded/Nested Documents

  • Query an Array

  • Query an Array of Embedded Documents

  • Project Fields to Return from Query

  • Query for Null or Missing Fields

For reads to replica sets and replica set shards, read concern allows clients to choose a level of isolation for their reads. For more information, see Read Concern.

Docs HomeMongoDB Manual

MongoDB offers a full-text search solution, MongoDB Atlas Search, for data hosted on MongoDB Atlas. Users running self-managed MongoDB deployments have access to on-premises text search capabilities.

For MongoDB Atlas users, MongoDB's Atlas Search supports fine-grained text indexing and a rich query language for fast, relevant search results. To learn more about full-text search indexes and $search queries, see:

  • Atlas Search Aggregation Pipeline Stages

  • Defining Atlas Search Indexes

  • Running Atlas Search Queries

Atlas Search also offers common analyzers for parsing text for full-text search, including support for over 40 language-specific analyzers.

For on-premises (non-Atlas) deployments, MongoDB's text search capability supports query operations that perform a text search of string content. To perform text search, MongoDB uses a text index and the $text operator.

Note

Views do not support text search.

To learn more about text search for on-premises deployments, see:

  • Text Indexes

  • Text Search Operators

MongoDB also supports text search for various languages. See Text Search Languages for a list of supported languages.

How do I search for a specific document in MongoDB?

Find method in MongoDB Query is used to fetch a particular document from the MongoDB collection. There are totally six methods available in Mongo DB by which we can fetch particular records..
find().
findAndModify().
findOne().
findOneAndDelete().
findOneAndReplace().
findOneAndUpdate().

How do I search in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.

How does MongoDB searching work?

Databases store information in logical units. MongoDB, for example, stores units of data, called documents, and groups them together in collections. When you perform a database search, you go through all the collection documents to find a match with the search terms.

How do I see all files in MongoDB?

In Java, you can retrieve all the documents in the current collection using the find() method of the com..
Create a MongoDB client by instantiating the MongoClient class..
Connect to a database using the getDatabase() method..