MongDb group操作

这几天看了mongdb权威指南这本书,但是我下载的最新的版本(mongodb-src-r2.4.5)。 有些例子执行不成功。

如下面:

1、一条都没有返回 初始化time为0,

> db.runCommand({"group":{"ns":"stocks", "key":"day","initial":{"time":0},"$reduce":function(doc, prev){if(doc.time>prev.time){prev.time=doc.time; prev.price=doc.price; prev.day=doc.day}},"condition":{"day":{"$gt":"2010/09/30"}}}})

{

"retval" : [

{

"time" : 0

}

],

"count" : 5,

"keys" : 1,

"ok" : 1

}

 

2、只返回了一条初始化time为“0”

> db.runCommand({"group":{"ns":"stocks", "key":"day","initial":{"time":"0"},"$reduce":function(doc, prev){if(doc.time>prev.time){prev.time=doc.time; prev.price=doc.price; prev.day=doc.day}},"condition":{"day":{"$gt":"2010/09/30"}}}})

{

"retval" : [

{

"time" : "10/6/2010 05:27:01 GMT-400",

"price" : 4.3,

"day" : "2010/10/06"

}

],

"count" : 5,

"keys" : 1,

"ok" : 1

}

 

3、查了一下手册把“key”:"day"  改成了"key":{"day":1} ,这下数据OK了,数据升序排序,关于结实没有看到更多,我尝试改为“-1”和“0”想实现倒序排序,没有实现

db.runCommand({"group":{"ns":"stocks", "key":{"day":1},"initial":{"time":"0"},"$reduce":function(doc, prev){if(doc.time>prev.time){prev.time=doc.time; prev.price=doc.price; prev.day=doc.day}},"condition":{"day":{"$gt":"2010/09/30"}}}})

{

"retval" : [

{

"day" : "2010/10/03",

"time" : "10/3/2010 05:57:01 GMT-400",

"price" : 4.17

},

{

"day" : "2010/10/04",

"time" : "10/4/2010 08:27:01 GMT-400",

"price" : 4.01

},

{

"day" : "2010/10/06",

"time" : "10/6/2010 05:27:01 GMT-400",

"price" : 4.3

}

],

"count" : 5,

"keys" : 3,

"ok" : 1

}

 

你可能感兴趣的:(mongdb)