java操作mongoDB

来源:互联网 发布:js new array 长度 编辑:程序博客网 时间:2024/05/14 02:34
最近刚刚研究了mongodb,觉得很有意思,自己写了个小demo,有兴趣的可以看一下,欢迎各位大神的点评!

开发环境
真实OS:Win7
VM Workstation版本:11
Linux版本:CentOS 6.7
Xshell版本:5
IDE版本:MyEclipse8.6

MongoDB4Linux版本:3.2
JDK版本:1.5+

步骤

1 创建一个JAVA Project
2 引入MongoDB的jar
3 创建一个测试类
4 依次运行增删改查方法

步骤1-3

步骤1-3

步骤4

package demo;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;

/**
* 类描述:使用JavaApi操作mongodb
* 作者:xxx
* 日期:2016-6-21下午05:08:06
*
*/
public class MongoDBTest {

/** * @param args */public static void main(String[] args) {    // 获取mongodb实例     MongoClient client = getSingleMongo();    // 获取实例中的指定数据库    MongoDatabase database = client.getDatabase("admin");    // 获取指定数据库中的集合(相关于关系型数据库中的数据表)    MongoCollection<Document> collection = database.getCollection("t_user");    // 查询功能演示    getAll(collection);    // 插入功能演示    //insert(collection);    // 更新功能演示    //update(collection);    // 删除功能演示    //delete(collection);}/** * 方法描述:取得mongoDB实例(单个数据库服务器) * @return */private static MongoClient getSingleMongo(){    // 获取mongodb实例     MongoClient client = new MongoClient("192.168.137.3", 27017);    return client;}/** * 方法描述:取得mongoDB实例(集群方式) * @return */private static MongoClient getColonyMongo(){    // 创建多个MongoDB服务地址    ServerAddress server_1 = new ServerAddress("192.168.137.3", 27017);    ServerAddress server_2 = new ServerAddress("192.168.137.4", 27017);    ServerAddress server_3 = new ServerAddress("192.168.137.5", 27017);    List<ServerAddress> serverAddressList = new ArrayList<ServerAddress>();    serverAddressList.add(server_1);    serverAddressList.add(server_2);    serverAddressList.add(server_3);    // 创建mongodb集群实例     MongoClient client = new MongoClient(serverAddressList);    return client;}private static void getAll(MongoCollection<Document> collection){    System.out.println("---------------查询开始---------------");    // 取得集合中的数据列表    FindIterable<Document> find = collection.find();    // 循环输出集合中的数据    for (Document document : find) {        System.out.println(document);    }    System.out.println("---------------查询结束---------------");}private static void insert(MongoCollection<Document> collection){    System.out.println("---------------插入开始---------------");    // 创建需要插入的数据    Document document = new Document();    document.put("user_id", "100");    document.put("user_name", "张三");    // 执行插入操作    collection.insertOne(document);    System.out.println("---------------插入结束---------------");}private static void update(MongoCollection<Document> collection){    System.out.println("---------------更新开始---------------");    // 创建更新条件    Document condition = new Document();    condition.put("user_id", "100");    // 创建更新内容    Document updateContent = new Document();    updateContent.put("user_name", "李四");    Document updateSet = new Document();    updateSet.put("$set", updateContent);    // 执行更新操作    collection.updateOne(condition, updateSet);    System.out.println("---------------更新结束---------------");}private static void delete(MongoCollection<Document> collection){    System.out.println("---------------删除开始---------------");    // 创建删除条件    Document condition = new Document();    condition.put("user_id", "100");    // 执行删除操作    DeleteResult deleteOne = collection.deleteOne(condition);    // 根据返回删除数据的数量判断操作是否成功    if(deleteOne.getDeletedCount() == 1){        System.out.println("Delete Successful!");    }    else{        System.out.println("Delete Failure!");    }    System.out.println("---------------删除结束---------------");}

}

0 0
原创粉丝点击