java应用MongoDB简单操作
来源:互联网 发布:tcl网络电视如何看直播 编辑:程序博客网 时间:2024/05/21 07:56
/** * @Author: Administrator * @Date: 10:25 2017/10/16 * @Description: java链接mongodb数据库 */public class MongodbJdbc { public static void main(String[] args){ try { //连接mongodb服务,如果是远程连接替换"localhost"为服务器的IP的地址 //ServerAddress()两个参数分别为:1.服务器的地址 2.端口号 ServerAddress serverAddress = new ServerAddress("localhost", 27017); //为了通过认证连接数据库 ArrayList<ServerAddress> addresses = new ArrayList<ServerAddress>(); addresses.add(serverAddress); //MongoCredential.createScramSha1Credential()三个参数分别为 1.用户名 1.数据库名称 3.密码 MongoCredential credential = MongoCredential.createScramSha1Credential("MongoDB", "chen", "".toCharArray()); //为了通过认证连接数据库 ArrayList<MongoCredential> credentials = new ArrayList<MongoCredential>(); credentials.add(credential); //通过连接认证获取MongoDB连接,参数是上边创建的集合 MongoClient mongoClient1 = new MongoClient(addresses, credentials); //没有通过认证的链接 MongoClient mongoClient = new MongoClient("localhost", 27017); //连接到数据库 MongoDatabase database = mongoClient.getDatabase("chen"); System.out.println("连接数据库 MongoDB 成功"); //创建集合 我们可以使用com.mongodb.client.MongoDatabase类中的createCollection()来创建集合.参数是集合的名称 database.createCollection("mongodb_test"); System.out.printf("创建结合成功"); //获取集合 MongoCollection<Document> colle = database.getCollection("mongodb_test"); System.out.printf("colle=="+colle); //插入文档 /** * 1.创建文档 org.bson.Document 参数key-value的格式 * 2.创建文档集合List<Document> * 3.将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 插入单个文档可以用 mongoCollection.insertOne(Document) */ Document document = new Document("title", "MongoDB"); document.append("description", "database"); document.append("likes", 100); document.append("by", "Fly"); ArrayList<Document> list = new ArrayList<Document>(); list.add(document); colle.insertMany(list); //查看所有文档 /** * 1.获取迭代器FindIterable<Document> * 2.获取游标MongoCursor<Document> * 3.通过用表遍历检索出的文档集合 */ FindIterable<Document> findIterable = colle.find(); MongoCursor<Document> iterator = findIterable.iterator(); while (iterator.hasNext()){ // Document next = iterator.next(); System.out.println(iterator.next()); System.out.printf("666666"); } //更新文档 colle.updateMany(Filters.eq("likes",100), new Document("$set",new Document("likes", 200))); FindIterable<Document> findIterable1 = colle.find(); MongoCursor<Document> iterator1 = findIterable.iterator(); while(iterator1.hasNext()){ System.out.println(iterator.next()); } }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } }
阅读全文
0 0
- java应用MongoDB简单操作
- java简单操作mongodb
- java简单操作mongodb
- java简单操作mongodb实例
- MongoDB Java Driver 简单操作
- java中MongoDB的简单应用实例
- mongodb之java CRUD 简单操作
- mongodb之java CRUD 简单操作
- java操作mongoDB数据库的简单实例
- [mongodb]java操作mongodb
- [mongodb]java操作mongodb
- MongoDB简单应用
- MongoDB的简单操作
- MongoDB简单操作
- mongodb 简单操作
- mongodb简单操作
- MongoDB简单操作指令
- mongodb的简单操作
- java多线程与多核心测试
- 1085. Perfect Sequence (25)
- Ubuntu上安装wine进行apt-get update时遇到的问题
- vim 插件 (安装 and 用法)
- Android使用ShareSDK实现微信第三方登录中要获取unionid的方法
- java应用MongoDB简单操作
- Ones and Zeroes 问题及解法
- LightOJ 1094 Farthest Nodes in a Tree(dfs)
- getopt、getopt_long和getopt_long_only
- C# 钉钉api接口开发(三) 免登录及自动刷新AccessToken
- 求平均数的几种方法
- ElasticSearch基础
- BZOJ1827 [Usaco2010 Mar]gather 奶牛大集会
- Uiautomator的基础对象