java 对 mongo 的调用

驱动

https://github.com/mongodb/mongo-java-driver 
http://central.maven.org/maven2/org/mongodb/mongo-java-driver/

连接

private static void mongo_conn(){
    MongoClient mongoClient = new MongoClient(properties.getProperty("mongo_host") , 27017 );
    mongoDB = mongoClient.getDatabase(properties.getProperty("mongo_db"));
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

查找

MongoCollection<Document>  users = mongoDB.getCollection("coll");
Document findObj=users.find(new Document("SEGMENT1","abc")).first();
if(findObj!=null){
    return findObj.get("name").toString();
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

插入

MongoCollection<Document>  coll= mongoDB.getCollection("coll_name");
coll.insertOne(new Document("name","abc")
        .append("age",20)
        );
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

查找更新

注意要引用: 
import static com.MongoDB.client.model.Filters.*;

coll.updateOne(eq("id",3), new Document("$set",new Document("HANDLED",true)));

coll.findOneAndReplace(doc1, doc2);
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

JSON 转换

DBObject dbObject = (DBObject) JSON.parse("{'name':'mkyong', 'age':30}");
String str=JSON.serialize(dbObject);
 
 
  • 1
  • 2
  • 1
  • 2

MapReduce

    String map="function(){"+
            "emit(this.MATERIAL_CODE,{count:1,RECORD_DATE:this.RECORD_DATE} );"+
        "}";
        String reduce="function(key,values){"+
            "var earliest_record_date=new Date('2099-1-1');"+
            "var sum=0;"+
            "for(var i=0;i<values.length;i++){"+
            "   var m=values[i];"+
            "   sum += m.count;"+
            "   if(m.record_date){"+
            "       if(m.record_date<earliest_record_date)"+
            "           earliest_record_date=m.record_date;"+
            "   };"+
            "}"+
            "return {count:sum,RECORD_DATE:earliest_record_date};"+
        "}";
        MapReduceIterable<Document>  result= CRM_CSS_REPAIR_ORDER_TABLE_coll.mapReduce(map, reduce)
        .filter(
                and(
                        //eq("cateogry_id",category_id)
                        //,gt("PRODUCE_DATE",tempYearMonth)
                        //,lt("PRODUCE_DATE",dateLastDay)
                        )
                )
        .action(MapReduceAction.REPLACE);
         MongoCursor<Document> cursor = result.iterator();
         int i=0;
         String sql="";
         while (cursor.hasNext()) {
             i++;
             Document d=cursor.next();
             Document _id = (Document)d.get("_id");          
             Document value=(Document)d.get("value");
             Date record_date=value.getDate("RECORD_DATE");
             Date purchase_date=value.getDate("PURCHASE_DATE");
             double count=value.getDouble("count");
        }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

aggregate

db.CRM_CSS_REPAIR_ORDER_TABLE.aggregate(
   [
        {
            $group : {
                _id : { 
                    key1:"$COLUMN_NAME"
                    "key2_date_year":{$year:"$produce_date"},
                    "key3_date_month":{$month:"$produce_date"}
                },
                count: { $sum: 1 },
                min_RECORD_DATE:{$min:'$RECORD_DATE'},
                min_purchase_date:{$min:'$purchase_date'}
            }

        },
        {
            $sort:{
                count:-1
            }
        }
   ]
)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Java代码

List<Document> pipeline = Arrays.asList(
    new Document("$group", new Document( "_id", 
            new Document("MATERIAL_CODE","$MATERIAL_CODE")                         
                .append("PROVINCE_NAME","$PROVINCE_NAME")
                .append("RECORD_DATE_YEAR",new Document("$year","$RECORD_DATE"))
                .append("RECORD_DATE_MONTH",new Document("$month","$RECORD_DATE"))
            )
            .append("count", new Document("$sum",1))
            .append("min_produce_date",new Document("$min","$produce_date"))
    )
    ), 
    new Document("$sort", new Document("count", -1))
);    

AggregateIterable<Document> iterable = CRM_CSS_REPAIR_ORDER_TABLE_coll.aggregate(pipeline).allowDiskUse(true);
int i=0;
for (Document d : iterable){
     i++;
     Document _id=(Document)d.get("_id");
     String ITEM_NUMBER=_id.getString("MATERIAL_CODE");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值