本文主要讲解mongodb3 java API操作,mongodb shell 和java API结合学习
1.首先安装mongodb,网上有教程,我这里用的windos上的mongo
2.开启mongo服务,命令 mongod
客户端连接 命令 mongo:
先说下简单的 操作:
db 查看当前的数据库
show dbs 查看所有数据库
show collections 查看当前的collection
use test 切换到test数据库下
db.help() 查看所有可操作的函数 ,很多函数我就不一一举例了
-----------------------------------------------------------------------------------------------------------------------------------------
mongodb 增删改查操作:
1.增
先创建一个集合user
> db.createCollection("user",{capped:false})
{ "ok" : 1 }
创建一条bson语句
> doc={
... "name":"zhangjun",
... "age":23,
... "hobby":["写代码","吉他"]
... }
{ "name" : "zhangjun", "age" : 23, "hobby" : [ "写代码", "吉他" ] }
插入
> db.user.insert(doc)
WriteResult({ "nInserted" : 1 })
查看
> db.user.find()
{ "_id" : ObjectId("576fa42026f5534
"hobby" : [ "写代码", "吉他" ] }
下来讲解用java实现上述操作
首先给出最新的mongodb driver 3.2.2 maven配置
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.2.2</version>
</dependency>
创建java mavan工程,并且配置好maven 建立类APP
public class App
{
private final static String HOST = "localhost";
private final static Integer PORT = 27017;
private static String default_database_name = "test";
private static MongoClient mongo;
private static MongoDatabase db ;
private static void init(){
mongo = new MongoClient(HOST,PORT);
<span style="white-space:pre"> </span>db = mongo.getDatabase(default_database_name);
}
@SuppressWarnings("unused")
private static void init(String database_name){
mongo = new MongoClient(HOST,PORT);
<span style="white-space:pre"> </span> db = mongo.getDatabase(database_name);
}
<span style="white-space:pre"> </span> public static void main( String[] args )
<span style="white-space:pre"> </span> {
<span style="white-space:pre"> </span>init();
<span style="white-space:pre"> </span>}
}
下来创建一个测试函数
public static void testCreateCollection(){
db.createCollection("testColl");
}
下来看源码 这个db.createCollecitions() 函数必然有重载,因为 mongodb语法是:
db.createCollection(name, options)
参数 | 类型 | 描述 |
---|---|---|
Name | String | 要创建的集合名称 |
Options | Document | (可选)指定有关内存大小和索引选项 |
<TResult> ListCollectionsIterable<TResult> listCollections(Class<TResult> resultClass);
/**
* Create a new collection with the given name.
*
* @param collectionName the name for the new collection to create
* @mongodb.driver.manual reference/commands/create Create Command
*/
void createCollection(String collectionName);
/**
* Create a new collection with the selected options
*
* @param collectionName the name for the new collection to create
* @param createCollectionOptions various options for creating the collection
* @mongodb.driver.manual reference/commands/create Create Command
*/
void createCollection(String collectionName, CreateCollectionOptions createCollectionOptions);
确实有个重载方法,是语法后面的可选参数
public static void testCreateCollection(){
CreateCollectionOptions cc = new CreateCollectionOptions();
cc.capped(false);
db.createCollection("testColl",cc);
}
成功
删除testColl集合
db.testColl.drop()
java代码删除:
public static void testDtop(){
MongoCollection<Document> mo = db.getCollection("testColl");
mo.drop();
}