MongoDB Tutorial

  • Home
  • Getting Started
  • CRUD
  • Indexes
Home / MongoDB CRUD / How to Use the MongoDB $min Operator to Update Field Values

How to Use the MongoDB $min Operator to Update Field Values

Summary: in this tutorial, you’ll learn how to use the MongoDB $min operator in the update() method to update the values of one or more fields.

Introduction to the MongoDB $min operator

The $min operator is a field update operator that allows you to update the value of a field to a specified value if the specified value is less than (<) the current value of the field.

The $min operator has the following syntax:

{ $min: {<field1>: <value1>, ...} }

If the current value of a field is greater than or equal to the value that you want to update, the $min operator won’t update the value.

If the field doesn’t exist, the $min operator creates the field and sets its value to the specified value.

MongoDB $min operator example

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" : 5.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]} ])

The following example uses the $min operator to update the price of the document _id 5:

db.products.updateOne({ _id: 5 }, { $min: { price: 699 } })

The query found a matching document. However, it didn’t update any:

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 0 }

The reason is that the new value 699 is greater than the current value 599.

The following example uses the $min operator to update the price of the document _id 5:

db.products.updateOne({ _id: 5 }, { $min: { price: 499 } })

In this case, the price field of the document _id 5 is updated to 499:

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

This query verifies the update:

db.products.find({ _id: 5 }, { name: 1, price: 1 })

Output:

{ "_id" : 5, "name" : "SmartPhone", "price" : 499 }

Summary

  • Use the $min operator to update the value of a field to a specified value when the specified value is less than the current field value.
  • Was this tutorial helpful ?
  • YesNo
Previous How to Use MongoDB $inc Operator in update() Method
Next How to Use the MongoDB $max Operator to Update Field Values

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.