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.<
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:
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/sequence');
var queueSchema = new Schema({
name: String,
same: { type: String, default: "same" }
});
var Queue = mongoose.model( "Queue", queueSchema );
var count = 0;
async.series(
[
// Remove any documents
function(callback) {
Queue.remove(function(err) {
if (err) throw err;
callback();
});
},
// Insert some new ones
function(callback) {
async.eachSeries(
["one","two","three"],
function(item,callback) {
var queue = new Queue({ 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 one
function(callback) {
Queue.findOne({ "same": "same" },function(err,doc) {
if (err) throw err;
console.log( "FindOne:\n%s", doc );
callback();
});
},
// Or is sorted
function(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 singular
function(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 sort
function(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();
}
);