淘先锋技术网

首页 1 2 3 4 5 6 7

nosql中通过Java访问MongoDB数据库以及增删改查

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 javax.swing.text.*;

public class MongoDemo {
    public static void main(String[] args) {
        try{
            MongoClient mongoclient=new MongoClient("localhost",27017);//连接mongodb服务器
            System.out.println("连接服务器成功");
            MongoDatabase database=mongoclient.getDatabase("MongoDBConn");//连接数据库MongoDBConn
            System.out.println("连接数据库成功");
            MongoCollection collection=database.getCollection("stuinf");//切换到集合stuinf
            System.out.println("切换到集合成功");
            System.out.println("******************************");

            //增
            Document document1=new Document("_id",1001).append("name","zhao").append("score",80);
            Document document2=new Document("_id",1002).append("name","qian").append("score",90);
            Document document3=new Document("_id",1003).append("name","sun").append("score",81);
            Document document4=new Document("_id",1004).append("name","li").append("score",91);
            Document document5=new Document("_id",1005).append("name","zhou").append("score",82);
            Document document6=new Document("_id",1006).append("name","wu").append("score",81);
            collection.insertOne(document1);
            collection.insertOne(document2);
            collection.insertOne(document3);
            collection.insertOne(document4);
            collection.insertOne(document5);
            collection.insertOne(document6);
            System.out.println("增加成功");
            System.out.println("******************************");
            //删
            collection.deleteOne(document1);//删除document1文档
            System.out.println("删除成功");
            System.out.println("******************************");
            collection.deleteMany(new Document("score",81));//删除指定内容全部文档
            System.out.println("删除成功");
            System.out.println("******************************");
            //改
            collection.updateOne(Filters.eq("name","li"),new Document("$set",new Document("name","lili")));
            System.out.println("修改成功");
            System.out.println("******************************");
            //查
            FindIterable iterable=collection.find();
            MongoCursor cursor=iterable.iterator();
            while(cursor.hasNext()){
                System.out.println(cursor.next());
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

效果图如下:
在这里插入图片描述

在这里插入图片描述

增加另一种方式

            List list=new ArrayList();
            list.add(new Document("_id",111).append("name","1").append("score",91));
            list.add(new Document("_id",222).append("name","2").append("score",92));
            list.add(new Document("_id",333).append("name","3").append("score",93));
            list.add(new Document("_id",444).append("name","4").append("score",94));
            list.add(new Document("_id",555).append("name","5").append("score",95));
            list.add(new Document("_id",666).append("name","6").append("score",96));
            
            collection.insertMany(list);