这里的示例代码都是简要代码,详细代码可以参考我打包好的代码,地址:百度云 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();
这里都是比较简单的例子,仅供学习使用,不喜勿喷。如果以后开发需要,我会继续拓展博客。