java操作mongodb——查询数据

通过find方法查询集合中的文档信息

--------------------------------------------------------

find()  

查询所有文档信息,返回FindIterable<Document>

我们可以通过FindIterable的forEach方法取得document信息

MongoClient mongoClient = new MongoClient();

MongoDatabase db = mongoClient.getDatabase("test");
MongoCollection<Document> doc = db.getCollection("text");

FindIterable<Document> iter = doc.find();
iter.forEach(new Block<Document>() {
  public void apply(Document _doc) {
    System.out.println(_doc.toJson());
  }
});

 

--------------------------------------------------------------------

 

条件查询

new Document( <field>, <value> )

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

 

相等 — =

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());
  }
});

 

不等 — !=

// 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());
  }
});

 

大于 — >

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());
  }
});

 

大于等于 — >=

用法同上,符号为$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());
  }
});

 

小于等于 — <=

用法同上,符号为$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());
  }
});

 

或者 — 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());
  }
});

 

存在 — 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());
  }
});

 

不存在 — 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());
  }
});

 

 

 



 

posted @ 2016-08-23 09:54  Simple°  阅读(49043)  评论(1编辑  收藏  举报