博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hello MongoDB -- Java编程
阅读量:6853 次
发布时间:2019-06-26

本文共 3368 字,大约阅读时间需要 11 分钟。

环境配置:在 Java 程序中如果要使用 MongoDB,你需要确保已经安装了 Java 环境及 MongoDB JDBC 驱动。

首先你必须下载mongo jar包,下载地址:, 请确保下载最新版本。

https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver/

Hello MongoDB -- Java编程DEMO:

package com.xctdemo.main;import java.util.ArrayList;import java.util.List;import org.bson.Document;import com.mongodb.MongoClient;import com.mongodb.client.FindIterable;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoCursor;import com.mongodb.client.MongoDatabase;import com.mongodb.client.model.Filters;public class HelloMongoDB {    public static void main(String[] args) {        System.out.println("---- Hello MongoDB ----");        try {            // 连接到 mongodb 服务            MongoClient mongoClient = new MongoClient("localhost", 27017);            // 连接到数据库            MongoDatabase mongoDatabase = mongoClient.getDatabase("world");            System.out.println("Connect to database successfully");            /*             * mongoDatabase.createCollection("city");             * System.out.println("集合city创建成功"); //             */            // 获取集合            MongoCollection
collection = mongoDatabase .getCollection("city"); System.out.println("集合 city 选择成功"); // 在集合中插入文档 /** * 1. 创建文档 org.bson.Document 参数为key-value的格式 2. 创建文档集合List
* 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List
) * 插入单个文档可以用 mongoCollection.insertOne(Document) * */ Document document = new Document("name", "beijing") .append("country", "china").append("provice", "BEIJING") .append("people-count", 1000); List
documents = new ArrayList
(); documents.add(document); collection.insertMany(documents); System.out.println("文档插入成功-Many"); Document mydocument = new Document("name", "washington") .append("country", "america") .append("provice", "WASHINGTON") .append("people-count", 1200); collection.insertOne(mydocument); System.out.println("文档插入成功-One"); // 检索所有文档 showDocuments(collection); // 删除符合条件的第一个文档 // collection.deleteOne(Filters.eq("name", "beijing")); // 删除所有符合条件的文档 collection.deleteMany(Filters.eq("name", "beijing")); System.out.println("---- deleted OK ----"); System.out.println("--------------------------"); // 更新文档 将文档中likes=100的文档修改为likes=200 collection.updateMany(Filters.eq("name", "washington"), new Document("$set", new Document("country", "JAPAN"))); // 检索所有文档 showDocuments(collection); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } System.out.println("---- over ----"); } private static void showDocuments(MongoCollection
collection) { /** * 1. 获取迭代器FindIterable
2. 获取游标MongoCursor
3. * 通过游标遍历检索出的文档集合 * */ FindIterable
findIterable = collection.find(); MongoCursor
mongoCursor = findIterable.iterator(); while (mongoCursor.hasNext()) { System.out.println(mongoCursor.next()); // .get("name") } }}

 

转载地址:http://etfyl.baihongyu.com/

你可能感兴趣的文章
Linux shell 之 提取文件名和目录名的一些方法
查看>>
test.
查看>>
树莓派3B连接WIFI
查看>>
springMVC启动过程源码解析(一)
查看>>
linux中生成考核用的FAT32文件系统结构样例(一)
查看>>
Docker 常用命令
查看>>
eclipse快捷键
查看>>
纯虚函数和虚函数的区别
查看>>
配置adb环境变量
查看>>
Jenkins安装
查看>>
命名空间 (一)
查看>>
Django报错
查看>>
性能测试培训:分析内训泄露的案例
查看>>
javax.persistence.TransactionRequiredException: Executing an update/delete query异常
查看>>
分享33个不容错过的免费社交图标集
查看>>
如果是你你会如何重新设计和定义维基百科(wikipedia)?
查看>>
ppp pap和chap 认证
查看>>
交换机的基本配置
查看>>
PHP结合Python的WEB开发技术
查看>>
华为:缺省路由:默认路由 default route
查看>>