# 配置

在resource下创建文件:config/mongo.setting

#每个主机答应的连接数(每个主机的连接池大小),当连接池被用光时,会被阻塞住 ,默以为10 --int
connectionsPerHost=100
#线程队列数,它以connectionsPerHost值相乘的结果就是线程队列最大值。如果连接线程排满了队列就会抛出“Out of semaphores to get db”错误 --int
threadsAllowedToBlockForConnectionMultiplier=10
#被阻塞线程从连接池获取连接的最长等待时间(ms) --int
maxWaitTime = 120000
#在建立(打开)套接字连接时的超时时间(ms),默以为0(无穷) --int
connectTimeout=0
#套接字超时时间;该值会被传递给Socket.setSoTimeout(int)。默以为0(无穷) --int
socketTimeout=0
#是否打开长连接. defaults to false --boolean
socketKeepAlive=false
user = root
pass = root
database = admin
#---------------------------------- MongoDB实例连接
[master]
host = 10.0.74.127:27017
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 基础应用

MongoDatabase db = MongoFactory.getDS ("10.0.64.127",27017).getDb ("admin");
        MongoCollection<Document> collection = db.getCollection ("col");

        BsonDocument document = new BsonDocument ();
        Pattern pattern = Pattern.compile("d", Pattern.MULTILINE);
//        document.put ("by", pattern);
        FindIterable<Document> documents = collection.find (new Document ("likes.title",pattern));
        MongoCursor<Document> iterator = documents.iterator ();
        while (iterator.hasNext ()){
            Document next = iterator.next ();
            System.out.println (new JSONObject (next.get ("likes")).get ("title"));
        }
1
2
3
4
5
6
7
8
9
10
11
12

# 通过过滤器查询

field为查询字段,value为查询值,也可以通过过滤器Filters,Filters提供了一系列查询条件的静态方法

collection.find (Filters.in ("likes", ""));
1

# 相等 — =

FindIterable<Document> iter = doc.find(new Document("name","张三"));

// 或者  FindIterable<Document> iter = doc.find(new Document("age",new Document("$eq",24)));

//或者   FindIterable<Document> iter = doc.find(Filters.eq("name", "张三"));

iter.forEach(new Block<Document>() {
  public void apply(Document _doc) {
    System.out.println(_doc.toJson());
  }
});
1
2
3
4
5
6
7
8
9
10
11

# 不等 — !=

// FindIterable<Document> iter = doc.find(new Document("age",new Document("$ne",24)));
FindIterable<Document> iter = doc.find(Filters.ne("name", "张三"));
iter.forEach(new Block<Document>() {
  public void apply(Document _doc) {
    System.out.println(_doc.toJson());
  }
});
1
2
3
4
5
6
7

# 大于 — >

FindIterable<Document> iter = doc.find(new Document("age",new Document("$gt",22)));

//或者   FindIterable<Document> iter = doc.find(Filters.gt("age",22));

iter.forEach(new Block<Document>() {
  public void apply(Document _doc) {
    System.out.println(_doc.toJson());
  }
});
1
2
3
4
5
6
7
8
9

# 大于等于 — >=

用法同上,符号为$gte

# 小于 — <

FindIterable<Document> iter1 = doc.find(new Document("age",new Document("$lt",22)));

//或者   FindIterable<Document> iter = doc.find(Filters.lt("age",22));
iter1.forEach(new Block<Document>() {
  public void apply(Document _doc) {
    System.out.println(_doc.toJson());
  }
});
1
2
3
4
5
6
7
8

# 小于等于 — <=

用法同上,符号为$lte

# 且 — and

FindIterable<Document> iter1 = doc.find(new Document("age",20).append("name", "张三"));
//或者 FindIterable<Document> iter1 = doc.find(Filters.and(Filters.eq("name", "张三"),Filters.lt("age", 30)));
iter1.forEach(new Block<Document>() {
  public void apply(Document _doc) {
    System.out.println(_doc.toJson());
  }
});
1
2
3
4
5
6
7

# 或者 — or

List<Document> list = new ArrayList<Document>();
list.add(new Document("name","张三"));
list.add(new Document("age",24));
FindIterable<Document> iter1 = doc.find(new Document("$or", list));
// FindIterable<Document> iter1 = doc.find(Filters.and(Filters.eq("name", "张三"),Filters.lt("age", 30)));
iter1.forEach(new Block<Document>() {
  public void apply(Document _doc) {
    System.out.println(_doc.toJson());
  }
});
1
2
3
4
5
6
7
8
9
10

# 存在 — in

// List<String> list = new ArrayList<String>();
// list.add("张三");
// list.add("李四");
// FindIterable<Document> iter = doc.find(new Document("name",new Document("$in",list)));
FindIterable<Document> iter = doc.find(Filters.in("name", "张三","李四","王五"));
iter.forEach(new Block<Document>() {
  public void apply(Document _doc) {
    System.out.println(_doc.toJson());
  }
});
1
2
3
4
5
6
7
8
9
10

# 不存在 — not in

用法同上,符号为$nin

# 排序 — sort

FindIterable<Document> iter1 = doc.find().sort(new Document("age",1).append("phone", 1));

//FindIterable<Document> iter1 = doc.find().sort(Sorts.orderBy(Sorts.ascending("age"),Sorts.descending("phone")));
iter1.forEach(new Block<Document>() {
  public void apply(Document _doc) {
    System.out.println(_doc.toJson());
  }
});
1
2
3
4
5
6
7
8