JAVA操作mongoDB(一) helloworld

Java MongoDB 实例HelloWorld NULL 博文链接:https://flycun2.iteye.com/blog/1906539 立即下载

一、安装配置好mongoDB

http://www.mongodb.org/downloads

二、下载mongoDB的JAVA驱动

优快云下载:http://download.youkuaiyun.com/detail/thundercat/4072904

官网下载: https://github.com/mongodb/mongo-java-driver/downloads

三、在JAVA中连接DB,插入数据,查询数据

package com.cs.mongodb.test;

import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
 * Java + MongoDB Hello world Example
 * 
 */
public class HelloWorld {
	public static void main(String[] args) {
		try {
			// 实例化Mongo对象,连接27017端口
			Mongo mongo = new Mongo("localhost", 27017);
			// 连接名为mydb的数据库,假如数据库不存在的话,mongodb会自动建立
			DB db = mongo.getDB("mydb");
			// 从Mongodb中获得名为myCollection的数据集合,如果该数据集合不存在,Mongodb会为其新建立
			DBCollection collection = db.getCollection("myCollection");
			// 使用BasicDBObject对象创建一个mongodb的document,并给予赋值。
			BasicDBObject document = new BasicDBObject();
			document.put("id", 1001);
			document.put("msg", "hello world mongoDB in Java");
			// 将新建立的document保存到collection中去
			collection.insert(document);
			// 创建要查询的document
			BasicDBObject searchQuery = new BasicDBObject();
			searchQuery.put("id", 1001);
			// 使用collection的find方法查找document
			DBCursor cursor = collection.find(searchQuery);
			// 循环输出结果
			while (cursor.hasNext()) {
				System.out.println(cursor.next());
			}
			System.out.println("Done");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (MongoException e) {
			e.printStackTrace();
		}
	}
}

四、结果为

{ "_id" : { "$oid" : "4f3fb90968d7350bb10d36ac"} , "id" : 1001 , "msg" : "hello world mongoDB in Java"}
Done


Java + MongoDB 实现 hello world 例子(译) NULL 博文链接:https://ysj5125094.iteye.com/blog/2205022 立即下载

相关推荐

Godot4 TileMap分层绘制全攻略:解决角色、树木遮挡混乱,打造正确视觉层级

本文详细介绍了Godot4 TileMap分层绘制技术,通过图层管理和Y-Sort功能解决2D游戏中角色与物体的遮挡问题。从基础图层设置到高级优化策略,帮助开发者打造符合自然视觉规律的场景层次,提升游戏沉浸感。特别针对TileMap和TileSet的使用提供了实用技巧和代码示例。

weixin_28717411的博客 512

MongoDB学习笔记(1)HelloWorld(增删改查)

package com.liyb.test.mongodb.testmongodb; import static com.mongodb.client.model.Filters.and; import static com.mongodb.client.model.Filters.eq; import static com.mongodb.client.model.Filters.gt; im

leo 882

别再混淆了!5分钟搞懂机器人导纳控制与阻抗控制的本质区别(附Simulink对比仿真)

本文深入解析机器人柔顺控制中的导纳控制与阻抗控制的本质区别,通过信号流方向、架构对比和应用场景三个关键视角进行详细阐述。文章还提供Simulink对比仿真案例,帮助读者快速理解两种控制策略的差异及其适用场景,特别适合机器人控制领域的研究者和工程师参考。

weixin_42543046的博客 605

MongoDB:创建用户

db.createUser( { user: "zhibo", pwd: "123456", roles: [ { role: "readWrite", db: "zhibo" } ] } Read:允许用户读取指定数据库 readWrite:允许用户读写指定数据库 dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或

Methun 984

Altium Designer铺铜陷阱:为什么你的死铜总是清除不干净?

本文深入探讨Altium Designer中铺铜操作导致死铜无法清除的常见问题,分析死铜形成的五大诱因,并提供高级参数优化和网络连接性验证技巧。通过调整边界计算精度和泪滴算法设置,有效解决死铜残留问题,提升PCB设计质量与制造良率。

weixin_29169505的博客 359

SwarmClaw:自托管多智能体AI运行时部署与实战指南

多智能体系统是人工智能领域的重要发展方向,它通过多个具备自主决策和协作能力的智能体共同完成任务,其核心原理在于分布式问题求解与协同规划。这种架构在提升任务处理效率、复杂问题分解和鲁棒性方面具有显著技术价值,广泛应用于自动化工作流、虚拟团队协作、智能客服和个性化助手等场景。本文聚焦于SwarmClaw这一自托管的多智能体AI运行时,它原生支持Model Context Protocol,并内置了超过23种AI提供商,为开发者提供了一个开箱即用且高度可定制的控制平面,便于构建和管理复杂的多智能体协作网络。

weixin_27207591的博客 401

MongoDB Helloworld For Window

4. 用java code 连接mongo. 做增删改查操作。在User collection上增加记录。在MongoDB ui 上可以查看数据。1. 下载MongoDB。2. 安装MongoDB。

Harry的博客 744

Ubuntu22.04下ROS2与PX4仿真环境搭建全流程解析

本文详细解析了在Ubuntu22.04系统下搭建ROS2与PX4仿真环境的全流程,包括系统准备、ROS2 Humble安装、PX4飞控环境部署、ROS2与PX4通信桥梁搭建等关键步骤。针对常见问题提供了解决方案,并分享了性能优化和开发调试技巧,帮助开发者快速构建无人机仿真开发环境。

weixin_29248653的博客 607

java与mongodb_Java和MongoDB之Hello World

1.新建Project新建Java Project,并把mongo-java-driver驱动加入到项目bulid path中,如果你使用的是maven增加依赖。org.mongodbmongo-java-driver2.10.12.连接上MongoDB//>2.10版本MongoClient mongo = new MongoClient( "localhost" , 27017);//老...

weixin_33609486的博客 173

通过miniconda conda安装jupyer的时候报错: InvalidArchiveError(“Error with archive

摘要:本文针对Windows系统使用Conda时出现的InvalidArchiveError错误(文件路径过长或压缩包损坏),提供了解决方案。建议以管理员身份运行PowerShell并执行命令启用长路径支持(LongPathsEnabled),然后重启电脑或终端。作者通过该方法成功解决了jupyter安装问题。附有错误截图和解决后的成功截图。

猿小白的博客 788

定位精度再突破!Inner-MPDIoU让YOLOv11边界框回归更精准

Inner-MPDIoU 凭借辅助边框与最小点距离的双机制创新,彻底解决了传统 IoU 损失在边界框回归上的精度瓶颈。本教程从原理到代码实现,再到工业场景适配,完整呈现了其在 YOLOv11 中的集成路径。只需简单修改两处核心脚本,你的目标检测模型就能在边界框定位精度上实现跨越式提升——无论是学术研究中的指标突破,还是工业场景下的精细检测需求,Inner-MPDIoU 都能成为你攻克边界框回归难题的利器。现在就动手尝试,让你的检测模型在定位精度上再上一个台阶吧。

FJN110的博客 160

<转>Mongodb快速入门之使用Java操作Mongodb

Mongodb快速入门之使用Java操作Mongodb 2011年06月20日00:00 it168网站原创 作者:廖煜嵘/译 编辑:董建伟   【IT168 专稿】在上一篇文章中,我们学习了Mongodb的安装和初步使用,在本文中,将学习如何使用Java去编程实现对Mongodb的操作。   HelloWorld程序   学习任何程序的第一步,都是编写HelloWor...

tianyestar 659

数控系统(CNC)系列:Fanuc 31i-B_(16).CNC系统的网络连接与数据传输

硬件连接:包括网络接口的类型、连接方式等。网络协议:常用的网络协议包括Ethernet、Modbus TCP、Profinet等。网络配置:如何在CNC系统中配置网络参数,使其能够正确接入网络。在网络化的现代工业生产环境中,CNC系统的网络连接和数据传输能力变得越来越重要。通过以太网、Modbus TCP和Profinet等网络协议,可以实现CNC系统与上位机、其他CNC系统以及各种工业设备之间的高效通信。

2401_87715305的博客 2436

mongodb java in_Mongodb快速入门之使用Java操作Mongodb

【IT168专稿】在上一篇文章中,我们学习了Mongodb的安装和初步使用,在本文中,将学习如何使用Java去编程实现对Mongodb的操作。HelloWorld程序学习任何程序的第一步,都是编写HelloWorld程序,我们也不例外,看下如何通过Java编写一个HelloWorld的程序。首先,要通过Java操作Mongodb,必须先下载Mongodb的Java驱动程序,可以在这里下载。新建立...

weixin_32534073的博客 316

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

java 操作mongodb_[转载]使用Java操作Mongodb

HelloWorld程序学习任何程序的第一步,都是编写HelloWorld程序,我们也不例外,看下如何通过Java编写一个HelloWorld的程序。首先,要通过Java操作Mongodb,必须先下载Mongodb的Java驱动程序,可以在这里下载。新建立一个Java工程,将下载的驱动程序放在库文件路径下,程序代码如下:1 packagecom.mkyong.core;2 importjava.n...

weixin_39959236的博客 300

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

java mongodb开发_Mongodb快速入门之使用Java操作Mongodb

【IT168专稿】在上一篇文章中,我们学习了Mongodb的安装和初步使用,在本文中,将学习如何使用Java去编程实现对Mongodb的操作。HelloWorld程序学习任何程序的第一步,都是编写HelloWorld程序,我们也不例外,看下如何通过Java编写一个HelloWorld的程序。首先,要通过Java操作Mongodb,必须先下载Mongodb的Java驱动程序,可以在这里下载。新建立...

weixin_39961522的博客 200

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Mongodb快速入门之使用Java操作Mongodb

转自: http://tech.it168.com/a2011/0617/1206/000001206231_all.shtml   【IT168 专稿】在上一篇文章中,我们学习了Mongodb的安装和初步使用,在本文中,将学习如何使用Java去编程实现对Mongodb的操作。   HelloWorld程序   学习任何程序的第一步,都是编写HelloWorld程序,我们也不

百川 1072

[转载]使用Java操作Mongodb

HelloWorld程序   学习任何程序的第一步,都是编写HelloWorld程序,我们也不例外,看下如何通过Java编写一个HelloWorld的程序。   首先,要通过Java操作Mongodb,必须先下载Mongodb的Java驱动程序,可以在这里下载。   新建立一个Java工程,将下载的驱动程序放在库文件路径下,程序代码如下: 1 package com.m...

ab316600的博客 147

java操作mongodb实例

package mongodb; import java.net.UnknownHostException; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.m...

ppxieppp的博客 118

Spring整合MongoDB(一)----快速指南

虽然MongoClient是MongoDB驱动程序API的入口点,但连接到特定的MongoDB数据库实例需要额外的信息,例如数据库名称和可选的用户名和密码。与直接实例化MongoClient实例相比,FactoryBean还有一个额外的优势,即还为容器提供了ExceptionTranslator实现,该实现将MongoDB异常转换为Spring的可移植DataAccessException层次结构中的异常,用于@Repository注解的数据访问类。首先,需要设置一个正在运行的MongoDB服务器。

爱看技术博客 1978

java操作mongodb

代码1: package com.ooooo.mongo; import java.net.UnknownHostException; import java.util.Set; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import or...

weixin_33788244的博客 86

mongodb的helloworld

1.数据默认保存在mkdir -p /data/db 2.下载curl -O http://downloads.mongodb.org/linux/mongodb-linux-i686-latest.tgz 目前用的是mongodb-linux-i686-2011-07-08, 3./opt/mongodb/bin/mongod run & 或/mongod --fork --dbpath...

走在左边 193

使用Java操作Mongodb

HelloWorld程序   学习任何程序的第一步,都是编写HelloWorld程序,我们也不例外,看下如何通过Java编写一个HelloWorld的程序。   首先,要通过Java操作Mongodb,必须先下载Mongodb的Java驱动程序,可以在这里下载。   新建立一个Java工程,将下载的驱动程序放在库文件路径下,程序代码如下: package com.mkyon

ywb201314的专栏 524
上一篇: Mongodb快速入门之使用Java操作Mongodb
下一篇: 正确关闭MongoDB
thundercat
博客等级 码龄18年 18粉丝 10原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值