简体   繁体   中英

mongo hint command for native node.js driver

Two same query gives me different records in mongo shell and in native node.js driver:

collection.find(query).limit(1).hint(hint).toArray

collection.find(query).limit(1).hint(hint).toArray

I supposed that in native node.js driver hint is ignored, and it was approved:

when I removed hint from query in node.js driver, the result was the same as I got when I used hint in node.js driver:

collection.find(query).limit(1).toArray 

So maybe there is special rule to use hint in node.js mongo native driver ? How to use it?

There actually isn't a .hint() method implemented in the node driver. In order to do this you supply "options" to the cursor creation in .find() :

collection.find({},{ "hint": { "a": -1 }}).limit(1).toArray(function(err,docs) {

Providing that actually matches an index that is specified then you will get the documents returned in the selected index order. Perhaps a longer example:

var async = require("async"),
    mongo = require("mongodb"),
    MongoClient = mongo.MongoClient;


MongoClient.connect('mongodb://localhost/test',function(err,db) {

  async.waterfall(
    [
      function(callback) {
        db.collection('asample',function(err,collection) {
          if (err) throw err;
          callback(err,collection);
        });
      },

      function(collection,callback) {
        collection.remove(function(err,removed) {
          if (err) throw err;
          callback(err,collection);
        });
      },

      //
      function(collection,callback) {
        async.eachSeries(
          [
            { "a": 1 },
            { "a": 2 }
          ],
          function(item,callback) {
            collection.insert(item,function(err,num) {
              if (err) throw err;
              callback();
            });
          },
          function(err) {
            if (err) throw err;
            callback(err,collection);
          }
        );
      },

      function(collection,callback) {
        collection.ensureIndex({ "a": -1 },function(err,index) {
          if (err) throw err;
          console.log("Index:\n%s", index);
          callback(err,collection);
        });
      },

      function(collection,callback) {
        collection.find({},{ "hint": { "a": -1 }})
          .limit(1).toArray(function(err,docs) {
            if (err) throw err;
            console.log("Docs:\n%s", JSON.stringify( docs, undefined, 4));
            callback();

        });
      }

    ],
    function(err) {
       console.log("done");
       db.close();
    }

  );

});

Where in the reverse index order the value of "2" is returned first.

Really though, "hint" should not need to be applied as long as either your "query" or .sort() specification mention the correct index to use. So that would be the better approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM