java如何使用mongodb_java怎么做到使用mongodb的原生命令来执行操作

07e1e7192e4aaf8238c94d3d8a95c73a.png

慕田峪7331174

public class MongoDBJDBC {public static void main(String[] args) {try {// 实例化Mongo对象,连接27017端口Mongo mongo = new Mongo("localhost", 27017);// 连接名为yourdb的数据库,假如数据库不存在的话,mongodb会自动建立DB db = mongo.getDB("test");// Get collection from MongoDB, database named "yourDB"// 从Mongodb中获得名为yourColleection的数据集合,如果该数据集合不存在,Mongodb会为其新建立DBCollection collection = db.getCollection("test1");// 使用BasicDBObject对象创建一个mongodb的document,并给予赋值。BasicDBObject document = new BasicDBObject();//document.put("id", 1001);//document.put("msg", "hello world mongoDB in Java");// 将新建立的document保存到collection中去//collection.insert(document);// 创建要查询的documentBasicDBObject searchQuery = new BasicDBObject();searchQuery.put("name", "chen");// 使用collection的find方法查找documentDBCursor cursor = collection.find(searchQuery);// 循环输出结果while (cursor.hasNext()) {System.out.println(cursor.next());}System.out.println("Hello World");} catch (UnknownHostException e) {e.printStackTrace();} catch (MongoException e) {e.printStackTrace();}}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值