Bulk.find.hint()

Tip

Starting in version 3.2, MongoDB also provides thedb.collection.bulkWrite() method for performing bulkwrite operations.

Description

  • Bulk.find.hint()

New in version 4.2.1.

Sets the hint option that specifies the indexto support the Bulk.find() for:

If you specify an index that does not exist, the operationerrors.

Bulk.find.hint() has no effect on Bulk.find.removeOne()

Example

Create an example collection orders:

  1. db.orders.insert([
  2. { "_id" : 1, "item" : "abc", "price" : NumberDecimal("12"), "quantity" : 2, "type": "apparel" },
  3. { "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : 1, "type": "electronics" },
  4. { "_id" : 3, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : 5, "type": "apparel" },
  5. { "_id" : 4, "item" : "abc", "price" : NumberDecimal("8"), "quantity" : 10, "type": "apparel" },
  6. { "_id" : 5, "item" : "jkl", "price" : NumberDecimal("15"), "quantity" : 15, "type": "electronics" }
  7. ])

Create the following indexes on the example collection:

  1. db.orders.createIndex( { item: 1 } );
  2. db.orders.createIndex( { item: 1, quantity: 1 } );
  3. db.orders.createIndex( { item: 1, price: 1 } );

The following bulk operations specify different index to use for thevarious update/replace document operations:

  1. var bulk = db.orders.initializeUnorderedBulkOp();
  2. bulk.find({ item: "abc", price: { $gte: NumberDecimal("10") }, quantity: { $lte: 10 } }).hint({item: 1, quantity: 1}).replaceOne( { item: "abc123", status: "P", points: 100 } );
  3. bulk.find({ item: "abc", price: { $gte: NumberDecimal("10") }, quantity: { $lte: 10 } }).hint({item: 1, price: 1}).updateOne( { $inc: { points: 10 } } );
  4. bulk.execute();

To view the indexes used, you can use the $indexStats pipeline:

  1. db.orders.aggregate( [ { $indexStats: { } }, { $sort: { name: 1 } } ] )

See also