MISTERY

PINPOINT 란

Skill/BigData2017.03.02 18:00

PINPOINT란

대규모 분산 시스템의 성능을 분석하고 문제를 진단, 처리하는 java 플랫폼.

네이버에서 2012년 7월 부터 개발을 시작하여 14년 말에 배포하였다.

(PINPOINT 뜻 : 원인을 콕! 집어내다)


기능

 1. 서버 맵 - 대상 서버와 연결된 다른 서버와의 관계 다이어그램

 2. 스캐터 - 요청별 응답시간에 따른 그래프

 3. request detail - 스캐터에서 선택된 요청의 스택트레이스 뷰

 4. 에러 발생 요청 표시 - 에러가 발생한 요청 빨간색으로 표시

 5. 서버의 jar목록 표시 - 서버에 접속하지 않아도 관련된 jar 목록 확인 가능


구성 


 1. DB - HBase(하둡 분산 데이터베이스 기반)

 2. Web UI로 view적으로 보여줌

 3. Collector - Web UI를 보여주는것을 쌓아두는거 , 대상이 되는 타겟 서버의 정보를 HBase가 저장함

 4. Agent - 각각의 대상 서버에 pinpoint agent를 줘서 각각의 pinpoint agent가 collector의 데이터를
    udp/tcp + thrift를 통해서 보내주고 그걸로 디비에 저장하고 그걸 web ui가 보여줌


특징

- 분산 트랜잭션 추적

- 토플로지 다오 발견

- 수평 확장성

- 코드 수준의 가시성


요청/반응 분석 차트

실시간 트래픽 차트

콜 스택 트리(여러 대의 서버를 한번에 볼 수 있다.)

인스펙터(JVM 힙상태 등 여러개 확인 가능)


도입 전 사전 확인

애플리케이션 환경 확인(java version, framework & lib, was 등)


도입 과정에서 고민했던 내용

HBase 이중화? 샤딩? Hadoop? 구성을 해야하는 가?

(핀 포인트 데이터의 경우 실 서버 데이터가 아니라 날라가도 문제 없다고 판단했기 때문에)








출처 : https://yangbongsoo.gitbooks.io/study/content/pinpoint.html

   <기초적이지만, 제법 괜찮은 자료들이 많습니다>








저작자 표시
신고

Comment +0


package com.wujintao.mongo;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import org.junit.Test;

import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;
import com.mongodb.Mongo;
import com.mongodb.QueryBuilder;
import com.mongodb.WriteConcern;

public class TestCase {
       //DBCursor cursor = coll.find(condition).addOption(Bytes.QUERYOPTION_NOTIMEOUT);//설정 시간 커서 마.

	@Test
	/**
	 * 모든 데이터베이스 인스턴스 가져오기
	 */
	public void testGetDBS() {
		List<String> dbnames = MongoUtil.getMong().getDatabaseNames();
		for (String dbname : dbnames) {
			System.out.println("dbname:" + dbname);
		}
	}

	@Test
	/**
	 * 삭제 데이터베이스
	 */
	public void dropDatabase() {
		MongoUtil.getMong().dropDatabase("my_new_db");
	}

	@Test
	/**
	 * 쿼리 모든 시계
	 */
	public void getAllCollections() {
		Set<String> colls = MongoUtil.getDB().getCollectionNames();
		for (String s : colls) {
			System.out.println(s);
		}
	}

	@Test
	public void dropCollection() {
		MongoUtil.getColl("jellonwu").drop();
	}

	/**
	 * 한 개의 추가 기록
	 */
	@Test
	public void addData() {
		DBCollection coll = MongoUtil.getColl("wujintao");
		BasicDBObject doc = new BasicDBObject();
		doc.put("name", "MongoDB");
		doc.put("type", "database");
		doc.put("count", 1);

		BasicDBObject info = new BasicDBObject();
		info.put("x", 203);
		info.put("y", 102);
		doc.put("info", info);
		coll.insert(doc);
		// 설정 write concern 얻을 수 있도록 운영 실패 때 물어봅니다
		coll.setWriteConcern(WriteConcern.SAFE);
	}

	@Test
	/**
	 * 색인 생성
	 */
	public void createIndex() {
		MongoUtil.getColl("wujintao").createIndex(new BasicDBObject("i", 1));
	}

	@Test
	/**
	 * 인덱스 정보 가져오기
	 */
	public void getIndexInfo() {
		List<DBObject> list = MongoUtil.getColl("hems_online").getIndexInfo();
		for (DBObject o : list) {
			System.out.println(o);
		}
	}

