JIYIK CN >

Current Location:Home > Learning > DATABASE > MongoDB >

MongoDB query nested objects

Author:JIYIK Last Updated:2025/04/27 Views:

In this article, we will learn how to query nested objects in MongoDB.


Querying nested objects in MongoDB

MongoDB provides read operations to retrieve embedded or nested data from a collection or to search for embedded or nested documents.

We can perform read operations with db.collection.find()the method. This method selects a document from an embedded or nested collection and returns the cursor to that document.

grammar:

>db.collection.find($filter, $projection)

We have two options in the find() method: $filter and $project, which are briefly described below.

  • $filter Option

    You can use document type parameters to exclude documents. Also, if you want to access every document in the collection, you can use it with empty document or without these parameters.

  • $project Options

    Using this parameter, we can fetch only specific fields that satisfy the query filter provided for the document. But of course, you can also omit this parameter if you want to fetch all the fields of the document.

This method returns a cursor to the documents that match the provided query criteria. The find() method returns a pointer to the document as you use it because it returns the document as you use it.

Let us understand this concept with the following example:

> db.users.find({address: {country: "United Kingdom"}}).pretty()
> db.users.find({address.country: "United Kingdom"}).pretty()

The example code above uses a nifty technique to target all users from the United Kingdom.

The main difference between the two methods is that the earlier address check includes only the fields { country: ... }, while the latter takes the address into account.

Other fields in the address or the lack of other fields in the address have no effect on the country field. For example, after running the above code in MongoShell, you will see the following output.

Output:

{ "_id" : ObjectId("54f612b6029b47909a90cesd"), "email" : "johndoe@example.com", "comment" : "This is the first user in the collection.", "country" : "United Kingdom" }

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

$unset operator in MongoDB

Publish Date:2025/04/27 Views:77 Category:MongoDB

This article will discuss how the $unset operator works in MongoDB. Additionally, we will demonstrate the use of this operator to remove a field from all documents in a MongoDB collection. $unset operator in MongoDB $unset is an operator us

Compass Filters in MongoDB

Publish Date:2025/04/27 Views:132 Category:MongoDB

This short article will cover the various ways to use Compass filters in MongoDB . Compass Filters in MongoDB MongoDB has a GUI called Compass . It is also known as MongoDB GUI. Users can use MongoDB to inspect the contents of their stored

Sorting by timestamp in MongoDB

Publish Date:2025/04/27 Views:54 Category:MongoDB

This article will introduce various methods of sorting timestamps in MongoDB. Sorting by timestamp in MongoDB sort() The method will sort documents in MongoDB. The method accepts a document containing a list of fields and the order in which

Deleting a user from a database in MongoDB

Publish Date:2025/04/27 Views:50 Category:MongoDB

This article will explain how to delete a user from a MongoDB database. In addition, we will see an example to make the topic easier to understand. Deleting a User from a MongoDB Database Sometimes we need to remove a particular user from t

Deleting items by ID in MongoDB

Publish Date:2025/04/27 Views:158 Category:MongoDB

Sometimes we need to delete data from a database based on specified criteria. Unlike other SQL databases, MongoDB does not include SQL queries for this purpose. Instead, it uses commands. This article will discuss how to delete documents ba

Using ISODate for date queries in MongoDB

Publish Date:2025/04/27 Views:77 Category:MongoDB

This MongoDB tutorial article will explain Date() the methods. This article introduces different ways to query using dates. Date() Method in MongoDB Date() The method returns the date as a string or a Date object. In the MongoDB shell or mo

Return unique values in MongoDB

Publish Date:2025/04/27 Views:189 Category:MongoDB

In this article, we will address how to use the MongoDB distinct() method to return unique values. In addition, returning unique values ​​in arrays and fields is discussed. In MongoDB, sometimes you may want to present or return unique

Querying documents with array size greater than 1 in MongoDB

Publish Date:2025/04/27 Views:154 Category:MongoDB

When working with projects where you need to validate the size of an array or find elements whose size is greater or less than a certain length, you might use MongoDB operators such as $size, $where, $exists, etc. The methods discussed belo

Case insensitive query in MongoDB

Publish Date:2025/04/27 Views:167 Category:MongoDB

In this article, case insensitive queries are discussed briefly and in detail. Also, case insensitive search queries are explained in detail. This article discusses the following topics. Case-insensitive search Improve case-insensitive regu

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial