10月22日

 

 今天学习Mongo的api操作,用相关操作来完成实验报告

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.mongodb.client.*;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import org.bson.Document;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Filter;

import static com.mongodb.client.model.Filters.*;

public class api {
    private MongoClient mongoClient=null;
    private MongoDatabase database=null;

    @Before
    public void init(){
        mongoClient = MongoClients.create("mongodb://localhost:27017");
        database = mongoClient.getDatabase("teat");
    }

    @After
    public void close()
    {
        if(mongoClient!=null){
            mongoClient.close();
        }
    }
    @Test
    public void t(){
        MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
        MongoDatabase database = mongoClient.getDatabase("teat");
        MongoCollection<Document> collection =database.getCollection("Student");
        System.out.println("------"+collection.find().first().toJson());
        if(mongoClient!=null){
            mongoClient.close();
        }
    }

    @Test
    public void insert(){
        MongoCollection<Document> collection =database.getCollection("t2");
//        Document doc=new Document("name","为什么")
//                .append("score",new Document("English","100").append("Math","100").append("Computer","100"));
//
//        Document doc1=new Document("name","你在哪")
//                .append("score",new Document("English",100).append("Math",100).append("Computer",100));
//
//        Document doc2=new Document("name","scofield")
//                .append("score",new Document("English",45).append("Math",89).append("Computer",100));
//        collection.insertOne(doc2);

        //批量插入
        List<Document> documents=new ArrayList();
        for(int i=0;i<5;i++){
            Document document=new Document("i",1);
            documents.add(document);
        }
        collection.insertMany(documents);

    }
    @Test
    public void gai() {
        MongoCollection<Document> collection = database.getCollection("t2");
        //$set 表示更新部分字段
        //collection.updateOne(Filters.eq("i",1),new Document("$set",new Document("i",999)));

        //批量修改
        collection.updateMany(Filters.eq("i",1),new Document("$set",new Document("i",999)));
    }
    @Test
    public void shan() {
        MongoCollection<Document> collection = database.getCollection("t2");

       // collection.deleteOne(Filters.eq("i",0));
        //批量删除
        collection.deleteMany(Filters.eq("i",999));
    }
    @Test
    public void cha() {
        MongoCollection<Document> collection = database.getCollection("Student");
        //FindIterable<Document>documents=collection.find(Filters.eq("i",2));
        //通过filters的式子进行连接多条条件,多条件查询
       // FindIterable<Document>documents=collection.find(or(Filters.eq("i",2),eq("i",4)));
        //通过投影指定显示那些字段
        FindIterable<Document>documents=collection.find(Filters.eq("name","scofield"))
                .projection(Projections.fields(Projections.include("score"),Projections.excludeId()));

        for(Document document:documents){
            System.out.println(document.toJson());
            /**用阿里的FastJson包
             *
             * 该方法用于将已有的json字符串转换为json对象,并取出该对象中相应的key对应的value值
             * 将已有的字符串转换成jsonobject,用JSON.parseObject(jsonStr)方法
             * json中只要是{}就代表一个JSONObject,[]就代表一个JSONArray
             * 获取JSONObject对象用JSONObject jsonobject.getJSONObject("key")方法
             * 获取JSONArray对象用JSONObject jsonobject.getJSONArray("key")方法
             */
            JSONObject jsonobj = JSON.parseObject(document.toJson()); //将json字符串转换成jsonObject对象
            System.out.println(jsonobj.getJSONObject("score").getString("Math"));//获取嵌套的json字符串中的属性值
        }
    }

}

学习时间:3个小时22分钟

posted @ 2021-10-22 22:43  不咬牙  阅读(64)  评论(0编辑  收藏  举报