MongoDB 的小例子

xml version="1.0" encoding="UTF-8"?>
xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    com.test.mongo
    com.test.mongo
    1.0-SNAPSHOT
    
        UTF-8
    
    
        
            junit
            junit
            3.8.1
            test
        
        
            org.mongodb
            mongo-java-driver
            3.2.2
        
        
            junit
            junit
            4.12
            test
        
    

--------------------

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;

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

/**
 * Created by Administrator on 2016/9/15.
 */
public class MongoTest {

    // 连接到 mongodb 服务
    MongoClient mongoClient = null;//
    // 连接到数据库
    MongoDatabase mongoDatabase = null;//

    MongoCollection collection = null;

    public void init() {
        mongoClient = new MongoClient("localhost", 27017);
        mongoDatabase = mongoClient.getDatabase("mycol");

        collection = mongoDatabase.getCollection("test");
        System.out.println("集合 test 选择成功");

    }

    public void insertMe() {


        //插入文档
        /**
         * 1. 创建文档 org.bson.Document 参数为key-value的格式
         * 2. 创建文档集合List
         * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List) 插入单个文档可以用 mongoCollection.insertOne(Document)
         * */

        for (int i = 0; i <= 100000; i++) {
            Document document = new Document("title", "MongoDB").
                    append("description", "描述" + (i + 1)).
                    append("likes", 100 + i).
                    append("by", "将文档" + (i + 1) + "集合插入数据库集合中");
            List documents = new ArrayList();
            documents.add(document);
            collection.insertMany(documents);
            System.out.println("文档插入成功");
        }


    }

    public void updateMe() {

        //更新文档   将文档中likes=100的文档修改为likes=200
        //collection.updateMany(Filters.eq("description", "mysql"), new Document("$set", new Document("description", "updatedName")));
        //
        collection.updateMany(Filters.eq("likes", 100), new Document("$set", new Document("likes", 200)));
    }
    //检索所有文档

    /**
     * 1. 获取迭代器FindIterable
     * 2. 获取游标MongoCursor
     * 3. 通过游标遍历检索出的文档集合
     */
    public void showMe() {


        FindIterable findIterable = collection.find();
        MongoCursor mongoCursor = findIterable.iterator();
        int i = 0;
        while (mongoCursor.hasNext()) {
            System.out.println("row:" + (i++) + "  \t" + mongoCursor.next());
        }


    }

    public void deleteMe() {
        //删除符合条件的第一个文档
        //collection.deleteOne(Filters.eq("likes", 200));
        //删除所有符合条件的文档
        collection.deleteMany(Filters.eq("title", "MongoDB"));
    }

    public static void main(String[] args) {


        try {

            MongoTest mongoTest = new MongoTest();

            mongoTest.init();// 初始化

            // mongoTest.insertMe();// 插入
            //mongoTest.updateMe();
            // mongoTest.deleteMe();
            mongoTest.showMe();//显示数据

        } catch (Exception e)

        {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }


    }
}





你可能感兴趣的:(Java)