Write Operations (Insert, Update, Replace, Delete)

Perform write operations to insert new documents into a collection, update existing document or documents in a collection, replace an existing document in a collection, or delete existing document or documents from a collection.

Prerequisites

  • The example below requires a restaurants collection in the test database. To create and populate the collection, follow the directions in github.

  • Include the following import statements:

     import org.mongodb.scala._
     import org.mongodb.scala.model._
     import org.mongodb.scala.model.Filters._
     import org.mongodb.scala.model.Updates._
     import org.mongodb.scala.model.UpdateOptions
     import org.mongodb.scala.bson.BsonObjectId
    
important

This guide uses the Observable implicits as covered in the Quick Start Primer.

Connect to a MongoDB Deployment

Connect to a MongoDB deployment and declare and define a MongoDatabase instance.

For example, include the following code to connect to a standalone MongoDB deployment running on localhost on port 27017 and define database to refer to the test database and collection to refer to the restaurants collection:

val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("test")
val collection: MongoCollection[Document] = database.getCollection("restaurants")

For additional information on connecting to MongoDB, see Connect to MongoDB.

Insert New Document

To insert a single document into the collection, you can use the collection’s insertOne() method.

// Note: this code example uses a custom implicit helper referenced in the Quick Start Primer
val document = Document("name" -> "Café Con Leche" , 
                        "contact" -> Document("phone" -> "228-555-0149", 
                                              "email" -> "cafeconleche@example.com", 
                                              "location" -> Seq(-73.92502, 40.8279556)),
                        "stars" -> 3, "categories" -> Seq("Bakery", "Coffee", "Pastries"))

collection.insertOne(document).printResults()
Note

If no top-level _id field is specified in the document, the Java driver automatically adds the _id field to the inserted document.

Insert Multiple Documents

To add multiple documents, you can use the collection’s insertMany() method, which takes a list of documents to insert.

The following example inserts two documents to the collection:

// Note: this code example uses a custom implicit helper referenced in the Quick Start Primer
val doc1 = Document("name" -> "Amarcord Pizzeria" , 
                    "contact" -> Document("phone" -> "264-555-0193", 
                                          "email" -> "amarcord.pizzeria@example.net", 
                                          "location" -> Seq(-73.88502, 40.749556)),
                    "stars" -> 2, "categories" -> Seq("Pizzeria", "Italian", "Pasta"))

val doc2 = Document("name" -> "Blue Coffee Bar" , 
                    "contact" -> Document("phone" -> "604-555-0102", 
                                          "email" -> "bluecoffeebar@example.com", 
                                          "location" -> Seq(-73.97902, 40.8479556)),
                    "stars" -> 5, "categories" -> Seq("Coffee", "Pastries"))

collection.insertMany(Seq(doc1, doc2)).printResults()
Note

If no top-level _id field is specified in the documents, the Java driver automatically adds the _id field to the inserted documents.

Update Existing Documents

To update existing documents in a collection, you can use the collection’s updateOne() or updateMany methods.

Filters

You can pass in a filter document to the methods to specify which documents to update. The filter document specification is the same as for read operations. To facilitate creating filter objects, the Scala driver provides the Filters helper.

To specify an empty filter (i.e. match all documents in a collection), use an empty Document object.

Update Operators

To change a field in a document, MongoDB provides update operators. To specify the modification to perform using the update operators, use an updates document.

To facilitate the creation of updates documents, the Scala driver provides the Updates class.

important

The _id field is immutable; i.e. you cannot change the value of the _id field.

Update a Single Document

The updateOne() method updates at most a single document, even if the filter condition matches multiple documents in the collection.

The following operation on the restaurants collection updates a document whose _id field equals BsonObjectId("57506d62f57802807471dd41").

