MongoDB学习日记 - java代码(七):runCommand & BasicDBObject

这篇博客主要介绍了在Java中使用MongoDB的runCommand和BasicDBObject进行数据库操作。文中提供了简要的代码示例,并指出完整代码可在百度云mongo-demo.rar找到,适用于学习用途。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里的示例代码都是简要代码,详细代码可以参考我打包好的代码,地址:百度云 mongo-demo.rar ,这部分代码在other中

runCommand

MongoClient client = new MongoClient();
        MongoDatabase database = client.getDatabase("mydb");
        // buildInfo 版本信息 
        // Document{{version=3.0.6, 
        //           gitVersion=1ef45a23a4c5e3480ac919b28afcba3c615488f2, 
        //           targetMinOS=Windows 7/Windows Server 2008 R2, 
        //           OpenSSLVersion=OpenSSL 1.0.1p-fips 9 Jul 2015, 
        //           sysInfo=windows sys.getwindowsversion(major=6,
        //           minor=1, build=7601, platform=2, service_pack='Service Pack 1') 
        //           ...
        // }}
        Document buildInfo = database.runCommand(new Document("buildInfo", 1));
        System.out.println(buildInfo);

        client.close();

语法解释:

db.runCommand( { "buildInfo" : 1 } )

BasicDBObject

注:这是一个用来查询的类

MongoClient client = new MongoClient();     
MongoDatabase database = client.getDatabase("mydb");

// Pass BasicDBObject.class as the second argument
MongoCollection<BasicDBObject> collection = database.getCollection("mycoll", BasicDBObject.class);

// insert a document
BasicDBObject document = new BasicDBObject("x", 1);
collection.insertOne(document);
document.append("x", 2).append("y", 3);

// replace a document
collection.replaceOne(Filters.eq("_id", document.get("_id")), document);

// find documents
List<BasicDBObject> foundDocument = collection.find().into(new ArrayList<BasicDBObject>());
for (BasicDBObject basicDBObject : foundDocument) {
    System.out.println(basicDBObject);
}

client.close();

这里都是比较简单的例子,仅供学习使用,不喜勿喷。如果以后开发需要,我会继续拓展博客。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值