[MongoDB]MongoDB与JAVA结合使用CRUD

汇总:

1. [MongoDB]安装MongoDB2. [MongoDB]Mongo基本使用:3. [MongoDB]MongoDB的优缺点及与关系型数据库的比较4. [MongoDB]MongoDB与JAVA结合使用CRUD

使用起来也相当简单,由于MongoDB是类文件的数据库,所以其操作起来非常方便

首先下载相应的jar包,这里我直接使用Maven自动获取,以下为POM文件中的配置:

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.8.2</version>

</dependency>

<dependency>

<groupId>org.mongodb</groupId>

<artifactId>mongo-java-driver</artifactId>

<version>2.7.2</version>

</dependency>

一个MongoDb的jar包和Junit的jar包,即可

后面即可开始编写代码,直接在某个java类的main方法中调用即可:

public static void main(String[] args) throws Exception{

// 创建一个Mongo实例,默认使用本地"127.0.0.1", 27017

Mongo mongo1j = new Mongo();

// 使用IP+端口号创建

Mongo mongo = new Mongo("127.0.0.1", 27017);

// 得到所有数据库的名称

for(String name : mongo.getDatabaseNames()) {

System.out.println("dbName: " + name);

}

// 取得某个数据库,用于后续对数据库的操作

DB db = mongo.getDB("student");

for(String name : db.getCollectionNames()) {

System.out.println("collectionName: " + name);

}

// 取得对应的集合

DBCollection students = db.getCollection("student");

// 查询所有集合中的数据

DBCursor cur = students.find();

// 遍历集合中的所有内容

while(cur.hasNext()) {

System.out.println(cur.next());

}

System.out.println("cur.count(): " + cur.count());

System.out.println("cur.getCursorId(): " + cur.getCursorId());

System.out.println("JSON.serialize(): " + JSON.serialize(cur));

// DELETE操作

students.remove(new BasicDBObject("name", "yangjx"));

System.out.println("cur.count(): " + cur.count());

// ADD操作

DBObject student = new BasicDBObject();

student.put("name", "yangjx");

student.put("age", 55);

students.save(student);

System.out.println("cur.count(): " + cur.count());

// UPDATE操作

DBObject updateObj = new BasicDBObject();

updateObj.put("age", 30);

updateObj.put("name", "zhangGY");

updateObj.put("extra", "markHere");

students.update(new BasicDBObject("age", 30), updateObj);

System.out.println(JSON.serialize(students.find(new BasicDBObject("name", "zhangGY"))));

}

上述代码执行完成之后会打印出如下:

dbName: local

dbName: student

collectionName: colName

collectionName: student

collectionName: system.indexes

{ "_id" : { "$oid" : "555ae83a09668180fadccdeb"} , "age" : 30}

{ "_id" : { "$oid" : "555ae84009668180fadccdec"} , "name" : "zhangYC" , "age" : 29.0 , "sex" : true}

{ "_id" : { "$oid" : "555ae84709668180fadccded"} , "name" : "zhangYYQ" , "age" : 30.0 , "sex" : true}

{ "_id" : { "$oid" : "555aebdb09b332c5a188b620"} , "passwd" : "1234"}

{ "_id" : { "$oid" : "555d80d7b7a5042665b195ae"} , "name" : "yangjx" , "age" : 55}

cur.count(): 5

cur.getCursorId(): 0

JSON.serialize(): [ { "_id" : { "$oid" : "555ae83a09668180fadccdeb"} , "age" : 30} , { "_id" : { "$oid" : "555ae84009668180fadccdec"} , "name" : "zhangYC" , "age" : 29.0 , "sex" : true} , { "_id" : { "$oid" : "555ae84709668180fadccded"} , "name" : "zhangYYQ" , "age" : 30.0 , "sex" : true} , { "_id" : { "$oid" : "555aebdb09b332c5a188b620"} , "passwd" : "1234"} , { "_id" : { "$oid" : "555d80d7b7a5042665b195ae"} , "name" : "yangjx" , "age" : 55}]

cur.count(): 4

cur.count(): 5

[ { "_id" : { "$oid" : "555ae83a09668180fadccdeb"} , "age" : 30 , "name" : "zhangGY" , "extra" : "markHere"}]

参考:

http://www.cnblogs.com/hoojo/archive/2011/06/02/2068665.html

时间: 2024-08-08 23:42:28

[MongoDB]MongoDB与JAVA结合使用CRUD的相关文章

[MongoDB]MongoDB的优缺点及与关系型数据库的比较

汇总: 1. [MongoDB]安装MongoDB2. [MongoDB]Mongo基本使用:3. [MongoDB]MongoDB的优缺点及与关系型数据库的比较4. [MongoDB]MongoDB与JAVA结合使用CRUD 参考:http://www.cnblogs.com/hoojo/archive/2011/06/01/2066119.html 介绍:MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. 特点:高性能.易部

【MongoDB数据库】Java MongoDB CRUD Example

上一篇我们讲了MongoDB 的命令入门初探,本篇blog将基于上一篇blog所建立的数据库和表完毕一个简单的Java MongoDB CRUD Example,利用Java连接MongoDB数据库.并实现创建数据库.获取表.遍历表中的对象.对表中对象进行CRUD操作等例程. 1.下载MongoDB Java 支持驱动包 [gitHub下载地址]https://github.com/mongodb/mongo-java-driver/downloads 2.建立Javaproject,并导入ja

Java操作Mongodb 保存/读取java对象到/从mongodb

Mongodb官方提供的Java驱动,保存与读取,需要的者是DBObject对象,这是一个接口,实现put,get等方法,跟map类似,如果我们要直接把普通的java对象保存到mongodb,就需要先转换成DBObject对象,或者直接实现DBObject接口,操作起来相当复杂.还好,monodb驱动带了把json转换成DBObject对象的功能,加上Google的Gson,就可以实现把普通的对象保存到mongodb中.如果要从mogodb中读出对象,反过来操作即可,但有一点需要注意的是,mon

MongoDB - MongoDB CRUD Operations

CRUD operations create, read, update, and delete documents. Create Operations Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection. MongoDB provides th

MongoDB分页的Java实现和分页需求的思考

前言 传统关系数据库中都提供了基于row number的分页功能,切换MongoDB后,想要实现分页,则需要修改一下思路. 传统分页思路 假设一页大小为10条.则 //page 1 1-10 //page 2 11-20 //page 3 21-30 ... //page n 10*(n-1) +1 - 10*n MongoDB提供了skip()和limit()方法. skip: 跳过指定数量的数据. 可以用来跳过当前页之前的数据,即跳过pageSize*(n-1). limit: 指定从Mon

MongoDB - MongoDB CRUD Operations, Update Documents

Update Methods MongoDB provides the following methods for updating documents in a collection: Method Description  db.collection.updateOne() Updates at most a single document that match a specified filter even though multiple documents may match the s

MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell

The db.collection.find() method returns a cursor. To access the documents, you need to iterate the cursor. However, in the mongo shell, if the returned cursor is not assigned to a variable using the varkeyword, then the cursor is automatically iterat

MongoDB - MongoDB CRUD Operations, Query Documents

Query Method MongoDB provides the db.collection.find() method to read documents from a collection. The db.collection.find() method returns a cursor to the matching documents. db.collection.find( <query filter>, <projection> ) For the db.collec

MongoDB - MongoDB CRUD Operations, Query Documents, Project Fields to Return from Query

By default, queries in MongoDB return all fields in matching documents. To limit the amount of data that MongoDB sends to applications, you can include a projection document in the query operation. Projection Document The projection document limits t