mongo db java_MongoDB Java 操作

1.编写Java代码连接MongoDB数据库。

2.编写Java代码在MongoDB中创建集合。

3.编写Java代码在MongoDB中获取集合。

4.编写Java代码在MongoDB中插入文档。

5.编写Java代码在MongoDB中检索所有文档。

6.编写Java代码在MongoDB中更新文档。

7.编写Java代码在MongoDB中删除文档。

86a3acd5265f5e1824e9efc97ea73344.png

069baff37602996326e95c4d6d7eab28.png

package DBtest;

import java.util.ArrayList;

import java.util.List;

import org.bson.Document;

import com.mongodb.MongoClient;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoCursor;

import com.mongodb.client.MongoDatabase;

import com.mongodb.client.model.Filters;

public class TestMongoDB {

/**

* @param args

*/

public static void main(String[] args) {

// insert();//插入数据。执行插入时,可将其他三句函数调用语句注释,下同

find(); //查找数据

// update();//更新数据

// delete();//删除数据

}

/**

*返回指定数据库中的指定集合

* @param dbname数据库名

* @param collectionname集合名

* @return

*/

//MongoDB无需预定义数据库和集合,在使用的时候会自动创建

public static MongoCollection getCollection(String dbname,String collectionname){

//实例化一个mongo客户端,服务器地址:localhost(本地),端口号:27017

MongoClient mongoClient=new MongoClient("192.168.223.133",27017);

//实例化一个mongo数据库

MongoDatabase mongoDatabase = mongoClient.getDatabase(dbname);

//获取数据库中某个集合

MongoCollection collection = mongoDatabase.getCollection(collectionname);

return collection;

}

/**

*插入数据

*/

public static void insert(){

try{

//连接MongoDB,指定连接数据库名,指定连接表名。

MongoCollection collection= getCollection("test","test"); //数据库名:School集合名:student

//实例化一个文档,文档内容为{sname:'Mary',sage:25},如果还有其他字段,可以继续追加append

Document doc1=new Document("sname","Mary").append("sage", 25);

//实例化一个文档,文档内容为{sname:'Bob',sage:20}

Document doc2=new Document("sname","Bob").append("sage", 20);

List documents = new ArrayList();

//将doc1、doc2加入到documents列表中

documents.add(doc1);

documents.add(doc2);

//将documents插入集合

collection.insertMany(documents);

System.out.println("插入成功");

}catch(Exception e){

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

}

}

/**

*查询数据

*/

public static void find(){

try{

MongoCollection collection = getCollection("test","test"); //数据库名:School集合名:student

//通过游标遍历检索出的文档集合

// MongoCursor cursor= collection.find(new Document("sname","Mary")). projection(new Document("sname",1).append("sage",1).append("_id", 0)).iterator(); //find查询条件:sname='Mary'。projection筛选:显示sname和sage,不显示_id(_id默认会显示)

//查询所有数据

MongoCursor cursor= collection.find().iterator();

while(cursor.hasNext()){

System.out.println(cursor.next().toJson());

}

}catch(Exception e){

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

}

}

/**

*更新数据

*/

public static void update(){

try{

MongoCollection collection = getCollection("test","test"); //数据库名:School集合名:student

//更新文档将文档中sname='Mary'的文档修改为sage=22

collection.updateMany(Filters.eq("sname", "Mary"), new Document("$set",new Document("sage",22)));

System.out.println("更新成功!");

}catch(Exception e){

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

}

}

/**

*删除数据

*/

public static void delete(){

try{

MongoCollection collection = getCollection("School","student"); //数据库名:School集合名:student

//删除符合条件的第一个文档

collection.deleteOne(Filters.eq("test", "test"));

//删除所有符合条件的文档

//collection.deleteMany (Filters.eq("sname", "Bob"));

System.out.println("删除成功!");

}catch(Exception e){

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

}

}

}

插入数据

650039a524493d23348f6769faf458ba.png

查找数据

8165275ca24e78b27818da7c49d11177.png

更新数据

484e93068f4b5704062accb6a7a5eec0.png

96deaf5675747cf133831ef2bbdd8216.png

删除数据

229f0bd836d366275f4b2cbaaba03537.png

adfbef2c1b7f16571866303e44aaa0f2.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值