	@Test
	/**
	 * 여러 가지 추가 기록
	 */
	public void addMultiData() {
		for (int i = 0; i <100; i++) {
			MongoUtil.getColl("wujintao").insert(
					new BasicDBObject().append("i", i));
		}

		List<DBObject> docs = new ArrayList<DBObject>();
		for (int i = 0; i <50; i++) {
			docs.add(new BasicDBObject().append("i", i));
		}
		MongoUtil.getColl("wujintao").insert(docs);
		// 설정 write concern 얻을 수 있도록 운영 실패 때 물어봅니다
		MongoUtil.getColl("wujintao").setWriteConcern(WriteConcern.SAFE);
	}

	@Test
	/**
	 * 기록 중 첫 번째
	 */
	public void findOne() {
		DBObject myDoc = MongoUtil.getColl("wujintao").findOne();
		System.out.println(myDoc);
	}

	@Test
	/**
	 * 시계 가져오는 중 모든 기록이 가지 개수
	 */
	public void count() {
		System.out.println(MongoUtil.getColl("wujintao").getCount());
		System.out.println(MongoUtil.getColl("wujintao").count());
	}

	@Test
	/**
	 * 가져오기 조회 결과 집합 기록 세다.
	 */
	public void getCount() {
		DBObject query = new BasicDBObject("name", "a");
		long count = MongoUtil.getColl("wujintao").count(query);
		System.out.println(count);
	}