// Note: this code example uses a custom implicit helper referenced in the Quick Start Primer
collection.updateOne(
                equal("_id", BsonObjectId("57506d62f57802807471dd41")),
                combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")))
          .printResults()

Specifically, the operation uses:

  • Updates.set to set the value of the stars field to 1 and the contact.phone field to "228-555-9999", and

  • Updates.currentDate to modify the lastModified field to the current date. If the lastModified field does not exist, the operator adds the field to the document.

Update Multiple Documents

The updateMany method updates all documents that match the filter condition.

The following operation on the restaurants collection updates all documents whose stars field equals 2.

// Note: this code example uses a custom implicit helper referenced in the Quick Start Primer
collection.updateMany(
              equal("stars", 2),
              combine(set("stars", 0), currentDate("lastModified")))
          .println()

Specifically, the operation uses:

  • Updates.set to set the value of the stars field to 0 , and

  • Updates.currentDate to modify the lastModified field to the current date. If the lastModified field does not exist, the operator adds the field to the document.

Update Options

With the updateOne() and updateMany methods, you can include an UpdateOptions document to specify the upsert option or the bypassDocumentationValidation option.

// Note: this code example uses a custom implicit helper referenced in the Quick Start Primer
collection.updateOne(
                equal("_id", 1),
                combine(set("name", "Fresh Breads and Tulips"), currentDate("lastModified")),
                UpdateOptions().upsert(true).bypassDocumentValidation(true))
          .printResults()

Replace an Existing Document

To replace an existing document in a collection, you can use the collection’s replaceOne method.

important

The _id field is immutable; i.e. you cannot replace the _id field value.

Filters

You can pass in a filter document to the method to specify which document to replace. The filter document specification is the same as for read operations. To facilitate creating filter objects, the Scala driver provides the Filters helper.

To specify an empty filter (i.e. match all documents in a collection), use an empty Document object.

The replaceOne method replaces at most a single document, even if the filter condition matches multiple documents in the collection.

Replace a Document

To replace a document, pass a new document to the replaceOne method.

important

The replacement document can have different fields from the original document. In the replacement document, you can omit the _id field since the _id field is immutable; however, if you do include the _id field, you cannot specify a different value for the _id field.

The following operation on the restaurants collection replaces the document whose _id field equals BsonObjectId("57506d62f57802807471dd41").

// Note: this code example uses a custom implicit helper referenced in the Quick Start Primer
collection.replaceOne(
                equal("_id", BsonObjectId("57506d62f57802807471dd41")),
                Document("name" -> "Green Salads Buffet", "contact" -> "TBD",
                         "categories" -> Seq("Salads", "Health Foods", "Buffet")))
         .printResults()

See also Update a Document.

Update Options

With the replaceOne, you can include an UpdateOptions document to specify the upsert option or the bypassDocumentationValidation option.

// Note: this code example uses a custom implicit helper referenced in the Quick Start Primer
collection.replaceOne(
                equal("name", "Orange Patisserie and Gelateria"),
                Document("stars" -> 5, "contact"  -> "TBD", 
                         "categories" -> Seq("Cafe", "Pastries", "Ice Cream")),
                UpdateOptions().upsert(true).bypassDocumentValidation(true))
          .printResults()

Delete Documents

To delete documents in a collection, you can use the deleteOne and deleteMany methods.

Filters

You can pass in a filter document to the methods to specify which documents to delete. The filter document specification is the same as for read operations. To facilitate creating filter objects, the Scala driver provides the Filters helper.

To specify an empty filter (i.e. match all documents in a collection), use an empty Document object.

Delete a Single Document

The deleteOne method deletes at most a single document, even if the filter condition matches multiple documents in the collection.

The following operation on the restaurants collection deletes a document whose _id field equals ObjectId("57506d62f57802807471dd41").

collection.deleteOne(equal("_id", new ObjectId("57506d62f57802807471dd41"))).subscribe(new ObservableSubscriber<DeleteResult>())

Delete Multiple Documents

The deleteMany method deletes all documents that match the filter condition.

The following operation on the restaurants collection deletes all documents whose stars field equals 4.

// Note: this code example uses a custom implicit helper referenced in the Quick Start Primer
collection.deleteMany(equal("stars", 4)).printResults()

See also Drop a Collection.

Write Concern

Write concern describes the level of acknowledgement requested from MongoDB for write operations.

Applications can configure write concern at three levels:

  • In a MongoClient()

      val mongoClient: MongoClient = MongoClient(MongoClientSettings.builder()
                                                    .applyConnectionString(ConnectionString("mongodb://host1,host2"))
                                                    .writeConcern(WriteConcern.MAJORITY)
                                                    .build())
    
      val mongoClientt = MongoClient("mongodb://host1:27017,host2:27017/?w=majority")
    
  • In a MongoDatabase via its withWriteConcern method, as in the following example:

     val database = mongoClient.getDatabase("test").withWriteConcern(WriteConcern.MAJORITY)
    
  • In a MongoCollection via its withWriteConcern method, as in the following example:

     val collection = database.getCollection("restaurants").withWriteConcern(WriteConcern.MAJORITY)
    

MongoDatabase and MongoCollection instances are immutable. Calling .withWriteConcern() on an existing MongoDatabase or MongoCollection instance returns a new instance and does not affect the instance on which the method is called.

For example, in the following, the collWithWriteConcern instance has the write concern of majority whereas the write concern of the collection is unaffected.

val collWithWriteConcern = collection.withWriteConcern(WriteConcern.MAJORITY)

You can build MongoClientSettings, MongoDatabase, or MongoCollection to include a combination of write concern, read concern, and read preference.

For example, the following sets all three at the collection level:

val collection = database.getCollection("restaurants")
                         .withReadPreference(ReadPreference.primary())
                         .withReadConcern(ReadConcern.MAJORITY)
                         .withWriteConcern(WriteConcern.MAJORITY)