(1) MongoDB 의 자바 응용
다음은 제 가 쓴 기본 적 인 Mongodb 데이터 베 이 스 를 응용 하 는 자바 도구 류 입 니 다.
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.client.model.Filters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bson.Document;
import org.bson.conversions.Bson;
/** * * @author Angela */
public class MongoDBUtil {
public static void main(String args[]){
MongoClient client=connect();
//listDatabase(client);//
MongoDatabase db =getDatabase(client,"mycol");
//createCappedCollection(db,"p",10);
MongoCollection<Document> collection=getCollection(db,"test");
//dropCollection(collection);
//listCollection(db);
//select(collection,"likes",200);
//update(collection,"likes",200,100,false);
//delete(collection,"likes",100,false);
Map<String,Object> map1=new HashMap<String,Object>();
map1.put("title","MonogD");
List<Integer> list=new ArrayList<Integer>();
for(int i=0;i<5;i++){
list.add(i);
}
Map<String,Object> map2=new HashMap<String,Object>();
map2.put("title",list);
List<Map<String,Object>> maps=new ArrayList<Map<String,Object>>();
maps.add(map1);
maps.add(map2);
insert(collection,maps);
select(collection);
close(client);
}
/** mongodb **/
public static MongoClient connect(){
MongoClient client = new MongoClient( "localhost" , 27017 );
System.out.println(" MongoDB ");
return client;
}
/** mongodb , **/
public static MongoClient connect(String IP,String userName,
String dbName,String password){
//ServerAddress()
ServerAddress serverAddress = new ServerAddress(IP,27017);
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
//MongoCredential.createScramSha1Credential()
MongoCredential credential = MongoCredential.createScramSha1Credential(
userName, dbName, password.toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
// MongoDB
MongoClient client = new MongoClient(addrs,credentials);
System.out.println(" MongoDB ");
return client;
}
/** **/
public static MongoDatabase getDatabase(MongoClient client,String dbName){
// , ,
// ,mongo 。
MongoDatabase db = client.getDatabase(dbName);
System.out.println(" "+dbName+" ");
return db;
}
/** **/
public static void dropDatabase(MongoClient client,String dbName){
client.dropDatabase(dbName);
System.out.println(" "+dbName+" ");
}
/** **/
public static void listDatabase(MongoClient client){
MongoIterable<String> dbNames=client.listDatabaseNames();
int count=0;
System.out.println(" ");
for(String dbName: dbNames){
System.out.println(dbName);
count++;
}
System.out.println(" "+count+" ");
}
/** db **/
public static void createCollection(MongoDatabase db,String collectionName){
// com.mongodb.client.MongoDatabase createCollection()
db.createCollection(collectionName);
System.out.println(collectionName+" ");
}
/** * * @param db * @param collectionName * @param sizeInBytes , 10000 */
public static void createCappedCollection(MongoDatabase db,
String collectionName,long sizeInBytes){
// com.mongodb.client.MongoDatabase
//createCollection(String collectionName,CreateCollectionOptions cco)
CreateCollectionOptions options=new CreateCollectionOptions();
options=options.capped(true); //
options=options.sizeInBytes(sizeInBytes); //
db.createCollection(collectionName,options);
System.out.println(collectionName+" ");
}
/** * * @param db * @param collectionName * @param sizeInBytes , 10000 * @param maxDocuments */
public static void createCappedCollection(MongoDatabase db,
String collectionName,long sizeInBytes,long maxDocuments){
// com.mongodb.client.MongoDatabase
//createCollection(String collectionName,CreateCollectionOptions cco)
CreateCollectionOptions options=new CreateCollectionOptions();
options.capped(true);//
options=options.sizeInBytes(sizeInBytes); //
options.maxDocuments(maxDocuments); //
db.createCollection(collectionName);
System.out.println(collectionName+" ");
}
/** db collectionName **/
public static MongoCollection<Document> getCollection(
MongoDatabase db,String collectionName){
// com.mongodb.client.MongoDatabase getCollection()
MongoCollection<Document> collection = db.getCollection(collectionName);
System.out.println(" "+collectionName+" ");
return collection;
}
/** db **/
public static void listCollection(MongoDatabase db){
MongoIterable<String> collectionNames=db.listCollectionNames();
int count=0;
System.out.println(db+" ");
for(String collectionName: collectionNames){
System.out.println(collectionName);
count++;
}
System.out.println(" "+count+" ");
}
/** **/
public static void dropCollection(MongoCollection<Document> collection){
collection.drop();
System.out.println(collection.getNamespace().getCollectionName()
+" ");
}
/** **/
public static void insert(MongoCollection<Document> collection,
Map<String,Object> map){
// org.bson.Document key-value
Document document=new Document(map);
// com.mongodb.client.MongoCollection
//insertOne(Document)
collection.insertOne(document);
System.out.println(" ");
}
/** **/
public static void insert(MongoCollection<Document> collection,
List<Map<String,Object>> maps){
List<Document> documents = new ArrayList<Document>();
for(Map<String,Object> map: maps){
//1. org.bson.Document key-value
Document document=new Document(map);
//2. List<Document>
documents.add(document);
}
// 3. com.mongodb.client.MongoCollection
//insertMany(List<Document>)
collection.insertMany(documents);
System.out.println(" ");
}
/** **/
public static void delete(MongoCollection<Document> collection,
String fieldName,Object value,boolean isAll){
if(isAll){
//
collection.deleteMany(Filters.eq(fieldName, value));
}else{
//
collection.deleteOne(Filters.eq(fieldName, value));
}
System.out.println(" ");
}
/** **/
public static void select(MongoCollection<Document> collection){
// com.mongodb.client.MongoCollection find() 。
// , 。
//
/** * 1. FindIterable<Document> * 2. MongoCursor<Document> * 3. * */
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
}
/** **/
public static void select(MongoCollection<Document> collection,
String fieldName,Object value){
Bson bson=Filters.eq(fieldName, value);
FindIterable<Document> findIterable = collection.find(bson);
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
}
/** **/
public static void update(MongoCollection<Document> collection,
String fieldName,Object oldValue,Object newValue,boolean isAll){
if(isAll){
// com.mongodb.client.MongoCollection updateMany() 。
collection.updateMany(Filters.eq(fieldName, oldValue), new Document(
"$set",new Document(fieldName,newValue)));
} else{
collection.updateOne(Filters.eq(fieldName, oldValue), new Document(
"$set",new Document(fieldName,newValue)));
}
System.out.println(" ");
}
/** mongodb **/
public static void close(MongoClient client){
client.close();
System.out.println(" !");
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.