驱动
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"));
}
查找
MongoCollection<Document> users = mongoDB.getCollection("coll");
Document findObj=users.find(new Document("SEGMENT1","abc")).first();
if(findObj!=null){
return findObj.get("name").toString();
}
插入
MongoCollection<Document> coll= mongoDB.getCollection("coll_name")
coll.insertOne(new Document("name","abc")
.append("age",20)
)
查找更新
注意要引用:
import static com.MongoDB.client.model.Filters.*;
coll.updateOne(eq("id",3), new Document("$set",new Document("HANDLED",true)));
coll.findOneAndReplace(doc1, doc2);
JSON 转换
DBObject dbObject = (DBObject) JSON.parse("{'name':'mkyong', 'age':30}");
String str=JSON.serialize(dbObject);
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");
}