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