2014-07-16 74 views
1

我特别使用猫鼬,虽然我不相信那么重要。例如,假设我有一个名为MongoQueue的集合,并且我将几个人添加到该队列中。可以找到第一个或最后一个匹配吗?

`MongoQueue.save {function(err, firstPerson) { 
    if (err) console.log(err); 
    MongoQueue.save {function(err, secondPerson) { 
     if (err) console.log(err); 
     MongoQueue.save {function(err, thirdPerson) { 
     if (err) console.log(err); 
}}}` 


如何取回谁是第一个保存到MongoQueue的人吗?或者.... mongoDB的findOne()方法如何缩小它的搜索范围?我可以指定findOne()的行为来选择集合中最早的文档吗?或者我必须对集合进行排序(这将是最后的手段),如果是的话,我将如何按时间戳排序?

+1

的MongoDB保持在DB对象_id时间戳,它自然排序按日期 – saj

回答

0

findOne()函数返回natural order中的文档,这是磁盘上的顺序。你不能指望这返回最近插入的文件。要返回最近最少插入的文档,可以在_id上使用find()函数进行反向排序并限制为1,假定_id是为您生成的默认ObjectId,或者是时间戳(_id部分构建,从一个时间戳,这就是为什么它与_id一起工作)。如果您使用_id(始终索引)或索引时间戳字段,这将非常快。

+0

该命令不在“自然”的顺序,除非返回你实际上告诉它做其他事情。 '_id'主索引默认使用,您可以提供排序规范。 'finOne()'实际上只是'.find()'的一个包装,它只返回一个文档。就像'.find()'一样,还有其他方法可以提供排序和其他查询修饰符。 –

+0

@NeilLunn:如果没有指定查询或排序选项,find()(或findOne())将使用自然顺序(而不是'_id'索引)。另请参见:[Mongo排序时没有指定排序顺序是什么?](http://stackoverflow.com/questions/11599069)..或尝试'.find.explain()'确认一个'BasicCursor'是正在使用。 – Stennie

0

是的,您可以指定.findOne()的行为,如本机驱动程序文档中所示。唯一的区别是,在猫鼬的实现中,“选项”文件必须是是传递给该方法的“第三”参数。

所以,你可以提供一个“分类”规范此为显示在可用的选项:

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

只是为了更多的信息,你可以在MongoDB的外壳采用以下做到这一点,利用$orderby查询选项:

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

另外,.findOne()方法可以只返回一个文件,但它确实是就在.find()的包装,因此所有的修饰符适用。包装仅在返回的游标上调用.next(),返回文档并丢弃游标。

此不再示例示出了其中本可以应用不同的方法:

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(); 
    } 
); 
相关问题