测试JDK:1.8
测试系统:WIN10
部分转载于:http://www.runoob.com/mongodb/mongodb-operators.html 的笔记
MongoClient mongoClient = new MongoClient();
MongoDatabase mongoDatabase = mongoClient.getDatabase("数据库名");
MongoCollection mongoCollection = mongoDatabase.getCollection("数据库下的集合");
//-------------------------基本的搜索-----------------------------------
//类似于SQL的"where 搜索的列名 > 100"
Document 搜索条件 = new Document("搜索的列名",new Document("$gt",100));
//类似于SQL的"where 搜索的列名 >= 100"
Document 搜索条件 = new Document("搜索的列名",new Document("$gte",100));
//类似于SQL的"where 搜索的列名 < 100"
Document 搜索条件 = new Document("搜索的列名",new Document("$lt",100));
//类似于SQL的"where 搜索的列名 <= 100"
Document 搜索条件 = new Document("搜索的列名",new Document("$lte",100));
//类似于SQL的"where 搜索的列名 != 100"
Document 搜索条件 = new Document("搜索的列名",new Document("$ne",100));
//类似于SQL的"where 搜索的列名 = 100"
Document 搜索条件 = new Document("搜索的列名",new Document("$eq",100));
//类似于SQL的"where 搜索的列名 > 100 and 搜索的列名 < 100"
Document 搜索条件 = new Document("搜索的列名",new Document("$gt",100)).append("搜索的列名",new Document("$lt",100));
//-------------------------基本的搜索-----------------------------------
//-------------------------较高级的搜索-----------------------------------
//类似于SQL的"where 搜索的列名 like '%搜索的内容%'"
//本人较为讨厌正则表达式,所以这里没写那么多,喜欢正则表达式的可以去学习一下,菜鸟教程还不错
Pattern pattern = Pattern.compile("搜索的内容", Pattern.CASE_INSENSITIVE);
Document 搜索条件 = new Document("搜索的列名", pattern);
//类似于SQL的"where 搜索的列名 in ('搜索的内容1','搜索的内容2','搜索的内容3')"
List<String> list = new ArrayList<>();
list.add("搜索的内容1");
list.add("搜索的内容2");
list.add("搜索的内容3");
Document 搜索条件 = new Document("搜索的列名",new Document("$in",list));
//-------------------------较高级的搜索-----------------------------------
MongoIterable<Document> mongoIterable = mongoCollection.find(搜索条件);