MongoDB Tutorial

  • Home
  • Getting Started
  • CRUD
  • Indexes
Home / MongoDB CRUD / MongoDB $ne

MongoDB $ne

Summary: in this tutorial, you’ll learn how to use the MongoDB $ne operator to query documents from a collection.

Introduction to MongoDB $ne operator

The $ne is a comparison query operator that allows you to select documents where the value of a filed is not equal to a specified value. It also includes documents that don’t contain the field.

The $ne is called the inequality operator. Here is the syntax of the $ne operator:

{ field: {$ne: value}}

MongoDB $ne operator examples

We’ll use the following products collection:

db.products.insertMany([ { "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]}, { "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]}, { "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]}, { "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]}, { "_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]} , { "_id" : 6, "name" : "xWidget", "spec" : { "ram" : 64, "screen" : 9.7, "cpu" : 3.66 },"color":["black"],"storage":[1024]} ])

1) Using the $ne operator to select documents where the value of a field is greater than a specified value

The following example uses the $ne operator to select documents from the products collection where the price is not equal to 899:

db.products.find({ price: { $ne: 899 } }, { name: 1, price: 1 })

It matches the following documents:

{ "_id" : 1, "name" : "xPhone", "price" : 799 } { "_id" : 4, "name" : "SmartPad", "price" : 699 } { "_id" : 5, "name" : "SmartPhone", "price" : 599 } { "_id" : 6, "name" : "xWidget" }

2) Using the $ne operator to check if a field in an embedded document is not equal to a value

The following example uses $ne operator to select documents where the value of the screen field in the spec document is not equal to 9.7:

db.products.find({ "spec.screen": { $ne: 9.7 } }, { name: 1, "spec.screen": 1 })

Output:

{ "_id" : 1, "name" : "xPhone", "spec" : { "screen" : 6.5 } } { "_id" : 2, "name" : "xTablet", "spec" : { "screen" : 9.5 } }

3) Using the $ne operator to check if an array element is not equal to a value

The following example uses the $ne operator to query the products collection to find documents where the array storage does not have any element that equals 128:

db.products.find({ storage: { $ne: 128 } }, { name: 1, storage: 1 });

It matched the following documents:

{ "_id" : 6, "name" : "xWidget", "storage" : [ 1024 ] }

4) Using the $ne operator to check if the value of a field is not equal to a date

The following query uses the $ne operator to find documents from the products collection where the release date is not 2015-01-14:

db.products.find({ releaseDate: { $ne: new ISODate('2015-01-14') } }, { name: 1, releaseDate: 1 });

It returns the documents whose release dates are not 2015-01-14 and also the document that does not include the field releaseDate :

{ "_id" : 1, "name" : "xPhone", "releaseDate" : ISODate("2011-05-14T00:00:00Z") } { "_id" : 2, "name" : "xTablet", "releaseDate" : ISODate("2011-09-01T00:00:00Z") } { "_id" : 4, "name" : "SmartPad", "releaseDate" : ISODate("2020-05-14T00:00:00Z") } { "_id" : 5, "name" : "SmartPhone", "releaseDate" : ISODate("2022-09-14T00:00:00Z") } { "_id" : 6, "name" : "xWidget" }

Summary

  • Use the $ne operator to check if the value of a field is not equal to a specified value.
  • Was this tutorial helpful ?
  • YesNo
Previous MongoDB $lte
Next MongoDB $in

Getting Started

  • What is MongoDB
  • Install MongoDB
  • MongoDB Basics
  • MongoDB Shell
  • MongoDB Data Types

Inserting Documents

  • insertOne
  • insertMany
  • mongoimport

Selecting Documents

  • findOne
  • find
  • Projection: Selecting Returned Fields

Comparison Query Operators

  • $eq: Equal To Operator
  • $lt: Less Than Operator
  • $lte: Less Than or Equal To Operator
  • $gt: Greater Than Operator
  • $gte: Greater Than or Equal To Operator
  • $ne: Not Equal To Operator
  • $in: In Operator
  • $nin: Not In Operator

Logical Query Operators

  • $and: Logical AND Opeartor
  • $or: Logical OR Operator
  • $not: Logical NOT Operator
  • $nor: Logical NOR Operator

Element Query Operators

  • $exists
  • $type

Array Query Operators

  • $size
  • $all
  • $elemMatch

Sorting & Limiting

  • sort(): Sorting documents
  • limit(): Limiting documents

Updating Documents

  • updateOne: Update one Document
  • updateMany: Update Multiple Documents
  • $inc: Increase / Decrease Field Value
  • $min: Update Field Value
  • $max: Update Field Value
  • $mul: Mutiply Field By a Number
  • $unset: Remove Fields
  • $rename: Rename Fields
  • Upsert

Deleting Documents

  • deleteOne
  • deleteMany

About MongoDBTutorial.com

This MongoDB Tutorial helps you master MongoDB quickly.

Recent Tutorials

  • MongoDB Indexes
  • MongoDB limit
  • MongoDB sort
  • MongoDB Upsert
  • MongoDB Projection

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Copyright © 2021 · By mongodbtutorial.org. All Rights Reserved.