1. mongodb-driver是mongo官方推出的java连接mongoDB的驱动包,相当于JDBC驱动。

(1)通过maven仓库导入:https://mvnrepository.com/artifact/org.mongodb/mongodb-driver

(2)官网中下载相应的java的驱动:http://docs.mongodb.org/ecosystem/drivers/java/

(3)不同的驱动使用的jar也不相同参考:http://mongodb.github.io/mongo-java-driver/

例如:

  <dependencies>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-sync</artifactId>
        <version>3.11.2</version>
    </dependency>
  </dependencies>

 2. 创建方法类

2.1 查询全部,遍历打印

package mongodb.test;

import org.bson.Document;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class Mongodb {

    /**
     * 查询打印全部集合
     */
    public static void mongoQueryAll() {
        //1.创建链接
        MongoClient client = new MongoClient("localhost");
        //2.打开数据库test
        MongoDatabase db = client.getDatabase("test");
        //3.获取集合
        MongoCollection<Document> collection = db.getCollection("stu");
        //4.查询获取文档集合
        FindIterable<Document> documents = collection.find();
        //5.循环遍历
        for (Document document : documents) {
            System.out.println(document);
        }
        //6.关闭连接
        client.close();
    }

    public static void main(String[] args) {
        mongoQueryAll();
    }
//打印输出stu全部数据
Document{{_id=5d7374e836a89c5a3d18b87a, name=xiaohua}}
Document{{_id=2.0, sn=002, name=xiaogang}}
Document{{_id=3.0, sn=003, name=zhangfei, job=前锋战将}}
Document{{_id=5d73782736a89c5a3d18b87b, sn=004, name=xiaobingbing}}
Document{{_id=5d7396b44ec120618b2dd0cb, name=Document{{surname=李, name=世名}}, job=[皇帝, 大人物, 大丈夫, 功成名就]}}

2.2 条件查询

    /**
     * 条件查询:如查询id为xxxx的学生所有信息
     */
    public static void mongoConditionQuery() {
        //1.创建链接
        MongoClient client = new MongoClient("localhost");
        //2.打开数据库test
        MongoDatabase db = client.getDatabase("test");
        //3.获取集合
        MongoCollection<Document> collection = db.getCollection("stu");
        //4.构建查询条件,按照name来查询
        BasicDBObject stu = new BasicDBObject("name","zhangfei");
        //5.通过id查询记录,获取文档集合
        FindIterable<Document> documents = collection.find(stu);
        //5.打印信息
        for (Document document : documents) {
            System.out.println("name:"+document.getString("name"));
            System.out.println("sn:"+document.getString("sn"));
            System.out.println("job:"+document.getString("job"));
        }
        //6.关闭连接
        client.close();
    }

    public static void main(String[] args) {
        mongoConditionQuery();
    }
//执行输出
name:zhangfei
sn:003
job:前锋战将

注意:当需要查询条件+判断的时候这样写,例如查询学号sn>003的学员

//查询sum大于3的学员
BasicDBObject stu = new BasicDBObject("sum",new BasicDBObject("$gt",003));

2.3 插入语句

    /**
     * 插入语句
     */
    public static void mongoInsert() {
        //1.创建链接
        MongoClient client = new MongoClient("localhost");
        //2.打开数据库test
        MongoDatabase db = client.getDatabase("test");
        //3.获取集合
        MongoCollection<Document> collection = db.getCollection("stu");
        //4.准备插入数据
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("sn","005");
        map.put("name","xiaoA");
        map.put("job","A工作");
        map.put("sum",6);
        //5.将map转换成document
        Document document = new Document(map);
        collection.insertOne(document);
        //6.关闭连接
        client.close();
    }
    //测试执行
    public static void main(String[] args) {
        mongoInsert();
    }

批量插入,仅供参考:

//当需要插入多条文档的时候,循环进行单条插入当然是可以,但是效率不高,MongoDB提供了批量插入的方法
List<DBObject> objs = new ArrayList<DBObject>();
objs.add(new BasicDBObject("name","user29").append("age", 30).append("sex", 1));
objs.add(new BasicDBObject("name","user30").append("age", 30).append("sex", 1));
collection.insert(objs);
//这样就批量进行了插入。批量插入通过一次请求将数据传递给数据库,然后由数据库进行插入,比循环单条插入节省了每次进行请求的资源。

学习后总结,不足之处请指出,后续修改!

01-31 23:28