当前位置:Gxlcms > 数据库问题 > MongoDB

MongoDB

时间:2021-07-01 10:21:17 帮助过:5人阅读

连接到 mongodb 服务 MongoClient mongoClient = new MongoClient( "127.0.0.1" , 27017 ); // 连接到数据库 MongoDatabase mongoDatabase = mongoClient.getDatabase("articledb"); System.out.println("Connect to database successfully"); MongoCollection<Document> collection = mongoDatabase.getCollection("article"); MongoCollection<Document> collection_txt = mongoDatabase.getCollection("article_txt");

2.2 添加查询条件

//模糊查询条件
Pattern pattern = Pattern.compile(".*atimo.cn.*", Pattern.CASE_INSENSITIVE);
query.put("url", pattern);//对应表字段名
MongoCursor<Document> cursor = collection.find(query).skip(0).limit(20).iterator();//添加查询条件分页条件

2.3 遍历查询结果

while (cursor.hasNext()) {
Article act = new Article();
//查询出的结果转换成jsonObject,然后进行封装或者直接返回给前端处理。我这是封装成对象了
JSONObject jsonObject = JSONObject.parseObject( cursor.next().toJson().toString());
System.out.println(jsonObject.getString("url"));
}

 

MongoDB

标签:添加   查询   nbsp   ESS   返回   cursor   alt   gets   man   

人气教程排行