Mongodb按照集合中的最新日期获取文档

时间:2014-01-30 08:28:38

标签: node.js mongodb

我是mongodb的新手,开发节点js应用程序并使用mongodb(Mongodb服务器)。我有一个文件结构如下文件

{ "associationid" : "52dbb5c0279f660000000001", "gold22ct1grm" : "2816", "gold24ct10grm" : "281736", "silver1grm" : "47.30", "silver1kg" : "473580", "created" : "28-01-2014 07:56:14 am", "_id" : { "$oid" : "52e7629ec1a04e0000000001" } }
{ "associationid" : "52dbb5c0279f660000000001", "gold22ct1grm" : "2816", "gold24ct10grm" : "281736", "silver1grm" : "47.30", "silver1kg" : "473580", "created" : "28-01-2014 07:56:17 am", "_id" : { "$oid" : "52e762a1c1a04e0000000002" } }
{ "associationid" : "52dbb5c0279f660000000001", "gold22ct1grm" : "2861", "gold24ct10grm" : "28650", "silver1grm" : "48.36", "silver1kg" : "48756", "created" : "28-01-2014 08:09:40 am", "_id" : { "$oid" : "52e765c4683bee0000000001" } }
{ "associationid" : "52e75a9e0c504d0000000002", "gold22ct1grm" : "2865", "gold24ct10grm" : "28653", "silver1grm" : "45.50", "silver1kg" : "45655", "created" : "28-01-2014 11:10:16 am", "_id" : { "$oid" : "52e790182ffd050000000001" } }
{ "associationid" : "52e75a9e0c504d0000000002", "gold22ct1grm" : "2865", "gold24ct10grm" : "28653", "silver1grm" : "45.50", "silver1kg" : "45655", "created" : "28-01-2014 11:10:18 am", "_id" : { "$oid" : "52e7901a2ffd050000000002" } }
{ "associationid" : "52dbb5c0279f660000000001", "gold22ct1grm" : "2895", "gold24ct10grm" : "28950", "silver1grm" : "45.36", "silver1kg" : "45690", "created" : "30-01-2014 06:26:43 am", "_id" : { "$oid" : "52e9f0a35a72ac0000000001" } }
{ "associationid" : "52dbb5c0279f660000000001", "gold22ct1grm" : "2895", "gold24ct10grm" : "28950", "silver1grm" : "45.36", "silver1kg" : "45690", "created" : "30-01-2014 06:26:44 am", "_id" : { "$oid" : "52e9f0a45a72ac0000000002" } }
{ "associationid" : "52dbb5c0279f660000000001", "gold22ct1grm" : "2895", "gold24ct10grm" : "28950", "silver1grm" : "45.36", "silver1kg" : "45690", "created" : "30-01-2014 06:26:45 am", "_id" : { "$oid" : "52e9f0a55a72ac0000000003" } }
{ "associationid" : "52dbb5c0279f660000000001", "gold22ct1grm" : "2895", "gold24ct10grm" : "28950", "silver1grm" : "45.36", "silver1kg" : "45690", "created" : "30-01-2014 06:26:46 am", "_id" : { "$oid" : "52e9f0a65a72ac0000000004" } }
{ "associationid" : "52dbb5c0279f660000000001", "gold22ct1grm" : "2808", "gold24ct10grm" : "30030", "silver1grm" : "47.50", "silver1kg" : "44385", "created" : "30-01-2014 06:59:00 AM", "_id" : { "$oid" : "52e9f8343e49250000000001" } }
{ "associationid" : "52dbb5f4279f660000000002", "gold22ct1grm" : "2809", "gold24ct10grm" : "30033", "silver1grm" : "47.50", "silver1kg" : "44385", "created" : "30-01-2014 07:31:10 AM", "_id" : { "$oid" : "52e9ffbe51e5d90000000001" } }
{ "associationid" : "52dbb5f4279f660000000002", "gold22ct1grm" : "2810", "gold24ct10grm" : "30050", "silver1grm" : "48.50", "silver1kg" : "44385", "created" : "30-01-2014 01:09:40 PM", "_id" : { "$oid" : "52ea01bc7c64dd0000000001" } }
{ "associationid" : "52dbb5c0279f660000000001", "gold22ct1grm" : "2816", "gold24ct10grm" : "281736", "silver1grm" : "47.30", "silver1kg" : "473580", "created" : "11-02-2014 07:56:14 AM", "_id" : { "$oid" : "52e7629ec1a04e0000000001" } }

How to get the document created finally.(most latest date created)
I used following code in my node.js but it returning wrong answer
exports.getMJDMADailyRates = function(id, callback)
{
    associationrates.find({associationid : id}, {'limit':1, 'sort': { 'created' : -1 } }).toArray(function(err, docs) { 
        if(err) callback(err,null);
        else callback(null,docs);
    });
}

如果我像getMJDMADailyRates那样调用函数('52dbb5c0279f660000000001');意味着它应该返回文件

 { "associationid" : "52dbb5c0279f660000000001", "gold22ct1grm" : "2816", "gold24ct10grm" : "281736", "silver1grm" : "47.30", "silver1kg" : "473580", "created" : "11-02-2014 07:56:14 AM", "_id" : { "$oid" : "52e7629ec1a04e0000000001" } }

但我很喜欢,

  { "associationid" : "52dbb5c0279f660000000001", "gold22ct1grm" : "2808", "gold24ct10grm" : "30030", "silver1grm" : "47.50", "silver1kg" : "44385", "created" : "30-01-2014 06:59:00 AM", "_id" : { "$oid" : "52e9f8343e49250000000001" } }

错误是什么以及如何通过查询获得预期结果。

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,您希望您的查询按创建的最新日期排序,是吗?

如果是这样,这应该适合你:

collection.find().sort({created: -1}, function(err, results){
    //
});