首页 > 数据库技术 > 详细

java 操作mongoDB做增删改查

时间:2019-11-29 11:34:36      阅读:97      评论:0      收藏:0      [点我收藏+]

Java代码操作mongoDB

package common.mongo;

import com.mongodb.*;
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 org.bson.conversions.Bson;
import java.util.ArrayList;
import java.util.List;

public class Test {

    private static MongoDatabase db=null;

    static{
        List<ServerAddress> adds = new ArrayList<>();
        //ServerAddress()两个参数分别为 服务器地址 和 端口
        ServerAddress serverAddress = new ServerAddress("127.0.0.1", 27017);
        adds.add(serverAddress);

        List<MongoCredential> credentials = new ArrayList<>();
        //MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("root", "admin", "123456".toCharArray());
        credentials.add(mongoCredential);

        //通过连接认证获取MongoDB连接
        MongoClient mongoClient = new MongoClient(adds, credentials);

        //连接到数据库
        db = mongoClient.getDatabase("runoob");

    }

    public static void insertOne(){
        MongoCollection<Document> collection = db.getCollection("user2");
        Document document=new Document()
                .append("id",102)
                .append("name","刘飞")
                .append("sex","男")
                .append("age",20);
        collection.insertOne(document);
    }
    public static void insertMany(){
        MongoCollection<Document> collection = db.getCollection("user2");
        Document document=new Document()
                .append("id",102)
                .append("name","刘飞")
                .append("sex","男")
                .append("age",20);
        Document document2=new Document()
                .append("id",101)
                .append("name","刘飞")
                .append("sex","男")
                .append("age",20);
        List<Document> documents=new ArrayList<>();
        documents.add(document);
        documents.add(document2);
        collection.insertMany(documents);
    }
    public static void deleteOne(){
        MongoCollection<Document> collection = db.getCollection("user2");
        Bson filter = Filters.eq("age",20);
        collection.deleteOne(filter);
    }
    public static void deleteMany(){
        MongoCollection<Document> collection = db.getCollection("user2");
        Bson filter = Filters.eq("age",20);
        collection.deleteMany(filter);
    }
    //取出查询到的第一个文档
    public static void findOne(){
        //获取集合
        MongoCollection<Document> collection = db.getCollection("user2");
        //查找集合中的所有文档
        FindIterable findIterable = collection.find();
        //取出查询到的第一个文档
        Document document = (Document) findIterable.first();
        //打印输出
        System.out.println(document);
    }
    public static void findMany(){
        MongoCollection<Document> collection = db.getCollection("user2");
        Bson filter = Filters.eq("age",20);
        //指定查询过滤器查询
        FindIterable findIterable = collection.find(filter);
        MongoCursor cursor = findIterable.iterator();
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    }
    public static void findAll(){
        //获取集合
        MongoCollection<Document> collection = db.getCollection("user2");
        //查找集合中的所有文档
        FindIterable findIterable = collection.find();
        MongoCursor cursor = findIterable.iterator();
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    }
    public static void updateOne(){
        //获取集合
        MongoCollection<Document> collection = db.getCollection("user2");
        //修改过滤器
        Bson filter = Filters.eq("name", "刘飞");
        //指定修改的更新文档
        Document document = new Document("$set", new Document("age", 30));
        //修改单个文档
        collection.updateOne(filter, document);
    }
    public static void updateMany(){
        //获取集合
        MongoCollection<Document> collection = db.getCollection("user2");
        //修改过滤器
        Bson filter = Filters.eq("name", "刘飞");
        //指定修改的更新文档
        Document document = new Document("$set", new Document("age", 25));
        //修改单个文档
        collection.updateMany(filter, document);
    }

    public static void insertOneCompany(Document document){
        MongoCollection<Document> collection = db.getCollection("companys");
        collection.insertOne(document);
    }
    public static void insertManyCompany(List<Document> lists){
        MongoCollection<Document> collection = db.getCollection("companys");
        collection.insertMany(lists);
    }
//    public static void main(String[] args) {
//        updateMany();
//    }
}

 

 

java 操作mongoDB做增删改查

原文:https://www.cnblogs.com/yanghe123/p/11956632.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!