Java Code for mongoDB Query
1. MongoCursor 생성
Document docQuery = new Document(); docQuery.put("key1","value1"); MongoCursor<Document> mc = collection.find(docQuery).iterator(); Document curDoc = mc.next; ... mc.close(); // cursor close |
2. MongoClient 생성
List<MongoCredential> credentialList = new ArrayList<>(); MongoCredential credential = MongoCredential.createCredential(_USER, "admin",_PWD.toCharArray() ); credentialList.add(credential); ServerAddress svr = new ServerAddress(_SVR, _PORT); mongoClient = new MongoClient(svr, credentialList); ... mongoClient.close(); // 연결 닫기 |
3. MongoCollection 생성
MongoDatabase db = mongoClient.getDatabase(dbName); MongoCollection<Document> collection = db.getColleciton(collectionName); |
* ISODate, docQuery, 자료 목록 가져오기
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); Date gtDate = sdf.parse("2018-06-04T00:00:00.000Z"); Document docQuery = new Document(); docQuery.put("reg_dt", new Document("$gt", gtDate) ); |
* ISODate, JSON -> Document, 자료 추가 하기
String jsonData = "{" + " 'key1' : '"+ curDoc.get("key1")+"',"+ + " 'key2' : '"+ curDoc.get("key2")+"'"+ "}"; Document docInsert = Document.parse(jsonData); docInsert("reg_dt", regDate); collection.insertOne(docInsert); |
* Error 1, 메시지 & 해결 시도 방법
Q |
A pipeline stage specification object must contain exactly one field. |
A |
You should make a pair every pipeline in {}. db.collection.aggregation( { $group: ... }, { $match: ...} ) |
'DBMS, noSQL > mongoDB' 카테고리의 다른 글
Robo 3T(Robomongo) 폰트 변경 (0) | 2018.10.24 |
---|---|
CentOS에 MongoDB 64bit 설치하기 (0) | 2017.02.07 |
사용자 계정 관리 (0) | 2017.02.07 |