mongodb的sum操作以及javaapi的实现

4 篇文章 0 订阅
2 篇文章 0 订阅


mongodb中对某一个字段的求和操作是通过 aggregate 来实现的:

   例如:db.successCard.aggregate({$group:{_id:null,moneysum:{$sum:"$money"}}});

   上述语法的解释:$group 分组  _id表示分组的字段(null表示不分组),  moneysum自定义的总和字段, $sum 表示求和 , $money 其中money表示被求和的字段

   注意:被计算的字段必须是 整型,long或者浮点型


java对它的实现如下

	/**
	 * 计算某个字段是和
	 * @param collection
	 * @param filedName
	 * @return
	 */
	public double sumField(String collection,String filedName,Criteria criteria) {  
		double total = 0l;  
        String reduce = "function(doc, aggr){" +  
                "            aggr.total += parseFloat((Math.round((doc." + filedName + ")*100)/100).toFixed(2));" +  
                "       }";  
        Query query = new Query();
        if(criteria!=null){
        	query.addCriteria(criteria);   	
        }
        DBObject result = mongoTemplate.getCollection(collection).group(null,   
                query.getQueryObject(),   
                new BasicDBObject("total", total),  
                reduce);  
          
        Map<String,BasicDBObject> map = result.toMap();  
        if(map.size() > 0){  
            BasicDBObject bdbo = map.get("0");  
            if(bdbo != null && bdbo.get("total") != null)  
                total = bdbo.getDouble("total");  
        }  
        return total;  
    } 


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MongoDB提供了Java驱动程序,以便使用Java语言访问数据库。以下是MongoDB Java操作API的一些示例代码: 1. 连接到数据库: ``` MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase database = mongoClient.getDatabase("mydatabase"); ``` 2. 插入数据: ``` MongoCollection<Document> collection = database.getCollection("mycollection"); Document document = new Document("name", "John Doe") .append("age", 30) .append("address", new Document("street", "123 Main St") .append("city", "Anytown") .append("state", "CA") .append("zip", 12345)); collection.insertOne(document); ``` 3. 查询数据: ``` MongoCollection<Document> collection = database.getCollection("mycollection"); Document query = new Document("name", "John Doe"); FindIterable<Document> results = collection.find(query); for (Document result : results) { System.out.println(result.toJson()); } ``` 4. 更新数据: ``` MongoCollection<Document> collection = database.getCollection("mycollection"); Document query = new Document("name", "John Doe"); Document update = new Document("$set", new Document("age", 31)); UpdateResult result = collection.updateOne(query, update); System.out.println("Modified count: " + result.getModifiedCount()); ``` 5. 删除数据: ``` MongoCollection<Document> collection = database.getCollection("mycollection"); Document query = new Document("name", "John Doe"); DeleteResult result = collection.deleteOne(query); System.out.println("Deleted count: " + result.getDeletedCount()); ``` 这些示例代码可以帮助你开始使用MongoDB Java操作API。但是,还有很多其他功能可以探索,具体取决于你的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值