• mongo学习-group操作以及java代码


    原数据:

    /* 1 */
    {
        "_id" : ObjectId("552a330e05c27486b9b9b650"),
        "_class" : "com.mongo.model.Orders",
        "onumber" : "002",
        "date" : ISODate("2014-01-03T16:03:00.000Z"),
        "cname" : "zcy",
        "item" : {
            "quantity" : 1.0,
            "price" : 4.0,
            "pnumber" : "p002"
        }
    }
    
    /* 2 */
    {
        "_id" : ObjectId("552a331d05c275d8590a550d"),
        "_class" : "com.mongo.model.Orders",
        "onumber" : "003",
        "date" : ISODate("2014-01-04T16:03:00.000Z"),
        "cname" : "zcy",
        "item" : {
            "quantity" : 10.0,
            "price" : 2.0,
            "pnumber" : "p001"
        }
    }
    
    /* 3 */
    {
        "_id" : ObjectId("552a333105c2f28194045a72"),
        "_class" : "com.mongo.model.Orders",
        "onumber" : "003",
        "date" : ISODate("2014-01-04T16:04:00.000Z"),
        "cname" : "zcy",
        "item" : {
            "quantity" : 30.0,
            "price" : 4.0,
            "pnumber" : "p002"
        }
    }
    
    /* 4 */
    {
        "_id" : ObjectId("552a333f05c2b62c01cff50e"),
        "_class" : "com.mongo.model.Orders",
        "onumber" : "004",
        "date" : ISODate("2014-01-05T16:03:00.000Z"),
        "cname" : "zcy",
        "item" : {
            "quantity" : 5.0,
            "price" : 4.0,
            "pnumber" : "p002"
        }
    }
    

      

    一。mongo语句

    db.orders.group({
        key:{date:1,"item.pnumber" : 1},
        initial:{"total":0},
        reduce:function Reduce(doc,out){
             out.total+=doc.item.price
            }
        })
    

      

    对out数据进一步处理
    
    db.orders.group({
    	key: {
    		date: 1
    	},
    	initial: {
    		"total": 0,
                    "money":0
    	},
    	reduce: function Reduce(doc, out) {
    		out.total += doc.item.quantity;
                    out.money += doc.item.price*doc.item.quantity
    	},
            finalize:function Finalize(out){
                out.avg=out.money/out.total
                return out
                
             }
            
    })
    

      

    keyf :对分组字段先进行处理然后分组
    
    db.orders.group({
    	keyf: function keysss(doc){
              return {"month":doc.date.getDay()+1}    
            },
    	initial: {
    		"total": 0,
                    "money":0
    	},
    	reduce: function Reduce(doc, out) {
    		out.total += doc.item.quantity;
                    out.money += doc.item.price*doc.item.quantity
    	},
            finalize:function Finalize(out){
                out.avg=out.money/out.total
                return out
             }
            
    })
    

      

    java代码:

    @Test
        public void group(){
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:111.xml");
            MongoTemplate mongoTemplate= (MongoTemplate) applicationContext.getBean("mongoTemplate");
            GroupBy groupBy=new GroupBy("date");
            BasicDBObject basicDBObject=new BasicDBObject();
            basicDBObject.put("total",0);
            basicDBObject.put("money",0);
            groupBy.initialDocument(basicDBObject);
            groupBy.reduceFunction("function Reduce(doc, out) {out.total += doc.item.quantity;out.money += doc.item.price*doc.item.quantity}");
            groupBy.finalizeFunction("function Finalize(out){
    " +
                    "            out.avg=out.money/out.total
    " +
                    "            return out
    " +
                    "         }");
    //        groupBy.finalizeFunction("function Finalize(out) {out.avg = out.money / out.totalreturn out}");
            GroupByResults<Object> orders = mongoTemplate.group("orders", groupBy, Object.class);
    
        }
    

      

  • 相关阅读:
    采用闭锁(CountDownLatch)控制线程的先后顺序(一)
    采用java信号量(semaphore)让线程轮流打印
    生产者消费者模式的java实现(实现四)
    生产者消费者模式的java实现(实现三)
    生产者消费者模式的java实现(实现二)
    生产者消费者模式的java实现(实现一)
    求最大子串和 最长子串的java写法
    Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process 异常处理
    定时任务服务器不定时重启原因解析
    centos 6.3 + gerrit-2.8.6 + repo 实践
  • 原文地址:https://www.cnblogs.com/anxbb/p/9480596.html
Copyright © 2020-2023  润新知