Cara menggunakan updatemany mongodb node

Use the db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 5 method to update multiple documents that matches with the specified filter criteria in a collection.

Syntax:

db.collection.updateMany(filter, document, options)

Parameters:

  1. filter: The selection criteria for the update, same as find() method.
  2. document: A document or pipeline that contains modifications to apply.
  3. options: Optional. May contains options for update behavior. It includes upsert, writeConcern, collation, etc.

In the above syntax, db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 6 points to the current database, db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 7 points is an existing collection name.

To demonstrate the update operation, insert the following sample documents in the db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 8 collection.

Sample Data

Copy

db.employees.insertMany([ { _id:1, firstName: "John", lastName: "King", email: "[email protected]", salary: 5000 }, { _id:2, firstName: "Sachin", lastName: "T", email: "[email protected]", salary: 8000 }, { _id:3, firstName: "James", lastName: "Bond", email: "[email protected]", salary: 7500 }, { _id:4, firstName: "Steve", lastName: "J", email: "[email protected]", salary: 7000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "[email protected]", salary: 4500 }, { _id:6, firstName: "Amitabh", lastName: "B", email: "[email protected]", salary: 7000 } ])

The following modifies matching documents using the db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 2 method in db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 8 collection.

Example: updateMany()

Copy

db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }})

Output

{ acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 }

In the above example, the first parameter is the filter criteria specified as a document, { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } 1 indicates that find documents whose { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } 2 are { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } 3. The second parameter is used to specify fields and values to be modified on the matching document in the { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } 4 format. Use the to specify an action to perform. Here we want to set the value of fields, so use { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } 5 operator to specify fields and updated values in { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } 6 format. { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } 7 modifies the { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } 2 fields of all matching documents to { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } 9.

In the output, db.employees.find() 0 indicates the number of documents that matched with the criteria, and db.employees.find() 1 indicates the number of documents updated.

Now, check whether it has updated a value or not using the db.employees.find() 2 method shown below.

Check Updated Document

Copy

db.employees.find()

Output

[ { _id: 1, firstName: 'John', lastName: 'King', email: '[email protected]', salary: 5000 }, { _id: 2, firstName: 'Sachin', lastName: 'T', email: '[email protected]', salary: 8000 }, { _id: 3, firstName: 'James', lastName: 'Bond', email: '[email protected]', salary: 7500 }, { _id: 4, firstName: 'Steve', lastName: 'J', email: '[email protected]', salary: 8500 }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: '[email protected]', salary: 4500 }, { _id: 6, firstName: 'Amitabh', lastName: 'B', email: '[email protected]', salary: 8500 } ]

The db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 2 method adds the specified field if it does not exist in a matching document. For example, the following will add the db.employees.find() 4 field.

Example: updateMany()

Copy

db.employees.updateMany({firstName:"Steve"}, { $set: {location: "USA"}})

Output

{ acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0 }

Execute the following db.employees.find() 2 method to see the updated data.

Check Updated Document

Copy

db.employees.find({firstName:"Steve"})

Output

{ _id:4, firstName: "Steve", lastName: "J", email: "[email protected]", salary: 8500, location:"USA" }

If you specify an empty filter criteria db.employees.find() 6, then it will update all the documents. The following will update or add db.employees.find() 4 field in all documents.

Example: updateMany()

Copy

db.employees.insertMany([ { _id:1, firstName: "John", lastName: "King", email: "[email protected]", salary: 5000 }, { _id:2, firstName: "Sachin", lastName: "T", email: "[email protected]", salary: 8000 }, { _id:3, firstName: "James", lastName: "Bond", email: "[email protected]", salary: 7500 }, { _id:4, firstName: "Steve", lastName: "J", email: "[email protected]", salary: 7000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "[email protected]", salary: 4500 }, { _id:6, firstName: "Amitabh", lastName: "B", email: "[email protected]", salary: 7000 } ]) 0

Output

db.employees.insertMany([ { _id:1, firstName: "John", lastName: "King", email: "[email protected]", salary: 5000 }, { _id:2, firstName: "Sachin", lastName: "T", email: "[email protected]", salary: 8000 }, { _id:3, firstName: "James", lastName: "Bond", email: "[email protected]", salary: 7500 }, { _id:4, firstName: "Steve", lastName: "J", email: "[email protected]", salary: 7000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "[email protected]", salary: 4500 }, { _id:6, firstName: "Amitabh", lastName: "B", email: "[email protected]", salary: 7000 } ]) 1

