标签:
Group大约需要一下几个参数。
测试数据
//测试的数据 db.stocks.insert({"day": "2010/10/03", "time": "2010/10/03 03:47:01 GMT-400", "price": 4.23}); db.stocks.insert({"day": "2010/10/04", "time": "2010/10/04 11:28:39 GMT-400", "price": 4.27}); db.stocks.insert({"day": "2010/10/03", "time": "2010/10/03 05:00:23 GMT-400", "price": 4.10}); db.stocks.insert({"day": "2010/10/06", "time": "2010/10/06 05:27:58 GMT-400", "price": 4.30}); db.stocks.insert({"day": "2010/10/04", "time": "2010/10/04 08:34:50 GMT-400", "price": 4.01});
测试要执行的GROUP程序代码
db.runCommand({"group": { "ns": "stocks", "key": "day", "initial": {"time": 0}, "$reduce": function(doc, prev) { if (doc.time > prev.time) { prev.price = doc.price; prev.time = doc.time; } } }});
执行之后的结果
{ "retval" : [ { "time" : 0 } ], "count" : 5, "keys" : 1, "ok" : 1 }
标签:
原文地址:http://www.cnblogs.com/timelesszhuang/p/4300310.html