Java MongoDB
package com.dys.mongo; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bson.Document;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.dys.model.Point;
import com.dys.model.Record;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; public class TestMongo { private static Logger logger = LogManager.getLogger(TestMongo.class); private MongoClient mongoClient = null; @Before
public void before() { /*
MongoCredential mongoCredential = MongoCredential.createCredential("dingyingsi", "test", "dingyingsi".toCharArray());
MongoClientOptions mongoClientOptions = MongoClientOptions.builder()
.connectionsPerHost(10)
.threadsAllowedToBlockForConnectionMultiplier(5)
.readPreference(ReadPreference.nearest())
.build(); this.mongoClient = new MongoClient(new ServerAddress("192.168.0.110", 27017), Arrays.asList(mongoCredential), mongoClientOptions);
*/ this.mongoClient = new MongoClient(new ServerAddress("192.168.0.110", 27017));
} @After
public void after() {
this.mongoClient.close();
} @Test
public void test01() throws Exception { MongoDatabase mdb = this.mongoClient.getDatabase("dingyingsi");
MongoCollection<Document> collection = mdb.getCollection("record"); FindIterable<Document> findIterable = collection.find(new BasicDBObject().append("_id","1")); for (Document doc : findIterable) {
ObjectMapper om = new ObjectMapper(); Record record = om.readValue(doc.toJson(), Record.class);
System.out.println(record.getPoints().size());
}
} @Test
public void test02() throws Exception {
MongoDatabase mdb = this.mongoClient.getDatabase("dingyingsi");
MongoCollection<Document> collection = mdb.getCollection("record"); Record record = null;
for (int j = 1; j <= 100000; j++) {
record = new Record();
record.set_id(j + "");
Point point = null;
List<Point> points = new ArrayList<Point>();
for (int i = 1; i <= 200000; i++) {
point = new Point();
point.setLongitude("120.12434");
point.setLatitude("134.56783");
points.add(point);
}
record.setPoints(points); ObjectMapper om = new ObjectMapper();
String recordJson = om.writeValueAsString(record); Document document = Document.parse(recordJson); collection.insertOne(document);
} }
}
Java MongoDB的更多相关文章
- Java + MongoDB Hello World Example--转载
原文地址:http://www.mkyong.com/mongodb/java-mongodb-hello-world-example/ A simple Java + MongoDB hello w ...
- 【MongoDB数据库】Java MongoDB CRUD Example
上一页告诉我们MongoDB 命令入门初探,本篇blog将基于上一篇blog所建立的数据库和表完毕一个简单的Java MongoDB CRUD Example.利用Java连接MongoDB数据库,并 ...
- Java MongoDB : Save image example
In this tutorial, we show you how to save an image file into MongoDB, via GridFS API. The GridFS API ...
- java MongoDB查询(二)复杂查询
前言 在上篇<java MongoDB查询(一)简单查询>中我们简单了解了下查询,但是仅仅有那些查询是不够用的,还需要复杂的查询,这篇就这点进行叙述. 1.数据结构 集合:firstCol ...
- Java mongodb api疑问之MongoCollection与DBCollection
在学习Java mongodb api时发现,可以调用不同的java mongodb api来连接数据库并进行相关操作. 方式一: 该方式使用mongoClient.getDB("xxx&q ...
- java mongoDB 二级数组嵌套查询
场景: 会员集合下有多个会员文档,会员文档下有多个订单文档,订单买了多个商品文档 member->orders>orderItems 要求: 通过会员id和商品id验证会员是否购买过该商品 ...
- Java MongoDB Driver 3.x - Quick Start
Maven Dependency: <dependency> <groupId>org.mongodb</groupId> <artifactId>mo ...
- Java MongoDB 资料集合
一.Mongodb介绍及对比 1.NoSQL介绍及MongoDB入门 http://renial.iteye.com/blog/684829 2.mongoDB 介绍(特点.优点.原理) http:/ ...
- java mongodb的MongoOptions生产级配置
autoConnectRetry仅仅意味着驱动程序会自动尝试重新连接到意外断开连接后在服务器(一个或多个).在生产环境中,您通常需要将此设置为true. connectionsPerHost是物理连接 ...
随机推荐
- POJ2385--Apple Catching(动态规划)
It is a little known fact that cows love apples. Farmer John has two apple trees (which are convenie ...
- MySQL1安装
---恢复内容开始--- (1)安装MySQL ①ZIP安装 ②MSI安装(Windows Installer) Ⅰ 先从官网下载 目前选择使用Typical安装↑ 目前不勾选配置选项↑ 找到配置向 ...
- 由VC2010与VC2017数据结构差异造成的程序错误
内容:VC2010和VC2017的标准库中,string(或wstring)的数据结构和操作有所不同,所以在将这两种数据作为参数在两个系统产生的函数中传递时会出现乱码(string和wstring在2 ...
- Android 四大组件之" ContentProvider "
前言 ContentProvider作为Android的四大组件之一,是属于需要掌握的基础知识,可能在我们的应用中,对于Activity和Service这两个组件用的很常见,了解的也很多,但是对Con ...
- 【BZOJ5290】 [Hnoi2018]道路
BZOJ5290 [Hnoi2018]道路 前言 这道题目我竟然没有在去年省选切? 我太菜了. Solution 对题面进行一个语文透彻解析,发现这是一个二叉树,乡村都是叶子节点,城市都有两个儿子.( ...
- 解决linux下source /etc/profile关闭终端失效问题
本来想配置环境变量的,看网上和博客上很多说改/etc/profile,然后source /etc/profile之后就可以永久保存使环境变量生效,但是终端一关闭,就环境变量就失效了,其他终端也用不了. ...
- TCP连接详解
一. 连接过程示意图 二. 建立TCP连接 2.1 三次握手 第一次握手:建立连接.客户端发送连接请求报文段,将SYN置为1,Sequence Number为x:然后,客户端进入SYN_SEND状态, ...
- SQLServer 在Visual Studio的2种连接方法
一.Sql Server 在Visual Studio的连接有两种方法: (1)本地计算机连接; string s = "Data Source=计算机名称;initial Catalog= ...
- iOS开发-实现相机app的方法[转载自官方]
This brief code example to illustrates how you can capture video and convert the frames you get to U ...
- 【learning】 扩展lucas定理
首先说下啥是lucas定理: $\binom n m \equiv \binom {n\%P} {m\%P} \times \binom{n/P}{m/P} \pmod P$ 借助这个定理,求$\bi ...