Use the db.employees.find() 8 update operator to increase the value of the field by the specified amount. The following increases the { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } 2 by [ { _id: 1, firstName: 'John', lastName: 'King', email: '[email protected]', salary: 5000 }, { _id: 2, firstName: 'Sachin', lastName: 'T', email: '[email protected]', salary: 8000 }, { _id: 3, firstName: 'James', lastName: 'Bond', email: '[email protected]', salary: 7500 }, { _id: 4, firstName: 'Steve', lastName: 'J', email: '[email protected]', salary: 8500 }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: '[email protected]', salary: 4500 }, { _id: 6, firstName: 'Amitabh', lastName: 'B', email: '[email protected]', salary: 8500 } ] 0 whose salary is { acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 } 9.

Example: $inc Operator

Copy

db.employees.insertMany([ { _id:1, firstName: "John", lastName: "King", email: "[email protected]", salary: 5000 }, { _id:2, firstName: "Sachin", lastName: "T", email: "[email protected]", salary: 8000 }, { _id:3, firstName: "James", lastName: "Bond", email: "[email protected]", salary: 7500 }, { _id:4, firstName: "Steve", lastName: "J", email: "[email protected]", salary: 7000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "[email protected]", salary: 4500 }, { _id:6, firstName: "Amitabh", lastName: "B", email: "[email protected]", salary: 7000 } ]) 2

Output

{ acknowledged: true, insertedId: null, matchedCount: 2, modifiedCount: 2, upsertedCount: 0 }

Update Multiple Fields

You can also specify multiple fields to update. The following updates [ { _id: 1, firstName: 'John', lastName: 'King', email: '[email protected]', salary: 5000 }, { _id: 2, firstName: 'Sachin', lastName: 'T', email: '[email protected]', salary: 8000 }, { _id: 3, firstName: 'James', lastName: 'Bond', email: '[email protected]', salary: 7500 }, { _id: 4, firstName: 'Steve', lastName: 'J', email: '[email protected]', salary: 8500 }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: '[email protected]', salary: 4500 }, { _id: 6, firstName: 'Amitabh', lastName: 'B', email: '[email protected]', salary: 8500 } ] 2 and [ { _id: 1, firstName: 'John', lastName: 'King', email: '[email protected]', salary: 5000 }, { _id: 2, firstName: 'Sachin', lastName: 'T', email: '[email protected]', salary: 8000 }, { _id: 3, firstName: 'James', lastName: 'Bond', email: '[email protected]', salary: 7500 }, { _id: 4, firstName: 'Steve', lastName: 'J', email: '[email protected]', salary: 8500 }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: '[email protected]', salary: 4500 }, { _id: 6, firstName: 'Amitabh', lastName: 'B', email: '[email protected]', salary: 8500 } ] 3 fields.

Example: Update Multiple Fields

Copy

db.employees.insertMany([ { _id:1, firstName: "John", lastName: "King", email: "[email protected]", salary: 5000 }, { _id:2, firstName: "Sachin", lastName: "T", email: "[email protected]", salary: 8000 }, { _id:3, firstName: "James", lastName: "Bond", email: "[email protected]", salary: 7500 }, { _id:4, firstName: "Steve", lastName: "J", email: "[email protected]", salary: 7000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "[email protected]", salary: 4500 }, { _id:6, firstName: "Amitabh", lastName: "B", email: "[email protected]", salary: 7000 } ]) 4

Output

{ acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0 }

Execute the following db.employees.find() 2 method to see the updated data.

Check Updated Document

Copy

db.employees.insertMany([ { _id:1, firstName: "John", lastName: "King", email: "[email protected]", salary: 5000 }, { _id:2, firstName: "Sachin", lastName: "T", email: "[email protected]", salary: 8000 }, { _id:3, firstName: "James", lastName: "Bond", email: "[email protected]", salary: 7500 }, { _id:4, firstName: "Steve", lastName: "J", email: "[email protected]", salary: 7000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "[email protected]", salary: 4500 }, { _id:6, firstName: "Amitabh", lastName: "B", email: "[email protected]", salary: 7000 } ]) 6

Output

db.employees.insertMany([ { _id:1, firstName: "John", lastName: "King", email: "[email protected]", salary: 5000 }, { _id:2, firstName: "Sachin", lastName: "T", email: "[email protected]", salary: 8000 }, { _id:3, firstName: "James", lastName: "Bond", email: "[email protected]", salary: 7500 }, { _id:4, firstName: "Steve", lastName: "J", email: "[email protected]", salary: 7000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "[email protected]", salary: 4500 }, { _id:6, firstName: "Amitabh", lastName: "B", email: "[email protected]", salary: 7000 } ]) 7

The db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 2 method does not update any documents if no matching documents found. For example, the following will not update any documents.

