Introduction to MongoDB $ne Operator

In MongoDB, the $ne operator is used to query documents that are not equal to the specified value. It can be used to query non-specific values in a specific field of a document, or to query non-specific values in a nested document. The $ne operator is often used in combination with other operators to more precisely query documents.

Syntax

The syntax of the $ne operator is as follows:

{
  field: {
    $ne: value
  }
}

Where field represents the name of the field to be queried, and value represents the value to be queried.

Use cases

The $ne operator can be used in the following scenarios:

  • Query non-specific values of a specific field: For example, query all rating records that are not equal to 5.
  • Query non-specific values in nested documents: For example, query all products that have a rating other than 5.

Examples

Here are two examples that demonstrate how to use the $ne operator to query documents that are not equal to a specified value.

Example 1: Querying Non-specific Values of a Specific Field

Assume we have a scores collection that contains score information for multiple students, including their names, course names, and scores. Now we need to query all records whose scores are not equal to 60, we can use the $ne operator to perform the query:

Suppose the following data is in the collection:

{
  "_id": 1,
  "name": "Alice",
  "course": "Math",
  "score": 75
},
{
  "_id": 2,
  "name": "Bob",
  "course": "English",
  "score": 60
},
{
  "_id": 3,
  "name": "Charlie",
  "course": "Science",
  "score": 80
},
{
  "_id": 4,
  "name": "David",
  "course": "History",
  "score": 85
}

We can use the following command to query all records whose scores are not equal to 60:

db.scores.find({ score: { $ne: 60 } })

After executing the command, the result returned is:

{
  "_id": 1,
  "name": "Alice",
  "course": "Math",
  "score": 75
},
{
  "_id": 3,
  "name": "Charlie",
  "course": "Science",
  "score": 80
},
{
  "_id": 4,
  "name": "David",
  "course": "History",
  "score": 85
}

This indicates that only the records whose scores are not equal to 60 are returned in the query result, namely the records of Alice, Charlie, and David.

Example 2: Querying Non-specific Values in Nested Fields

Assume we have a products collection that contains information for multiple products, including product numbers, product names, and prices. Each product also contains a reviews array that stores user reviews for the product. Each review contains a rating field that represents the rating of the review. Now we need to query all products whose ratings are not 5, we can use the $ne operator to perform the query.

The following is sample data:

The following is sample data:

{
  "product_id": "10001",
  "product_name": "iPhone 12",
  "price": 7999,
  "reviews": [
    {"username": "user1", "rating": 4},
    {"username": "user2", "rating": 3},
    {"username": "user3", "rating": 5},
    {"username": "user4", "rating": 4},
  ]
},
{
  "product_id": "10002",
  "product_name": "MacBook Pro",
  "price": 13999,
  "reviews": [
    {"username": "user1", "rating": 5},
    {"username": "user2", "rating": 4},
    {"username": "user3", "rating": 3},
    {"username": "user4", "rating": 4},
  ]
},
{
  "product_id": "10003",
  "product_name": "AirPods",
  "price": 1199,
  "reviews": [
    {"username": "user1", "rating": 5},
    {"username": "user2", "rating": 4},
    {"username": "user3", "rating": 2},
    {"username": "user4", "rating": 3},
  ]
}

The following is the query statement:

db.products.find({ "reviews.rating": { $ne: 5 } })

The following is the query result:

{
  "product_id": "10001",
  "product_name": "iPhone 12",
  "price": 7999,
  "reviews": [
    {"username": "user1", "rating": 4},
    {"username": "user2", "rating": 3},
    {"username": "user4", "rating": 4},
  ]
},
{
  "product_id": "10003",
  "product_name": "AirPods",
  "price": 1199,
  "reviews": [
    {"username": "user2", "rating": 4},
    {"username": "user3", "rating": 2},
    {"username": "user4", "rating": 3},
  ]
}

This query statement returns information for all products with ratings that are not 5.

Conclusion

The $ne operator can be used to query for non-specific values of a specified field, which is suitable for scenarios where specific values need to be excluded. In practical applications, more complex queries can be performed by combining other query conditions, such as using the $and or $or operators. It is important to note that when using the $ne operator, special attention should be paid to cases where the specific value may contain null values, which need to be handled in combination with the $exists operator.