您的位置:首页 > 数据库 > Mongodb

mongo-java-driver -3.2.2学习笔记-02-MongoDB Driver Admin Quick Tour

2017-12-05 11:58 603 查看
得到数据库并操作文档

MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");


得到数据库的名称列表

for (String name: mongoClient.listDatabaseNames()) {
System.out.println(name);
}


删除数据库

mongoClient.getDatabase("databaseToBeDropped").drop();


建立集合

database.createCollection("cappedCollection",
new CreateCollectionOptions().capped(true).sizeInBytes(0x100000));


得到集合的列表

for (String name : database.listCollectionNames()) {
System.out.println(name);
}


删除集合

collection.drop();


建立index

// create an ascending index on the "i" field
collection.createIndex(Indexes.ascending("i"));
// create a text index on the "content" field
coll.createIndex(Indexes.text("content"));


得到某个文档的所有的索引

for (final Document index : collection.listIndexes()) {
System.out.println(index.toJson());
}


跑命令,不是所有的额操作都有明确的helper,可以使用commad来操作

Document buildInfo = database.runCommand(new Document("buildInfo", 1));
System.out.println(buildInfo);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: