码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB(二)

时间:2017-10-20 11:58:46      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:tcl   family   client   message   for   cti   getc   .class   model   

传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,

         MongoDB是由(database)、集合(collection)、文档对象(document)三个层次组成。

         MongoDB对于关系型数据库里的表,但是集合中没有列、行和关系概念,这体现了模式自由的特点。

Java 代码连接查询:

技术分享

try {
MongoClientOptions.Builder build = new MongoClientOptions.Builder();
//与数据最大连接数50
build.connectionsPerHost(50);
//如果当前所有的connection都在使用中,则每个connection上可以有50个线程排队等待
build.threadsAllowedToBlockForConnectionMultiplier(50);
build.connectTimeout(1*60*1000);
build.maxWaitTime(2*60*1000);
MongoClientOptions options = build.build();
MongoClient mongoClient = new MongoClient("192.168.8.22", options);//连接地址 or localhost
MongoDatabase mgdb = mongoClient.getDatabase("spt");
System.out.println("Connect to database successfully!");
System.out.println("MongoDatabase inof is : "+mgdb.getName());
MongoCollection<Document> collection = mgdb.getCollection("spt");
FindIterable<Document> f = collection.find(Filters.eq("id", "29e01a0f23e9461ca871e10661292895"));
Document d = f.first();
SugarTestCommentBean sugarComment = JsonUtils.fromJson(d.toJson(), SugarTestCommentBean.class);
List<CommentBean> commentList = sugarComment.getComments();
PageFinder<CommentBean> pageFinder = new PageFinder<CommentBean>();
pageFinder.setData(commentList);
modelMap.put("pageFinder", pageFinder);
mongoClient.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}

MongoDB(二)

标签:tcl   family   client   message   for   cti   getc   .class   model   

原文地址:http://www.cnblogs.com/shenjiangwei/p/7698409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!