一、依赖包的下载

mongodb java 驱动依赖包下载
JSON 解析类库 以阿里为例 下载

或者:maven项目的pom.xml配置如下:

 <dependencies>
    <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongo-java-driver</artifactId>
      <version>3.2.2</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>
  </dependencies>
二、实现代码如下
package org.example;

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

import com.alibaba.fastjson.JSONObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class MongoDBJDBC {
    public static void main(String[] args){
        try {
            //连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
            //ServerAddress()两个参数分别为 服务器地址 和 端口
            ServerAddress serverAddress = new ServerAddress("localhost",27017);
            List<ServerAddress> addrs = new ArrayList<ServerAddress>();
            addrs.add(serverAddress);
            //MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
            MongoCredential credential = MongoCredential.createScramSha1Credential("用户名", "数据库名", "密码".toCharArray());
            List<MongoCredential> credentials = new ArrayList<MongoCredential>();
            credentials.add(credential);
            //通过连接认证获取MongoDB连接
            MongoClient mongoClient = new MongoClient(addrs,credentials);
            //连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("数据库名");
            System.out.println("Connect to database successfully");
            //选择集合
            MongoCollection<Document> collection = mongoDatabase.getCollection("集合名");
            System.out.println("集合 test 选择成功");
            //条件查询
            FindIterable<Document> findIterable = collection.find(new Document("字段名key","字段值value"));
            MongoCursor<Document> mongoCursor = findIterable.iterator();
            Document demo=new Document();
            if(mongoCursor.hasNext()){
                demo = mongoCursor.next();
                JSONObject jsonObject=JSONObject.parseObject(demo.toJson());
                //获取集合中value为Object对象"test"下的com的值
                System.out.println(jsonObject.getJSONObject("test").getString("com"));
                //获取集合中value为Array列表“testArray”下,索引为1的值
                System.out.println(jsonObject.getJSONArray("testArray").getString(1).getClass());
                //循环遍历获取集合中value为Array列表“testArray”下的值
                for (Object json:jsonObject.getJSONArray("testArray")){
                    System.out.println(json);
                }
                //获取集合中value为String类型的值
                System.out.println(jsonObject.getString("pharmacological_toxicology"));
            }
        } catch (Exception e) {
            System.err.println( "出现异常"+ e.getClass().getName() + ": " + e.getMessage() );
        }
    }
}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