时间:2021-07-01 10:21:17 帮助过:116人阅读
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
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());
}
});
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());
}
});
// 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());
}
});
用法同上,符号为$nin
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());
}
});
java操作mongodb——查询数据
标签:操作 lis string hone asc ase 等于 集合 filters