0% found this document useful (0 votes)
15 views6 pages

7 Explanation

The document explains the usage of the `$exists` and `$ne` operators in MongoDB queries, detailing their syntax and purpose. It provides an example query that filters documents based on the presence and non-empty value of the `host.picture_url` field, while projecting specific fields in the output. The example illustrates how the query operates on sample data and highlights which documents are returned based on the specified conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

7 Explanation

The document explains the usage of the `$exists` and `$ne` operators in MongoDB queries, detailing their syntax and purpose. It provides an example query that filters documents based on the presence and non-empty value of the `host.picture_url` field, while projecting specific fields in the output. The example illustrates how the query operates on sample data and highlights which documents are returned based on the specified conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Operators in Detail

1. `$exists`
- Purpose: The `$exists` operator checks for the presence of a field in a document.
- Syntax:
{field: {$exists: <boolean> } }

- `<boolean>`: Set to `true` if the field must exist, and `false` if the field must not exist.
- Usage:
- `$exists: true` ensures that the field is present in the document.
- `$exists: false` ensures that the field is not present in the document.

2. `$ne`
- Purpose: The `$ne` operator stands for "not equal." It matches all documents where the
specified field's value is not equal to the provided value.
- Syntax:
{field: {$ne: <value>} }

- `<value>`: The value that the field should not be equal to.
- Usage:
- `$ne: <value>` ensures that the field's value does not equal `<value>`.

Query Explanation
The query you provided uses both operators to filter documents where the host has a picture
URL, and then projects specific fields from those documents.
Query:
db.listingsAndReviews.find(
{
"host.picture_url": { $exists: true, $ne: "" }
},
{
listing_url: 1,
name: 1,
address: 1,
"host.picture_url": 1
}
).pretty()

Detailed Breakdown:

1. Filter Condition:

{
"host.picture_url": { $exists: true, $ne: "" }
}

- "host.picture_url": This specifies the field we are checking within the `host` sub-
document.
- $exists: true: Ensures that the `host.picture_url` field exists in the document.
- For example, if a document has a `host` sub-document with a `picture_url` field, it will
pass this check.
- $ne: "": Ensures that the `host.picture_url` field is not an empty string.
- This means that even if the `picture_url` field exists, it must contain a non-empty string
to pass this check.
- Combined, this filter selects documents where the `host.picture_url` field both exists and
is not an empty string.

2. Projection:
{
listing_url: 1,
name: 1,
address: 1,
"host.picture_url": 1
}

- This part of the query specifies which fields to include in the result set.
- `1` indicates that the field should be included in the output.
- Fields included:
- `listing_url`
- `name`
- `address`
- `host.picture_url`
- Other fields in the documents will be excluded from the output.

3. .pretty() Method:
- The `.pretty()` method formats the output in a more readable way with proper indentation
and line breaks.

Example Walkthrough

Given the sample data:

json
{
"listing_url": "http://www.example.com/listing/123456",
"name": "Beautiful Apartment",
"address": {
"street": "123 Main Street",
"suburb": "Central",
"city": "Metropolis",
"country": "Wonderland"
},
"host": {
"name": "Alice",
"picture_url": "http://www.example.com/images/host/host123.jpg"
}
},
{
"listing_url": "http://www.example.com/listing/654321",
"name": "Cozy Cottage",
"address": {
"street": "456 Another St",
"suburb": "North",
"city": "Smallville",
"country": "Wonderland"
},
"host": {
"name": "Bob",
"picture_url": ""
}
},
{
"listing_url": "http://www.example.com/listing/789012",
"name": "Modern Condo",
"address": {
"street": "789 Side Road",
"suburb": "East",
"city": "Gotham",
"country": "Wonderland"
},
"host": {
"name": "Charlie",
"picture_url": "http://www.example.com/images/host/host789.jpg"
}
}

Running the query:


db.listingsAndReviews.find(
{
"host.picture_url": { $exists: true, $ne: "" }
},
{
listing_url: 1,
name: 1,
address: 1,
"host.picture_url": 1
}
).pretty()

Result:
This query will return:
json
{
"listing_url": "http://www.example.com/listing/123456",
"name": "Beautiful Apartment",
"address": {
"street": "123 Main Street",
"suburb": "Central",
"city": "Metropolis",
"country": "Wonderland"
},
"host": {
"picture_url": "http://www.example.com/images/host/host123.jpg"
}
},
{
"listing_url": "http://www.example.com/listing/789012",
"name": "Modern Condo",
"address": {
"street": "789 Side Road",
"suburb": "East",
"city": "Gotham",
"country": "Wonderland"
},
"host": {
"picture_url": "http://www.example.com/images/host/host789.jpg"
}
}

- Excluded:
- The document with `name: "Cozy Cottage"` because its `host.picture_url` field is an empty
string, failing the `$ne: ""` condition.

You might also like