Example: updateMany()

Copy

db.employees.insertMany([ { _id:1, firstName: "John", lastName: "King", email: "[email protected]", salary: 5000 }, { _id:2, firstName: "Sachin", lastName: "T", email: "[email protected]", salary: 8000 }, { _id:3, firstName: "James", lastName: "Bond", email: "[email protected]", salary: 7500 }, { _id:4, firstName: "Steve", lastName: "J", email: "[email protected]", salary: 7000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "[email protected]", salary: 4500 }, { _id:6, firstName: "Amitabh", lastName: "B", email: "[email protected]", salary: 7000 } ]) 8

Output

db.employees.insertMany([ { _id:1, firstName: "John", lastName: "King", email: "[email protected]", salary: 5000 }, { _id:2, firstName: "Sachin", lastName: "T", email: "[email protected]", salary: 8000 }, { _id:3, firstName: "James", lastName: "Bond", email: "[email protected]", salary: 7500 }, { _id:4, firstName: "Steve", lastName: "J", email: "[email protected]", salary: 7000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "[email protected]", salary: 4500 }, { _id:6, firstName: "Amitabh", lastName: "B", email: "[email protected]", salary: 7000 } ]) 9

Upsert - Add if not Exist

Specify [ { _id: 1, firstName: 'John', lastName: 'King', email: '[email protected]', salary: 5000 }, { _id: 2, firstName: 'Sachin', lastName: 'T', email: '[email protected]', salary: 8000 }, { _id: 3, firstName: 'James', lastName: 'Bond', email: '[email protected]', salary: 7500 }, { _id: 4, firstName: 'Steve', lastName: 'J', email: '[email protected]', salary: 8500 }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: '[email protected]', salary: 4500 }, { _id: 6, firstName: 'Amitabh', lastName: 'B', email: '[email protected]', salary: 8500 } ] 6 as a third parameter in the [ { _id: 1, firstName: 'John', lastName: 'King', email: '[email protected]', salary: 5000 }, { _id: 2, firstName: 'Sachin', lastName: 'T', email: '[email protected]', salary: 8000 }, { _id: 3, firstName: 'James', lastName: 'Bond', email: '[email protected]', salary: 7500 }, { _id: 4, firstName: 'Steve', lastName: 'J', email: '[email protected]', salary: 8500 }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: '[email protected]', salary: 4500 }, { _id: 6, firstName: 'Amitabh', lastName: 'B', email: '[email protected]', salary: 8500 } ] 7 method. The [ { _id: 1, firstName: 'John', lastName: 'King', email: '[email protected]', salary: 5000 }, { _id: 2, firstName: 'Sachin', lastName: 'T', email: '[email protected]', salary: 8000 }, { _id: 3, firstName: 'James', lastName: 'Bond', email: '[email protected]', salary: 7500 }, { _id: 4, firstName: 'Steve', lastName: 'J', email: '[email protected]', salary: 8500 }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: '[email protected]', salary: 4500 }, { _id: 6, firstName: 'Amitabh', lastName: 'B', email: '[email protected]', salary: 8500 } ] 8 adds a new document if the matching document does not found.

Example: Upsert

Copy

db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 0

Output

db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 1

In the above example, MongoDB adds a new document with new [ { _id: 1, firstName: 'John', lastName: 'King', email: '[email protected]', salary: 5000 }, { _id: 2, firstName: 'Sachin', lastName: 'T', email: '[email protected]', salary: 8000 }, { _id: 3, firstName: 'James', lastName: 'Bond', email: '[email protected]', salary: 7500 }, { _id: 4, firstName: 'Steve', lastName: 'J', email: '[email protected]', salary: 8500 }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: '[email protected]', salary: 4500 }, { _id: 6, firstName: 'Amitabh', lastName: 'B', email: '[email protected]', salary: 8500 } ] 9, because it cannot find a document with the db.employees.updateMany({firstName:"Steve"}, { $set: {location: "USA"}}) 0.

Update Operators

The following table lists the update operators which can be used with the db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 4 and db.employees.updateMany({ salary:7000 }, { $set: { salary:8500 }}) 2 methods.

MethodDescription$currentDateSets the value of a field to current date, either as a Date or a Timestamp.$incIncrements the value of the field by the specified amount.$minOnly updates the field if the specified value is less than the existing field value.$maxOnly updates the field if the specified value is greater than the existing field value.$mulMultiplies the value of the field by the specified amount.$renameRenames a field.$setSets the value of a field in a document.$setOnInsertSets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.$unsetRemoves the specified field from a document.

Postingan terbaru

LIHAT SEMUA