	@Test
	/**
	 * 모든 결과는 조회
	 */
	public void getAllDocuments() {
		DBCursor cursor = MongoUtil.getColl("wujintao").find();
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	@Test
	/**
	 * 하나의 조건 그대로 조회
	 */
	public void queryByConditionOne() {
		BasicDBObject query = new BasicDBObject();
		query.put("name", "MongoDB");
		DBCursor cursor = MongoUtil.getColl("wujintao").find(query);

		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	@Test
	/**
	 * AND 많이 조건 조회, 구간 조회
	 */
	public void queryMulti() {
		BasicDBObject query = new BasicDBObject();
		// 쿼리 j 안 은 3 k 크다 10 결과 집합
		query.put("j", new BasicDBObject("$ne", 3));
		query.put("k", new BasicDBObject("$gt", 10));
		DBCursor cursor = MongoUtil.getColl("wujintao").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	@Test
	/**
	 * 구간 조회
	 * select * from table where i >50
	 */
	public void queryMulti2() {
		BasicDBObject query = new BasicDBObject();
		query = new BasicDBObject();
		query.put("i", new BasicDBObject("$gt", 50)); // e.g. find all where i >
		DBCursor cursor = MongoUtil.getColl("wujintao").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	@Test
	/**
	 * 구간 조회
	 * select * from table where 20 <i <= 30
	    //비교적 부호   
	    //"$gt":  크다   
	    //"$gte": 다음보다 크거나 같음   
	    //"$lt":  작다   
	    //"$lte": 다음보다 작거나 같음   
	    //"$in":  포함   
	 */
	public void queryMulti3() {
		BasicDBObject query = new BasicDBObject();
		query = new BasicDBObject();

		query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30));
		DBCursor cursor = MongoUtil.getColl("wujintao").find(query);
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	/**
	 * 그룹 in, and select * from test_Table where (a=5 or b=6) and (c=5 or d = 6)
	 */
	public void queryMulti4() {
		BasicDBObject query11 = new BasicDBObject();
		query11.put("a", 1);
		BasicDBObject query12 = new BasicDBObject();
		query12.put("b", 2);
		List<BasicDBObject> orQueryList1 = new ArrayList<BasicDBObject>();
		orQueryList1.add(query11);
		orQueryList1.add(query12);
		BasicDBObject orQuery1 = new BasicDBObject("$or", orQueryList1);

		BasicDBObject query21 = new BasicDBObject();
		query21.put("c", 5);
		BasicDBObject query22 = new BasicDBObject();
		query22.put("d", 6);
		List<BasicDBObject> orQueryList2 = new ArrayList<BasicDBObject>();
		orQueryList2.add(query21);
		orQueryList2.add(query22);
		BasicDBObject orQuery2 = new BasicDBObject("$or", orQueryList2);

		List<BasicDBObject> orQueryCombinationList = new ArrayList<BasicDBObject>();
		orQueryCombinationList.add(orQuery1);
		orQueryCombinationList.add(orQuery2);

		BasicDBObject finalQuery = new BasicDBObject("$and",
				orQueryCombinationList);
		DBCursor cursor = MongoUtil.getColl("wujintao").find(finalQuery);
	}

	@Test
	/**
	 * IN 조회
	 * if i need to query name in (a,b); just use { name : { $in : ['a', 'b'] } }
	 * select * from things where name='a' or name='b'
	 * @param coll
	 */
	public void queryIn() {
		BasicDBList values = new BasicDBList();
		values.add("a");
		values.add("b");
		BasicDBObject in = new BasicDBObject("$in", values);
		DBCursor cursor = MongoUtil.getColl("wujintao").find(
				new BasicDBObject("name", in));
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}

	@Test
	/**
	 * 또는 조회
	 * select * from table where name  = '12' or title = 'p'
	 * @param coll
	 */
	public void queryOr() {
		QueryBuilder query = new QueryBuilder();
		query.or(new BasicDBObject("name", 12), new BasicDBObject("title", "p"));
		DBCursor cursor = MongoUtil.getColl("wujintao").find(query.get()).addSpecial("$returnKey", "");
		try {
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
		} finally {
			cursor.close();
		}
	}
	
	@Test
	public void customQueryField() throws UnknownHostException{
        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("zhongsou_ad");
        BasicDBObjectBuilder bulder = new BasicDBObjectBuilder();
        bulder.add("times",1);
        bulder.add("aid",1);
        DBCursor cusor =  db.getCollection("ad_union_ad_c_1").find(new BasicDBObject(),bulder.get());
        for (DBObject dbObject : cusor) {
            System.out.println(dbObject);
        }
	}
	
	@Test
	public void mapReduce() throws UnknownHostException{
        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("zhongsou_ad");
        /***
         *  book1 = {name : "Understanding JAVA", pages : 100}
         *  book2 = {name : "Understanding JSON", pages : 200}
         *  db.books.save(book1)
         *  db.books.save(book2)
         *  book = {name : "Understanding XML", pages : 300}
         *  db.books.save(book)
         *  book = {name : "Understanding Web Services", pages : 400}
         *  db.books.save(book)
         *  book = {name : "Understanding Axis2", pages : 150}
         *  db.books.save(book)  
         *  
        var map = function() {
            var category;
            if ( this.pages >= 250 )
                category = 'Big Books';
            else
                category = "Small Books";
            emit(category, {name: this.name});
        };
        var reduce = function(key, values) {
            var sum = 0;
            values.forEach(function(doc) {
                sum += 1;
            });
            return {books: sum};
        };       
        var count  = db.books.mapReduce(map, reduce, {out: "book_results"});
         */
        try {

            DBCollection books = db.getCollection("books");

            BasicDBObject book = new BasicDBObject();
            book.put("name", "Understanding JAVA");
            book.put("pages", 100);
            books.insert(book);
            
            book = new BasicDBObject();  
            book.put("name", "Understanding JSON");
            book.put("pages", 200);
            books.insert(book);
            
            book = new BasicDBObject();
            book.put("name", "Understanding XML");
            book.put("pages", 300);
            books.insert(book);
            
            book = new BasicDBObject();
            book.put("name", "Understanding Web Services");
            book.put("pages", 400);
            books.insert(book);
          
            book = new BasicDBObject();
            book.put("name", "Understanding Axis2");
            book.put("pages", 150);
            books.insert(book);
            
            String map = "function() { "+ 
                      "var category; " +  
                      "if ( this.pages >= 250 ) "+  
                      "category = 'Big Books'; " +
                      "else " +
                      "category = 'Small Books'; "+  
                      "emit(category, {name: this.name});}";
            
            String reduce = "function(key, values) { " +
                                     "var sum = 0; " +
                                     "values.forEach(function(doc) { " +
                                     "sum += 1; "+
                                     "}); " +
                                     "return {books: sum};} ";
            
            MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,
              null, MapReduceCommand.OutputType.INLINE, null);

            MapReduceOutput out = books.mapReduce(cmd);

            for (DBObject o : out.results()) {
             System.out.println(o.toString());
            }
           } catch (Exception e) {
             e.printStackTrace();
           }
	}
	
	@Test
    public void GroupByManyField() throws UnknownHostException{
	    //이 방법은 없어 실행 성공
        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("libary");
        DBCollection books = db.getCollection("books");
        BasicDBObject groupKeys = new BasicDBObject();
        groupKeys.put("total", new BasicDBObject("$sum","pages"));
        
        BasicDBObject condition = new BasicDBObject();
        condition.append("pages", new BasicDBObject().put("$gt", 0));
   
        
        String reduce = "function(key, values) { " +
                "var sum = 0; " +
                "values.forEach(function(doc) { " +
                "sum += 1; "+
                "}); " +
                "return {books: sum};} ";
        /**
         BasicDBList basicDBList = (BasicDBList)db.getCollection("mongodb 에서 집합 인코딩 또는 코드")
                   .group(DBObject key,   --그룹 필드 즉 group by 필드
                DBObject cond,        --검색 중 where 조건
                DBObject initial,     --필드 값 초기화 각
                String reduce,        --모든 그룹 모두 필요한 실행할 Function
                String finial -- 종결 Funciton 결과에 대한 최종 처리
         */
        DBObject obj = books.group(groupKeys, condition,  new BasicDBObject(), reduce);
        System.out.println(obj);
        
        AggregationOutput ouput = books.aggregate(new BasicDBObject("$group",groupKeys));
        System.out.println(ouput.getCommandResult());
        System.out.println(books.find(new BasicDBObject("$group",groupKeys)));
    }	
	

	@Test
	/**
	 * 페이지 검색   
	 */
	public void pageQuery() {
		DBCursor cursor = MongoUtil.getColl("wujintao").find().skip(0)
				.limit(10);
		while (cursor.hasNext()) {
			System.out.println(cursor.next());
		}
	}

	/**
	 * 퍼지 조회
	 */
	public void likeQuery() {
		Pattern john = Pattern.compile("joh?n");
		BasicDBObject query = new BasicDBObject("name", john);

		// finds all people with "name" matching /joh?n/i
		DBCursor cursor = MongoUtil.getColl("wujintao").find(query);
	}

	@Test
	/**
	 * 조건이 삭제   
	 */
	public void delete() {
		BasicDBObject query = new BasicDBObject();
		query.put("name", "xxx");
		// 그리고 다시 찾을 삭제 및 삭제 대상
		DBObject removeObj = MongoUtil.getColl("wujintao").findAndRemove(query);
		System.out.println(removeObj);
	}

	@Test
	/**
	 * 업데이트
	 */
	public void update() {
		BasicDBObject query = new BasicDBObject();
		query.put("name", "liu");
		DBObject stuFound = MongoUtil.getColl("wujintao").findOne(query);
		stuFound.put("name", stuFound.get("name") + "update_1");
		MongoUtil.getColl("wujintao").update(query, stuFound);
	}


}

 

이 안에 있는 MongoUtil.java 걸친 다음과 같다:

package com.wujintao.mongo;

import java.net.UnknownHostException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;


/**
 * to see: 
 * Mongo 도구 클래스: 디자인 한 사례 모드 위해 때마다 월 변화가 데이터베이스 연결 이름 변화가 있으면, 이것은 비즈니스 규칙
 * MongoDB 자바 때문에 구동 것은 스레드 안전, 일반 응용 대해 단 한 Mongo 인스턴스 바로, Mongo 개 내장 연결 풀 있다 (풀 크기 기본값은 10개). 
 * 에 대해 많은 글을 읽기 및 환경 속에서 확보를 위해 한 정보 중 사용 같은 DB 때 우리는 쓸 수 있는 이하 방식 보증 일관성: 
 *	 DB mdb = mongo.getDB('dbname');
 *	 mdb.requestStart();
 *	 // 업무 코드
 *	 mdb.requestDone();
 * DB 및 DBCollection 절대 스레드 안전한
 * @author wujintao
 */
public class MongoUtil{
	
	private static Mongo mongo;
	private static DBCollection coll;
	private static Log log = LogFactory.getLog(MongoUtil.class);
	private static DB db;
	
	static{
		try {
		      MongoOptions options = new MongoOptions();
                      options.autoConnectRetry = true;
                      options.connectionsPerHost = 1000;
                      options.maxWaitTime = 5000;
                      options.socketTimeout = 0;
                      options.connectTimeout = 15000;
                      options.threadsAllowedToBlockForConnectionMultiplier = 5000;
			//사실 Mongo 인스턴스 대표 한 데이터베이스 연결 지, 설령 다중 스레드 환경 속에서 한 Mongo 인스턴스 우리한테 충분해요
			mongo = new Mongo(new ServerAddress(DBMongoConfig.getHost(),DBMongoConfig.getPort()),options);
			//mongo = new Mongo(DBMongoConfig.getHost(),DBMongoConfig.getPort());
			// or, to connect to a replica set, supply a seed list of members
			// Mongo m = new Mongo(Arrays.asList(new ServerAddress("localhost",
			// 27017),
			// new ServerAddress("localhost", 27018),
			// new ServerAddress("localhost", 27019)));

			// 주의 Mongo 이미 실현되었다 연결 지 고 은 스레드 안전한. 
			// 대부분의 사용자가 사용 mongodb 모두 안전 속그물 아래 하지만 만약 것이다 mongodb 신설 안전 검증 모드 위해 필요하다고 지금 클라이언트 사용자 이름과 비밀번호: 
			// boolean auth = db.authenticate(myUserName, myPassword);
		} catch (UnknownHostException e) {
			log.info("get mongo instance failed");
		}
	}
	
	public static DB getDB(){
		if(db==null){
			db = mongo.getDB(DBMongoConfig.getDbname());
		}
		return db;
	}
	
	
	public static Mongo getMong(){
		return mongo;
	}
	
	public static DBCollection getColl(String collname){
		return getDB().getCollection(collname);
	}
	
}

 















참조 : http://www.programkr.com/blog/METM0ADMwYT4.html













신고

Comment +0

시퀀스를 임의 생성하여 사용하는 방법입니다.

1. 시퀀스 용 Collection 생성
>db.createCollection("seq", {crapped: false });

2. 시퀀스 등록
>db.seq.insert({"_id":"seq_post", "seq":new NumberLong(1)});

 ( seq 값으로 1 대신에 NumberLong 값으로 넣길 바란다. ( float 형으로 처리됨을 방지 ) )

3. 시퀀스 가져오기
>db.seq.findAndModify({    query: {"_id":"seq_post"},    update: {$inc: {"seq":1}},    new: true});


이렇게 해서 "seq_post" 의 시퀀스를 가져오게 된다.


이렇게 되어있는것을 java 에서는 어떻게 가져올수 있을까?
spring-data-mongodb-1.0.0.M3.jar 를 사용하는 환경에서의 Sequece 가져오기 입니다.

/**
* 시퀀스 발급
* @param seqName
* @return
*/
public Double getSequence(String seqName){
MongoOperations mo = (MongoOperations)mongoTpl;
DBCollection coll = mo.getCollection("seq");
BasicDBObject queryObj = new BasicDBObject();
queryObj.append("_id", seqName);
BasicDBObject updateObj = new BasicDBObject();
BasicDBObject incObj = new BasicDBObject();
incObj.put("seq", 1);
updateObj.put("$inc", incObj);
DBObject result = coll.findAndModify(queryObj, updateObj);
Double seq = (Double)result.get("seq");
return seq;
}
spring-data-mongodb 를 사용하지 않을 경우에는 

Mongo m = new Mongo("58.226.93.246", 27017);
DB db = m.getDB("[DB명]");
DBCollection coll = db.getCollection("seq");
이런 과정으로 DBCollection 을 얻어와 작업을 하면 되겠습니다.





















출처 : http://blog.naver.com/endstar7/100134889683




















신고

Comment +0

MongoDB - Java Driver

CRUD in Java

  1. insert()
  2. find()
  3. update()
  4. remove()

DBObject is a main interface used by Java driver. Essentially a key/value data store.

BasicDBObject is a general purpose implementation which extendsLinkedHashMap.

Example:

import com.mongodb.BasicDBObject;

public class BasicDBObjectExample {

    public static void main(String[] args) {
        BasicDBObject basicDBObject = new BasicDBObject();
        basicDBObject.put("name","Shiv");
        basicDBObject.put("age",33);
        basicDBObject.put("address",new BasicDBObject("street","East Street")
                .append("block","2nd block")
        );
        System.out.println(basicDBObject);
    }
}

Output

{ "name" : "Shiv" , "age" : 33 , "address" : { "street" : "East Street" , "block" : "2nd block"}}

Insert

Example:

import com.mongodb.*;

import java.net.UnknownHostException;

public class InsertExample {

    public static void main(String[] args) throws UnknownHostException {
        Mongo client = new Mongo();
        DB test = client.getDB("test");
        DBCollection users = test.getCollection("users");

        DBObject newUser = new BasicDBObject().append("name","clark");

        System.out.println("Before insert - " + newUser);

        users.insert(newUser);

        System.out.println("After insert (Notice _id field) - " + newUser);
    }
}

Output:

Before insert - { "name" : "clark"}
After insert (Notice _id field) - { "name" : "clark" , "_id" : { "$oid" : "51c5a08e036476fa1d8ba516"}}
  1. MongoDB .insert() would add _id field if doc isn't having one.
  2. If you try to insert again the same document (same id), you would get an _MongoException$DuplicateKey exception.
  3. You can even insert a list of document.

Find

Example:

import com.mongodb.*;

import java.net.UnknownHostException;

public class FindExample {

    public static void main(String[] args) throws UnknownHostException {
        Mongo client = new Mongo();
        DB test = client.getDB("test");
        DBCollection people = test.getCollection("people");

        System.out.println("Finds any object from collection - " + people.findOne());
        System.out.println("Collection Count - "+people.count());

        try(DBCursor cursor = people.find()){
            for (DBObject dbObject : cursor) {
                System.out.println(dbObject);
            }
            System.out.println("Count - "+cursor.count());
        }
    }
}

Output:

Finds any object from collection - { "_id" : { "$oid" : "51b314985fb1aa4bb04952d0"} , "age" : 21.0 , "name" : "Bobby" , "title" : "Dr"}
{ "_id" : { "$oid" : "51b314985fb1aa4bb04952d0"} , "age" : 21.0 , "name" : "Bobby" , "title" : "Dr"}
{ "_id" : { "$oid" : "51b3149d5fb1aa4bb04952d1"} , "name" : "Chris" , "title" : "Dr"}
{ "_id" : { "$oid" : "51b314a15fb1aa4bb04952d2"} , "name" : "Don" , "title" : "Dr"}
Count - 3
  1. .findOne() - is similar to the one available on shell. Returns a random object from collection.
  2. .find() - This returns a cursor reference. Since DBCursor is implementing Iterator, we can use next(),hasNext() etc methods.
  3. .count() - This gives us no of docs in a cursor.

Query using Criteria

Sample Collection:

> db.accounts.find();
{ "_id" : ObjectId("51b31e1cd8f5f032e5970603"), "name" : "Howard", "favorites" : [ "pretzels", "beer" ] }
{ "_id" : ObjectId("51b31e2ed8f5f032e5970604"), "name" : "George", "favorites" : [ "pretzels", "ice cream" ] }
{ "_id" : ObjectId("51b31f38d8f5f032e5970605"), "name" : "Sam", "favorites" : "beer" }
{ "_id" : ObjectId("51b337c8d8f5f032e5970606"), "name" : "John", "favorites" : [ "beer", "cheese" ] }
{ "_id" : ObjectId("51b337e2d8f5f032e5970607"), "name" : "Irving", "favorites" : [ "beer", "cheese", "pretzels" ] }

Example:

import com.mongodb.*;

import java.net.UnknownHostException;
import java.util.Arrays;

public class QueryCriteriaExample {

    public static void main(String[] args) throws UnknownHostException {
        Mongo client = new Mongo();
        DB test = client.getDB("test");
        DBCollection accounts = test.getCollection("accounts");

        DBObject query = new BasicDBObject("favorites",new BasicDBObject("$all",Arrays.asList("beer")));
        System.out.println("Using BasicDBObject - ");
        try(DBCursor cursor = accounts.find(query)){
            for (DBObject dbObject : cursor) {
                System.out.println(dbObject);
            }
        }

        QueryBuilder builder = QueryBuilder.start("favorites").all(Arrays.asList("beer"));
        System.out.println("Using QueryBuilder - ");
        try(DBCursor cursor = accounts.find(builder.get())){
            for (DBObject dbObject : cursor) {
                System.out.println(dbObject);
            }
        }
    }
}

Output:

Using BasicDBObject - 
{ "_id" : { "$oid" : "51b31e1cd8f5f032e5970603"} , "name" : "Howard" , "favorites" : [ "pretzels" , "beer"]}
{ "_id" : { "$oid" : "51b31f38d8f5f032e5970605"} , "name" : "Sam" , "favorites" : "beer"}
{ "_id" : { "$oid" : "51b337c8d8f5f032e5970606"} , "name" : "John" , "favorites" : [ "beer" , "cheese"]}
{ "_id" : { "$oid" : "51b337e2d8f5f032e5970607"} , "name" : "Irving" , "favorites" : [ "beer" , "cheese" , "pretzels"]}
Using QueryBuilder - 
{ "_id" : { "$oid" : "51b31e1cd8f5f032e5970603"} , "name" : "Howard" , "favorites" : [ "pretzels" , "beer"]}
{ "_id" : { "$oid" : "51b31f38d8f5f032e5970605"} , "name" : "Sam" , "favorites" : "beer"}
{ "_id" : { "$oid" : "51b337c8d8f5f032e5970606"} , "name" : "John" , "favorites" : [ "beer" , "cheese"]}
{ "_id" : { "$oid" : "51b337e2d8f5f032e5970607"} , "name" : "Irving" , "favorites" : [ "beer" , "cheese" , "pretzels"]}
  1. Query by example - Using BasicDBObject, we can create a query and pass it on to find method.
  2. QueryBuilder - Provides various functions to build query.

Example:

import com.mongodb.*;

import java.net.UnknownHostException;
import java.util.Arrays;

public class FieldSelectionExample {

    public static void main(String[] args) throws UnknownHostException {
        Mongo client = new Mongo();
        DB test = client.getDB("test");
        DBCollection accounts = test.getCollection("accounts");

        QueryBuilder builder = QueryBuilder.start("favorites").all(Arrays.asList("beer"));
        try(DBCursor cursor = accounts.find(builder.get(),new BasicDBObject("name",true).append("_id",0))){
            for (DBObject dbObject : cursor) {
                System.out.println(dbObject);
            }
        }
    }
}

Output:

{ "name" : "Howard"}
{ "name" : "Sam"}
{ "name" : "John"}
{ "name" : "Irving"}
  1. Field Selection - By providing a list of keys to be returned in result.

Example:

import com.mongodb.*;

import java.net.UnknownHostException;

public class DotNotationExample {

    public static void main(String[] args) throws UnknownHostException {
        Mongo client = new Mongo();
        DB test = client.getDB("test");
        DBCollection accounts = test.getCollection("users");

        QueryBuilder builder = QueryBuilder.start("address.zip").is(51605);
        try (DBCursor cursor = accounts.find(builder.get())) {
            for (DBObject dbObject : cursor) {
                System.out.println(dbObject);
            }
        }
    }
}

Output:

{ "_id" : "shiv" , "address" : { "street" : "East street" , "block" : "2nd block" , "zip" : 51605.0}}
{ "_id" : "neo" , "address" : { "street" : "West street" , "block" : "3rd block" , "zip" : 51605.0}}
  1. Using . notation - In case of nested documents, using a dot notation we can provide query criteria as well as in field selection.

Sample Collection

> db.users.find()
{ "_id" : "clark" }
{ "_id" : "shiv", "address" : { "street" : "East street", "block" : "2nd block", "zip" : 51605 } }
{ "_id" : "neo", "address" : { "street" : "West street", "block" : "3rd block", "zip" : 51605 } }
{ "_id" : "sam", "address" : { "street" : "Main street", "block" : "1st block", "zip" : 21605 } }

Example:

import com.mongodb.*;

import java.net.UnknownHostException;

public class SortSkipExample {

    public static void main(String[] args) throws UnknownHostException {
        Mongo client = new Mongo();
        DB test = client.getDB("test");
        DBCollection accounts = test.getCollection("users");

        QueryBuilder builder = QueryBuilder.start();
        DBCursor cursor = accounts.find(builder.get())
                .sort(new BasicDBObject("_id",-1))
                .skip(1)
                .limit(3);
        try{
            for (DBObject dbObject : cursor) {
                System.out.println(dbObject);
            }
        }finally {
            cursor.close();
        }
    }
}

Output:

{ "_id" : "sam" , "address" : { "street" : "Main street" , "block" : "1st block" , "zip" : 21605.0}}
{ "_id" : "neo" , "address" : { "street" : "West street" , "block" : "3rd block" , "zip" : 51605.0}}
{ "_id" : "clark"}
  1. .sort() .skip() .limit() - These method are chained method calls on .find() method which returns a cursor.

Update

Example:

import com.mongodb.*;
import java.net.UnknownHostException;

public class UpdateExample {
    public static void main(String[] args) throws UnknownHostException {
        Mongo client = new Mongo();
        DB test = client.getDB("test");
        DBCollection users = test.getCollection("users");

        // replaces existing documents
        users.update(new BasicDBObject("_id","clark"),new BasicDBObject("age",40));

        // to add field to existing document
        users.update(new BasicDBObject("_id","clark"),new BasicDBObject("$set",new BasicDBObject("sex","M")));

        // since mark doesn't exist below will not have any affect
        users.update(new BasicDBObject("_id","mark"),new BasicDBObject("$set",new BasicDBObject("sex","M")));

        // but if we use upsert or saveOrUpdate()
        // update() api will insert new row
        users.update(new BasicDBObject("_id","rahul"),new BasicDBObject("$set",new BasicDBObject("sex","M")),true,false);

        // default behaviour of update() is that it will only update 1 row
        // if you would like to update more records, then the multi param comes in
        users.update(new BasicDBObject(),new BasicDBObject("$set",new BasicDBObject("title","Dr.")),false,true);


        printAll(users);
    }

    private static void printAll(DBCollection users) {
        QueryBuilder builder = QueryBuilder.start();
        DBCursor cursor = users.find(builder.get());
        try{
            for (DBObject dbObject : cursor) {
                System.out.println(dbObject);
            }
        }finally {
            cursor.close();
        }
    }
}

Output:

{ "_id" : "shiv" , "address" : { "street" : "East street" , "block" : "2nd block" , "zip" : 51605.0} , "title" : "Dr."}
{ "_id" : "neo" , "address" : { "street" : "West street" , "block" : "3rd block" , "zip" : 51605.0} , "title" : "Dr."}
{ "_id" : "sam" , "address" : { "street" : "Main street" , "block" : "1st block" , "zip" : 21605.0} , "title" : "Dr."}
{ "_id" : "clark" , "age" : 40 , "sex" : "M" , "title" : "Dr."}
{ "_id" : "rahul" , "sex" : "M" , "title" : "Dr."}
  1. Update() api works similar to shell update function.
  2. By default it will only update first matching record unless you specify multi = true.
  3. You can make update behave like a saveOrUpdate() using upsert = true.
  4. If you didn't use $set operator, the new document will replace existing document.

Remove

Example:

import com.mongodb.*;

import java.net.UnknownHostException;

public class RemoveExample {
    public static void main(String[] args) throws UnknownHostException {
        Mongo client = new Mongo();
        DB test = client.getDB("test");
        DBCollection users = test.getCollection("users");

        // removes all records matching criteria
        users.remove(new BasicDBObject("_id","clark"));

        printAll(users);
    }

    private static void printAll(DBCollection users) {
        QueryBuilder builder = QueryBuilder.start();
        DBCursor cursor = users.find(builder.get());
        try{
            for (DBObject dbObject : cursor) {
                System.out.println(dbObject);
            }
        }finally {
            cursor.close();
        }
    }
}

Output:

{ "_id" : "shiv" , "address" : { "street" : "East street" , "block" : "2nd block" , "zip" : 51605.0} , "title" : "Dr."}
{ "_id" : "neo" , "address" : { "street" : "West street" , "block" : "3rd block" , "zip" : 51605.0} , "title" : "Dr."}
{ "_id" : "sam" , "address" : { "street" : "Main street" , "block" : "1st block" , "zip" : 21605.0} , "title" : "Dr."}
{ "_id" : "rahul" , "sex" : "M" , "title" : "Dr."}
  1. The main difference between default behavior of remove() and update() api is that the remove() will remove all documents which match criteria whereas update() only removes first document which matches criteia.
  2. Another api findAndModify() allows you to find and modify the documents in same transaction.















출처 : http://scriptogr.am/shiv/post/mongodb-java-driver


















신고

Comment +0

Question.


I have to find a kind of "_id" in my Mongo, I can do it using the Mongo shell, and I can not do that using Perl API.

I'm trying to do it (mongo shell):

./mongo
use my_db
db.my_collection.find({_id : ObjectId("4d2a0fae9e0a3b4b32f70000")})

It works!(returns), but I can't do that using Perl API,

$mongo->my_db->my_collection(find({_id => "ObjectId(4d2a0fae9e0a3b4b32f70000"}));

Does not work because "ObjectId" is not a string, but if you do,

./mongo
use my_db
db.my_collection.find({_id : "ObjectId(4d2a0fae9e0a3b4b32f70000)"})

Does not work too, I'm guess Perl API are doing it ^

Now, I have to know how I do it:

db.my_collection.find({_id : ObjectId("4d2a0fae9e0a3b4b32f70000")})

using Perl API.



Answer 1.

The implementation seems changed.

$mongo->my_db->my_collection(
  find({ _id => MongoDB::OID->new(value => "4d2a0fae9e0a3b4b32f70000")})
);


Answer 2.

I found the solution, you have to do:

$mongo->my_db->my_collection(find({ _id => $mongo->oid("4d2a0fae9e0a3b4b32f70000")}));





















출처 : http://stackoverflow.com/questions/4659437/how-to-use-find-to-search-id-objectidid-in-perl-api


















신고

Comment +2

  • db.my_collection.find({_id : ObjectId("4d2a0fae9e0a3b4b32f70000")})

    꼭 기억하자!!

  • Reference
    http://docs.mongodb.org/manual/reference/object-id/
    http://bloodguy.tistory.com/m/post/814
    http://docs.mongodb.org/manual/reference/method/db.collection.find/
    http://entireboy.egloos.com/viewer/4747571
    http://tiger5net.egloos.com/viewer/5710514
    http://docs.mongodb.org/manual/tutorial/create-a-unique-index/

티스토리 툴바