JAVA????MongoDB??????
???????????? ???????[ 2015/6/15 13:30:14 ] ????????Java ???????
// ------------------------------------???÷???---------------------------------------------------
/**
* ???DB??? - ???DB
*
* @param dbName
* @return
*/
public MongoDatabase getDB(String dbName) {
if (dbName != null && !"".equals(dbName)) {
MongoDatabase database = mongoClient.getDatabase(dbName);
return database;
}
return null;
}
/**
* ???collection???? - ???Collection
*
* @param collName
* @return
*/
public MongoCollection<Document> getCollection(String dbName?? String collName) {
if (null == collName || "".equals(collName)) {
return null;
}
if (null == dbName || "".equals(dbName)) {
return null;
}
MongoCollection<Document> collection = mongoClient.getDatabase(dbName).getCollection(collName);
return collection;
}
/**
* ???DB?μ????б???
*/
public List<String> getAllCollections(String dbName) {
MongoIterable<String> colls = getDB(dbName).listCollectionNames();
List<String> _list = new ArrayList<String>();
for (String s : colls) {
_list.add(s);
}
return _list;
}
/**
* ?????????????????б?
*
* @return
*/
public MongoIterable<String> getAllDBNames() {
MongoIterable<String> s = mongoClient.listDatabaseNames();
return s;
}
/**
* ???????????
*/
public void dropDB(String dbName) {
getDB(dbName).drop();
}
/**
* ??????? - ????????_id
*
* @param collection
* @param id
* @return
*/
public Document findById(MongoCollection<Document> coll?? String id) {
ObjectId _idobj = null;
try {
_idobj = new ObjectId(id);
} catch (Exception e) {
return null;
}
Document myDoc = coll.find(Filters.eq("_id"?? _idobj)).first();
return myDoc;
}
/** ????? */
public int getCount(MongoCollection<Document> coll) {
int count = (int) coll.count();
return count;
}
/** ??????? */
public MongoCursor<Document> find(MongoCollection<Document> coll?? Bson filter) {
return coll.find(filter).iterator();
}
/** ?????? */
public MongoCursor<Document> findByPage(MongoCollection<Document> coll?? Bson filter?? int pageNo?? int pageSize) {
Bson orderBy = new BasicDBObject("_id"?? 1);
return coll.find(filter).sort(orderBy).skip((pageNo - 1) * pageSize).limit(pageSize).iterator();
}
/**
* ???ID???
*
* @param coll
* @param id
* @return
*/
public int deleteById(MongoCollection<Document> coll?? String id) {
int count = 0;
ObjectId _id = null;
try {
_id = new ObjectId(id);
} catch (Exception e) {
return 0;
}
Bson filter = Filters.eq("_id"?? _id);
DeleteResult deleteResult = coll.deleteOne(filter);
count = (int) deleteResult.getDeletedCount();
return count;
}
/**
* FIXME
*
* @param coll
* @param id
* @param newdoc
* @return
*/
public Document updateById(MongoCollection<Document> coll?? String id?? Document newdoc) {
ObjectId _idobj = null;
try {
_idobj = new ObjectId(id);
} catch (Exception e) {
return null;
}
Bson filter = Filters.eq("_id"?? _idobj);
// coll.replaceOne(filter?? newdoc); // ??????
coll.updateOne(filter?? new Document("$set"?? newdoc));
return newdoc;
}
public void dropCollection(String dbName?? String collName) {
getDB(dbName).getCollection(collName).drop();
}
/**
* ???Mongodb
*/
public void close() {
if (mongoClient != null) {
mongoClient.close();
mongoClient = null;
}
}
/**
* ???????
*
* @param args
*/
public static void main(String[] args) {
String dbName = "GC_MAP_DISPLAY_DB";
String collName = "COMMUNITY_BJ";
MongoCollection<Document> coll = MongoDBUtil.instance.getCollection(dbName?? collName);
// ???????
// for (int i = 1; i <= 4; i++) {
// Document doc = new Document();
// doc.put("name"?? "zhoulf");
// doc.put("school"?? "NEFU" + i);
// Document interests = new Document();
// interests.put("game"?? "game" + i);
// interests.put("ball"?? "ball" + i);
// doc.put("interests"?? interests);
// coll.insertOne(doc);
// }
// // ????ID???
// String id = "556925f34711371df0ddfd4b";
// Document doc = MongoDBUtil2.instance.findById(coll?? id);
// System.out.println(doc);
// ??????
// MongoCursor<Document> cursor1 = coll.find(Filters.eq("name"?? "zhoulf")).iterator();
// while (cursor1.hasNext()) {
// org.bson.Document _doc = (Document) cursor1.next();
// System.out.println(_doc.toString());
// }
// cursor1.close();
// ??????
// MongoCursor<Person> cursor2 = coll.find(Person.class).iterator();
// ????????
// MongoDBUtil2.instance.dropDB("testdb");
// ?????
// MongoDBUtil2.instance.dropCollection(dbName?? collName);
// ???????
// String id = "556949504711371c60601b5a";
// Document newdoc = new Document();
// newdoc.put("name"?? "???");
// MongoDBUtil.instance.updateById(coll?? id?? newdoc);
// ????
// System.out.println(MongoDBUtil.instance.getCount(coll));
// ???????
Bson filter = Filters.eq("count"?? 0);
MongoDBUtil.instance.find(coll?? filter);
}
}
?????????汾??
????<!-- MongoDB???? -->
????<dependency>
????<groupId>org.mongodb</groupId>
????<artifactId>mongo-java-driver</artifactId>
????<version>3.0.2</version>
????</dependency>
???????????????????顣
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
Java???????????Щ???????????????Java????????????????Java?б???Map????????Java Web???????????????Java??????????????д?????Java????????7???????????????????????(java .net ?????)???Java??????????Python??????Java webdriver??λ????????′????е?????Java??д??????????????????Java???????????????JavaScript????????????Java?????????????????? Java???????10??????????????Java?м????????????????java???????ü???????????м???????????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????