Skip to content Skip to sidebar Skip to footer

Can Findone Match First Or Last?

I'm specifically using mongoose, although I don't believe that matters that much. For example, say I have a collection called MongoQueue and I add a few people to that queue. `Mong

Solution 1:

Yes you can specify the behavior of .findOne() as is best shown in the native driver documentation. The only difference there is that in the mongoose implemetation the "options" document must be the "third" argument passed to the method.

So you can supply a "sort" specification to this as is shown in the available options:

Queue.findOne({ },null,{ "sort": { "_id": -1 } },function(err,doc) {

Just for additional information you can do this in the MongoDB shell with the following, using the $orderby query option:

db.collection.findOne({ "$query": { }, "$orderby": { "_id": -1 } })

Also the .findOne() method may only return one document, but it really is just a wrapper around .find() so all of the modifiers apply. The wrapping just calls .next() on the returned cursor, returns the document and discards the cursor.

This longer example shows different ways in which this can be applied:

varasync = require('async'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema;


mongoose.connect('mongodb://localhost/sequence');

var queueSchema = newSchema({
  name: String,
  same: { type: String, default: "same" }
});

varQueue = mongoose.model( "Queue", queueSchema );

var count = 0;

async.series(
  [

    // Remove any documentsfunction(callback) {
      Queue.remove(function(err) {
        if (err) throw err;
        callback();
      });
    },

    // Insert some new onesfunction(callback) {
      async.eachSeries(
        ["one","two","three"],
        function(item,callback) {
          var queue = newQueue({ name: item });
          queue.save(function(err,doc) {
            if (err) throw err;
            console.dir(doc);
            callback(err,doc);
          });
        },
        function(err) {
          callback(err);
        }
      );
    },

    function(callback) {
      async.whilst(
        function() { return count < 2 },
        function(callback) {
          count++
          async.series(
            [
              // findOne is just the first onefunction(callback) {
                Queue.findOne({ "same": "same" },function(err,doc) {
                  if (err) throw err;
                  console.log( "FindOne:\n%s", doc );
                  callback();
                });
              },

              // Or is sortedfunction(callback) {
                Queue.findOne(
                  { "same": "same" },
                  null,
                  { "sort": { "_id": -1 } },
                  function(err,doc) {
                    if (err) throw err;
                    console.log("FindOne last:\n%s", doc );
                    callback();
                  }
                );
              },

              // find is ordered but not singularfunction(callback) {
                async.eachSeries(
                  ["first","last"],
                  function(label,callback) {
                    var direction = ( label == "first" ) ? 1 : -1;
                    var query = Queue.find({ "same": "same" })
                      .sort({ "_id": direction })
                      .limit(1);
                    query.exec(function(err,docs) {
                      if (err) throw err;
                      console.log( ".find() %s:\n%s", label, docs[0] );
                      callback();
                    });
                  },
                  function(err) {
                    callback();
                  }
                );
              },

              // findAndModify takes a sortfunction(callback) {
                Queue.findOneAndUpdate(
                  { "same": "same" },
                  { "$set": { "same": "different" } },
                  { "sort": { "_id": -1 } },
                  function(err,doc) {
                    if (err) throw err;
                    console.log( "findOneAndUpdate:\n%s", doc );
                    callback();
                  }
                );
              }

            ],function(err) {
              callback();
            }
          );
        },
        function(err) {
          callback();
        }
      );
    }
  ],function(err) {
    console.log("done");1
    mongoose.disconnect();
  }
);

Solution 2:

The findOne() function returns documents in the natural order, which is the order on disk. You cannot count on this returning the least recently inserted document. To return the least recently inserted document, you can use the find() function with an inverse sort on _id and a limit of 1, assuming _id is a default ObjectId generated for you, or on a timestamp (_id is built, in part, from a timestamp, which is why it works with _id). If you use _id (which is always indexed) or index the timestamp field, this will be very fast.

Post a Comment for "Can Findone Match First Or Last?"