$ne

In MongoDB, the $ne operator is used to filter documents where the value of a specified field is not equal to a specified value.

Usage

To use the $ne comparison operator, include it within the query document as:

{
  field: {
    $ne: value;
  }
}

Example

Let’s say you have a collection called products with documents like:

{ _id: 1, name: "Apple", category: "Fruits" }
{ _id: 2, name: "Banana", category: "Fruits" }
{ _id: 3, name: "Carrot", category: "Vegetables" }

If you want to query all documents where the category is not “Fruits”, you would execute:

db.products.find({ category: { $ne: 'Fruits' } });

The result would be:

{ "_id" : 3, "name" : "Carrot", "category" : "Vegetables" }

Additional Notes

And that’s a brief summary of the $ne operator. Use it when you want to filter documents where a specified field’s value is not equal to another specified value. Happy querying!