• 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);
    
        }
    

      

  • 相关阅读:
    codesmith 连接mysql
    数据库 价格字段 设置 decimal(8,2),价格为100W,只显示999999.99
    AOP和IOC
    Android Studio 每次运行都会再下载一遍,修改
    gradle 的jar下载到哪里了
    遇到的坑
    Error:Failed to resolve: :Base:
    re-download dependencies and 无法下载jar 的解决
    DI是实现面向切面和面向抽象的前提
    基础才是重中之重~ConcurrentDictionary让你的多线程代码更优美
  • 原文地址:https://www.cnblogs.com/anxbb/p/9480596.html
Copyright © 2020-2023  润新知