노출되는 이미지가 불편하시겠지만 양해를 구합니다. 노출, 클릭등에 관한 자료로 활용 중입니다.


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,  메시지 & 해결 시도 방법

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
블로그 이미지

StartGuide

I want to share the basic to programming of each category and how to solve the error. This basic instruction can be extended further. And I have been worked in southeast Asia more than 3 years. And I want to have the chance to work another country.

,