Sling Academy
Home/MongoDB/Understanding MongoDB $setOnInsert operator (with examples)

Understanding MongoDB $setOnInsert operator (with examples)

Last updated: February 03, 2024

Introduction

MongoDB, a leading NoSQL database, offers a flexible schema that allows developers to efficiently manage and manipulate data. One of its powerful features is the $setOnInsert operator, which is specifically designed for use with the upsert operation. This operator specifies the fields to add to a document only if the operation results in an insertion. This guide delves into the $setOnInsert operator, demonstrating its utility through a variety of examples.

Understanding $setOnInsert

The $setOnInsert operator in MongoDB plays a crucial role in distinguishing between insert and update operations during an upsert. When an upsert query results in a new document insertion, the $setOnInsert fields are added. However, if the operation finds an existing document and updates it, the $setOnInsert modifications are ignored. Understanding this operator helps achieve precision in data manipulation, ensuring that certain fields are only added when a new document is actually created.

Basic Example

db.collection.update(
  { username: "johndoe" },
  {
    $setOnInsert: {
      createdAt: new Date(),
      email: "[email protected]"
    }
  },
  { upsert: true }
);

In this basic example, if a document with the username “johndoe” does not exist, the update operation will insert a new document with the specified createdAt and email fields. If the document exists, it performs an update operation without touching the createdAt and email fields.

Intermediate Example

db.collection.update(
  { _id: ObjectId("507f191e810c19729de860ea") },
  {
    $set: { active: true },
    $setOnInsert: { registeredAt: new Date() }
  },
  { upsert: true }
);

This example demonstrates how to use $setOnInsert along with $set. In this scenario, if the document is inserted, it will have both the registeredAt field and the active state set. If the document is an update instead of an insert, only the active field will be changed.

Advanced Example

db.collection.update(
  { "profile.username": "techguru" },
  {
    $set: { "profile.lastAccess": new Date() },
    $addToSet: { tags: "expert" },
    $setOnInsert: {
      "profile.creationDate": new Date(),
      "accountStatus": "active",
      email: "[email protected]"
    }
  },
  { upsert: true }
);

This more complex example further explores the use of $setOnInsert in conjunction with other update operators like $set and $addToSet. Here, if the upsert leads to an insertion, the document will be filled with a creation date, account status, and email, along with the last access time and tags. If an update occurs, only the last access time and tags are modified.

Tips for Using $setOnInsert

  • Always couple $setOnInsert with the upsert: true option to differentiate between insertions and updates effectively.
  • Utilize $setOnInsert to initialize fields that should only exist when a document is first created, such as timestamps, default settings, or identifiers.
  • Pair $setOnInsert with other update operators for complex operations, balancing between initial setup and updates.

Conclusion

The $setOnInsert operator is a sophisticated tool in MongoDB that enables nuanced control over document fields during upsert operations. By carefully applying this operator, developers can ensure that certain fields are only introduced at the creation of a document, maintaining both the efficiency and integrity of the database.

Next Article: Manipulating array fields in MongoDB (with examples)

Previous Article: MongoDB: How to rename a collection in a database

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)