import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document; import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer; import static com.mongodb.client.model.Accumulators.sum;
import static com.mongodb.client.model.Aggregates.group;
import static com.mongodb.client.model.Aggregates.match;
import static com.mongodb.client.model.Aggregates.project;
import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Filters.exists;
import static com.mongodb.client.model.Filters.gt;
import static com.mongodb.client.model.Filters.gte;
import static com.mongodb.client.model.Filters.lt;
import static com.mongodb.client.model.Filters.lte;
import static com.mongodb.client.model.Projections.excludeId;
import static com.mongodb.client.model.Sorts.descending;
import static com.mongodb.client.model.Updates.inc;
import static com.mongodb.client.model.Updates.set;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
public class QuickTour {
/**
* Run this main method to see the output of this quick example.
*
* @param args takes an optional single argument for the connection string
*/
public static void main(final String[] args) throws Exception{
MongoClient mongoClient; if (args.length == 0) {
// connect to the local database server
mongoClient = MongoClients.create();
} else {
mongoClient = MongoClients.create(args[0]);
} // get handle to "mydb" database
MongoDatabase database = mongoClient.getDatabase("mydb"); // get a handle to the "test" collection
MongoCollection<Document> collection = database.getCollection("test"); // drop all the data in it
collection.drop(); // make a document and insert it
Document doc = new Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("info", new Document("x", 203).append("y", 102)); collection.insertOne(doc); // get it (since it's the only one in there since we dropped the rest earlier on)
Document myDoc = collection.find().first();
System.out.println(myDoc.toJson()); // now, lets add lots of little documents to the collection so we can explore queries and cursors
List<Document> documents = new ArrayList<Document>();
for (int i = 0; i < 100; i++) {
documents.add(new Document("i", i));
}
collection.insertMany(documents);
System.out.println("total # of documents after inserting 100 small ones (should be 101) " + collection.countDocuments()); // find first
myDoc = collection.find().first();
System.out.println(myDoc.toJson()); // lets get all the documents in the collection and print them out
MongoCursor<Document> cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
} for (Document cur : collection.find()) {
System.out.println(cur.toJson());
} // now use a query to get 1 document out
myDoc = collection.find(eq("i", 71)).first();
System.out.println(myDoc.toJson()); // now use a range query to get a larger subset
cursor = collection.find(gt("i", 50)).iterator(); try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
} // range query with multiple constraints
cursor = collection.find(and(gt("i", 50), lte("i", 100))).iterator(); try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
} // Query Filters
myDoc = collection.find(eq("i", 71)).first();
System.out.println(myDoc.toJson()); // now use a range query to get a larger subset
Consumer<Document> printBlock = new Consumer<Document>() {
@Override
public void accept(final Document document) {
System.out.println(document.toJson());
}
};
collection.find(gt("i", 50)).forEach(printBlock); // filter where; 50 < i <= 100
collection.find(and(gt("i", 50), lte("i", 100))).forEach(printBlock); // Sorting
myDoc = collection.find(exists("i")).sort(descending("i")).first();
System.out.println(myDoc.toJson()); // Projection
myDoc = collection.find().projection(excludeId()).first();
System.out.println(myDoc.toJson()); // Aggregation
collection.aggregate(asList(
match(gt("i", 0)),
project(Document.parse("{ITimes10: {$multiply: ['$i', 10]}}")))
).forEach(printBlock); myDoc = collection.aggregate(singletonList(group(null, sum("total", "$i")))).first();
System.out.println(myDoc.toJson()); // Update One
collection.updateOne(eq("i", 10), set("i", 110)); // Update Many
UpdateResult updateResult = collection.updateMany(lt("i", 100), inc("i", 100));
System.out.println(updateResult.getModifiedCount()); // Delete One
collection.deleteOne(eq("i", 110)); // Delete Many
DeleteResult deleteResult = collection.deleteMany(gte("i", 100));
System.out.println(deleteResult.getDeletedCount()); // Create Index
collection.createIndex(new Document("i", 1)); // Clean up
database.drop(); // release resources
mongoClient.close();
}
}

