MongoDB作为一个介入关系型与非关系性这间的NoSQL数据库,其操作与SQL的单表的操作基本类似,相对传递数据库,MongoDB没有表的概念,引入集合的概念,以及于对数化的格式化无要求。由于MongoDB处理效率极高,且连接极快,所以对MongoDB的连接均采用的短连接
1、MongoDB 初始化,获取集合
public static DBCollection getDBCollection(Mongo mongo, String dbName, String collectionName) {
if (mongo == null) {
mongo = new Mongo(MongoDBUtil.IP, MongoDBUtil.PORT);
}
DB db = mongo.getDB(dbName);
DBCollection dbCollection = db.getCollection(collectionName);
return dbCollection;
}
注意以上是在不需要认证的情况下获取,修改集合,如果需要认证,请参考以下方法:
Java授权调用如下:
MongoClient client = null;
try {
MongoCredential credential = MongoCredential.createCredential("username" , "dbname", "pwd");
ServerAddress addr = new ServerAddress("ip", port);
client = new MongoClient(addr, Arrays.asList(credential));
DB db = client.getDB(MongoDBCfg.DB_SP2P);
DBCollection dbCollection = db.getCollection(collectionName);
return dbCollection;
} catch (Exception e) {
} finally {
if (client != null) {
client.close();
}
}
2、MongoDB插入, 如下将一个列表插入Collection
try {
/*获取集合*/
mongo = new Mongo(MongoDBUtil.IP, MongoDBUtil.PORT);
DB db = mongo.getDB(MongoDBUtil.BID_DBNAME);
DBCollection dbCollection = db.getCollection(MongoDBUtil.BID_COLLECTION_NAME);
for (int i = 0; i < bids.size(); i++) {
DBObject dbObject = this.convVFrontAllBidsToDBObject(bids.get(i));
if (dbObject == null) {
continue;
}
dbCollection.insert(dbObject);
}
} catch (Exception e) {
error.code = ErrorInfoDefine.ERROR_CODE_EXCEPTION;
error.msg = e.toString();
LogUtil.logException("saveInvestBidListToMongoDB", e, "严重错误.");
} finally {
MongoDBUtil.freeMongo(mongo);
}
3、MongoDB 更新
try {
/*获取集合*/
mongo = new Mongo(MongoDBUtil.IP, MongoDBUtil.PORT);
DB db = mongo.getDB(MongoDBUtil.BID_DBNAME);
DBCollection dbCollection = db.getCollection(MongoDBUtil.BID_COLLECTION_NAME);
DBObject dbObject = new BasicDBObject();
dbObject.put("has_invested_amount", amount);
dbObject.put("loan_schedule", schedule);
DBObject dbQuery = new BasicDBObject();
dbQuery.put("_id", bid);
dbCollection.update(dbQuery, new BasicDBObject("$set", dbObject));
} catch (Exception e) {
error.code = ErrorInfoDefine.ERROR_CODE_EXCEPTION;
error.msg = e.toString();
LogUtil.logException("updateinvestBid", e, "严重错误.");
} finally {
MongoDBUtil.freeMongo(mongo);
}
注意:dbCollection.update(dbQuery, new BasicDBObject(“$set”, dbObject)); 一定要这样写,否则后果自负
4、MongoDB查询
try {
/*获取集合*/
mongo = new Mongo(MongoDBUtil.IP, MongoDBUtil.PORT);
DB db = mongo.getDB(MongoDBUtil.BID_DBNAME);
DBCollection dbCollection = db.getCollection(MongoDBUtil.BID_COLLECTION_NAME);
//排序,与DAO保持一致 , 默认为0
//0 order by loan_schedule asc, is_hot desc, time desc ,
//1 order by period_unit asc, period desc, is_hot desc, time desc ,
//2 order by loan_schedule asc, is_hot desc, time desc
DBObject orderBy = new BasicDBObject();
if (order == 1) {
orderBy.put("period_unit", MongoDBUtil.ASC);
orderBy.put("period", MongoDBUtil.DESC);
orderBy.put("is_hot", MongoDBUtil.DESC);
orderBy.put("time", MongoDBUtil.DESC);
} else if (order == 2) {
orderBy.put("loan_schedule", MongoDBUtil.ASC);
orderBy.put("is_hot", MongoDBUtil.DESC);
orderBy.put("time", MongoDBUtil.DESC);
} else {
orderBy.put("loan_schedule", MongoDBUtil.ASC);
orderBy.put("is_hot", MongoDBUtil.DESC);
orderBy.put("time", MongoDBUtil.DESC);
}
int skip = (currPage - 1) * pageSize;
DBCursor dbCursor = dbCollection.find().sort(orderBy).skip(skip).limit(pageSize);
List<DBObject> list = dbCursor.toArray();
List<v_front_all_bids> bidList = new ArrayList<v_front_all_bids>();
for (int i = 0; i < list.size(); i++) {
bidList.add(this.convDBObjectToVFrontAllBids(list.get(i)));
}
return bidList;
} catch (Exception e) {
} finally {
MongoDBUtil.freeMongo(mongo);
}
5、删除,待续
驱动包下载地址:
https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongo-java-driver/
在下载的时候需要注意下插的java驱动包是mongo-java-driver而不是mongodb-java-driver