Query Optimization / Enhance Read and Write Operations
Code Summary: Optimizing Write Operations by Using Bulk Inserts
Using the bulkWrite
Command
The bulkWrite
command can be used to perform many insert, update, and delete operations on multiple collections in one request. To specify each collection in the bulkWrite
command, use a namespace (database and collection name).
The command has the following syntax:
db.adminCommand( {
bulkWrite: 1,
// Include the insert, update, and delete operations
// in the ops array
ops: [
{
insert: <integer>, // Namespace ID index for insert operation.
// Must match a namespace ID index in
// ns specified later in the nsInfo array.
document: <document>
},
{
update: <integer>, // Namespace ID index for update operation
filter: <document>,
updateMods: <document>,
arrayFilters: [ <filterDocument0>, <filterDocument1>, ... ],
multi: <bolean>,
hint: <document>,
constants: <document>,
collation: <document>
},
{
delete: <integer>, // Namespace ID index for delete operation
filter: <document>,
multi: <boolean>,
hint: <document>,
collation: <document>
},
...
// Additional insert, update, and delete operations in any order
...
],
// Include the namespaces with collections to modify
// in the nsInfo array. You can add multiple namespaces here.
nsInfo: [
{
ns: <string>, // Namespace (database and collection name) to modify.
// Each operation namespace ID index
// specified in the earlier ops array must
// match a namespace ID index here.
collectionUUID: <string>,
encryptionInformation: <document>
},
...
// Additional namespaces
...
],
// Additional fields
ordered: <boolean>,
bypassDocumentValidation: <boolean>,
comment: <string>,
let: <document>,
errorsOnly: <boolean>,
cursor: { batchSize: <integer> },
writeConcern: <string>
} )
In the command syntax, you can specify multiple:
- Insert, update, and delete operations in any order in the
ops
array. - Namespaces for the operations in the
nsInfo
array. To match the operation to the namespace, use the same namespace ID index. Indexes start at 0.
Single Namespace Example
Here’s an example of using the bulkWrite
command to perform multiple operations in a single namespace:
db.adminCommand({ bulkWrite: 1,
ops: [{
insert: 0,
document: {
_id: '9492006462',
name: 'Wine Country Retreat',
summary: "A cozy retreat nestled in the Temecula Valley Wine Country.",
property_type: 'House',
room_type: 'Entire home/apt',
bedrooms: 4,
bathrooms: Decimal128('3.5'),
amenities: ['Free HBO'],
price: Decimal128('415.00'),
address: {
street: 'Calle De Vida Loca', suburb: 'Temecula', market: 'So Cal',
country: 'United States', country_code: 'US'
}
}
},
{
update: 0,
filter: {_id: '14234514'},
updateMods: { $set: 'address.market': 'Kauai'}}
}
],
nsInfo: [{ns: "sample_airbnb.listingsAndReviews"}]
})
Multiple Namespace Example
Here is an example of using the bulkWrite
command to perform multiple operations in multiple namespaces:
db.adminCommand({ bulkWrite: 1,
ops: [{
update: 0,
filter: { _id: '9492006462'},
updateMods: {
$set: {
host: {host_id: '0118 999 881 999 119 7253', host_name: 'Engelbert Humperdinck'}}
}
},
{
insert: 1,
document: {
_id: '0118 999 881 999 119 7253',
name: 'Engelbert Humperdinck'}
}
],
nsInfo: [
{ns: "sample_airbnb.listingsAndReviews"},
{ns: "sample_airbnb.hosts"}
]
})