mongodb QuickStart Demo的更多相关文章

  1. nodejs中使用mongodb quickstart

    nodejs中使用mongodb quickstart node 中使用mongodb的quick start.整理的官网crud简单例子. 在百度找了几篇帖子都有问题,所以直接看官网了. 连接Mon ...

  2. 用c#操作Mongodb(附demo)

    因为需要,写了一个基于泛型的helper,这样要使用起来方便一点. 为了大家也不重复造轮子,所以发出来希望能帮到谁. 复杂的查询最好用linq,这也是mongodb官方建议的. mongodb的C#配 ...

  3. NodeJs + mongodb模块demo

    代码比较通俗易懂,但是我还是在这个过程中浪费了不少时间,也算是看到了nodejs中异步的一个小坑.未来的坑还有很多,慢慢找坑填坑吧. 参考资料如下: 1.断言模块 : https://nodejs.o ...

  4. [MongoDB]对数组操作

    摘要 在实际开发中遇到更新某个document中的数组的值,这里做一下记录. 这里使用的驱动为 using MongoDB.Bson;using MongoDB.Driver; 相关文章 [Mongo ...

  5. MongoDB,HDFS, Spark to 电影推荐

    http://www.infoq.com/cn/news/2014/12/mongdb-spark-movie-recommend MovieWeb是一个电影相关的网站,它提供的功能包括搜索电影信息. ...

  6. debezium mongodb 集成测试

    debezium 是一个方便的cdc connector 可以帮助我们解决好多数据实时变更处理.数据分析.微服务的数据通信 从上次跑简单demo到现在,这个工具是有好多的变更,添加了好多方便的功能,支 ...

  7. MongoDB学习之(三)增删查改

    发现一篇Java操作MongoDb不错的文章,记录一下: https://www.cnblogs.com/sa-dan/p/6836055.html 基本功能. import java.util.Ar ...

  8. MongoDB 学习(一)安装配置和简单应用

    一.安装和部署 1.服务端安装 1.官网下载(官方网站 https://www.mongodb.org/downloads/#production),傻瓜式安装,注意修改安装路径. 安装完成后的目录结 ...

  9. Koa 操作 Mongodb 数据库

    node-mongodb-native的介绍 使用基于官方的 node-mongodb-native 驱动,封装一个更小.更快.更灵活的 DB 模块, 让我们用 nodejs 操作 Mongodb 数 ...

  10. mongodb与java的整合

    mongodb的相关命令我们这里不在赘述,因为其文档下写的非常清楚,也很容易懂.这里我们说一下其余java的整合,mongodb配置请查看官方文档 1.首先我们应该导入期相关依赖, org.mongo ...

随机推荐

  1. N个技巧,编写更高效 Dockerfile|云效工程师指北

    简介:云原生时代下软件的构建和部署离不开容器技术.提到容器,几乎大家下意识都会联想到 Docker .而 Docker 中有两个非常重要的概念,一个是Image(镜像),一个是Container(容器 ...

  2. Go 调用 Java 方案和性能优化分享

    ​简介: 一个基于 Golang 编写的日志收集和清洗的应用需要支持一些基于 JVM 的算子. ​ 作者 | 响风 来源 | 阿里技术公众号 一 背景 一个基于 Golang 编写的日志收集和清洗的应 ...

  3. k8s证书延长时间(二)

    1.查看证书有效时间 # 通过下面可看到ca证书有效期是10年,2022-2032 [root@master ~]# openssl x509 -in /etc/kubernetes/pki/ca.c ...

  4. .NET Aspire 预览版 6 发布

    .NET Aspire 预览版 6 引入了一系列重大更新,主要包括 API 的重大更改.安全性和可靠性的提升.新的资源和组件.应用程序主机的更新.测试支持.模板更新.组件更新.Azure 配置包的更新 ...

  5. 一款基于Vue3实现的漂亮且功能强大的在线海报设计器

    大家好,我是 Java陈序员. 我们在工作中经常需要设计各种各样的图片,海报.产品图.文章图片.视频/公众号等. 我们可以选择使用 PS 来设计图片,但是有时候想快速完成任务,有没有一款工具支持快速生 ...

  6. R2_ES中数据的存储测试

    基本概念:ES(一): 架构及原理 关系数据库 ⇒ 数据库 ⇒ 表 ⇒ 行 ⇒ 列(Columns) Elasticsearch ⇒ 索引(Index) ⇒ 类型(Type) ⇒ 文档(Documen ...

  7. C#使用MX Component实现三菱PLC软元件数据采集的完整步骤(仿真)

    前言 本文介绍了如何使用三菱提供的MX Component插件实现对三菱PLC软元件数据的读写,记录了使用计算机仿真,模拟PLC,直至完成测试的详细流程,并重点介绍了在这个过程中的易错点,供参考. 用 ...

  8. 后宫酒店HaremHotel游戏Python游戏作弊方法游戏金币增加方法

    额,这个游戏嘛,内容就不说了, 顺便送给大家下载地址:https://store.zhaimaojun.cn/GameDetail/280 这个游戏刚开始起步其实比较困难,因为没钱啊,动不动就要花钱, ...

  9. HTTPS 是如何进行安全传输的 ?

    概述 现代密码学对信息的处理主要离不开以下的三种形式: 摘要:主要用于数据校验,例如存储密码等,摘要是对信息进行单向的哈希,改变信息的原有形态,因为哈希函数的特点是易变性(即使微小的变化也会产生完全不 ...

  10. 鸿蒙HarmonyOS实战-ArkUI事件(单一手势)

    一.单一手势 应用程序的手势操作是指在移动设备上使用手指或手势进行与应用程序交互的方式.手势操作可以包括点击.滑动.双击.捏合等动作,用于实现不同的功能和操作. HarmonyOS中常见的手势操作及其 ...