6.29MongoDB之"增"

MongoDB之"增"

MongoDB创建数据库

语法:

use DATABASE_NAME

如果数据库不存在,则创建数据库,否则切换到指定数据库。

查看所有数据库:

show dbs

刚创建好的数据库没数据不会显示在所有的集合中。需要往里面插入数据。

MongoDB 中默认的数据库为 test,如果你没有创建新的数据库,集合将存放在 test 数据库中。

MongoDB创建集合

集合相当于关系型数据库的表格。

MongoDB 中使用 createCollection() 方法来创建集合。

语法:

db.createCollection(name,option)

参数说明:

  • name: 要创建的集合名称

  • options: 可选参数, 指定有关内存大小及索引的选项

options 可以是如下参数:

字段类型描述
capped 布尔 (可选)如果为 true,则创建固定集合。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。 当该值为 true 时,必须指定 size 参数。
autoIndexId 布尔 3.2 之后不再支持该参数。(可选)如为 true,自动在 _id 字段创建索引。默认为 false。
size 数值 (可选)为固定集合指定一个最大值,即字节数。 如果 capped 为 true,也需要指定该字段。
max 数值 (可选)指定固定集合中包含文档的最大数量。

在插入文档时,MongoDB 首先检查固定集合的 size 字段,然后检查 max 字段。

capped collections

Capped collections 就是固定大小的collection。

有很高的性能以及队列过期的特性(过期按照插入的顺序). 有点和 "RRD" 概念类似。

Capped collections 是高性能自动的维护对象的插入顺序。它非常适合类似记录日志的功能和标准的 collection 不同,你必须要显式的创建一个capped collection,指定一个 collection 的大小,单位是字节。collection 的数据存储空间值提前分配的。

Capped collections 可以按照文档的插入顺序保存到集合中,而且这些文档在磁盘上存放位置也是按照插入顺序来保存的,所以当我们更新Capped collections 中文档的时候,更新后的文档不可以超过之前文档的大小,这样话就可以确保所有文档在磁盘上的位置一直保持不变。--->一致性

由于 Capped collection 是按照文档的插入顺序而不是使用索引确定插入位置,这样的话可以提高增添数据的效率。MongoDB 的操作日志文件 oplog.rs 就是利用 Capped Collection 来实现的。

要注意的是指定的存储大小包含了数据库的头信息。

实例:

db.createCollection("mycoll", {capped:true, size:100000})
  • 在 capped collection 中,你能添加新的对象。

  • 能进行更新,然而,对象不会增加存储空间。如果增加,更新就会失败 。

  • 使用 Capped Collection 不能删除一个文档,可以使用 drop() 方法删除 collection 所有的行。

  • 删除之后,你必须显式的重新创建这个 collection。

  • 在32bit机器中,capped collection 最大存储为 1e9( 1X109)个字节。

Java使用数据库创建集合

package mongodbtest;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;

import java.util.ArrayList;
import java.util.List;

/**
* 使用MongoDB连接到服务器环境上
* @since JDK 1.8
* @date 0221/6/29
* @author Lucifer
*/
public class mongoconnection {

   /*配置属性*/
   private static final String MONGO_HOST = "localhost";
//   private static final Integer MONGO_PORT = 27037;
   private static final Integer MONGO_PORT = 27017;
//   private static final String MONGO_USER = "admin";
//   private static final String MONGO_PASSWORD = "r123456";
   private static final String MONGO_DB_NAME = "practice";
   private static final String GATHER_NAME = "teacher";

   public static void getConnection(){
       /*用事务连接到远程的MongoDB服务器上*/
       try {
           /*使用java包下的类构造一个连接对象*/
           ServerAddress serverAddress = new ServerAddress(MONGO_HOST, MONGO_PORT);

           /*集群连接方法--->构造列表*/
           List<ServerAddress> addresses = new ArrayList<ServerAddress>();
           addresses.add(serverAddress);

//           /*使用Mongo包下的类MongoCredential建立连接对象*/
//           //三个参数:用户名、数据库名称、密码
//           MongoCredential credential = MongoCredential.createScramSha1Credential(MONGO_USER,
//                   MONGO_DB_NAME,
//                   MONGO_PASSWORD.toCharArray()); //最后一个形参需要转换成字符类型而不是字符串类型,使用toCharArray方法
//           /*集群连接方法--->构造列表*/
//           List<MongoCredential> credentials = new ArrayList<MongoCredential>();
//           credentials.add(credential);

           /*通过连接认证MongoDB连接--->使用MongoDBClient类下的方法*/
           MongoClient mongoClient = new MongoClient(addresses); //放入列表对象

           System.out.println("Connect to database successfully!");

           /*连接到数据库*/
           MongoDatabase mongoDatabase = mongoClient.getDatabase(MONGO_DB_NAME);

           mongoDatabase.createCollection(GATHER_NAME);
           System.out.println("create gather successfully!");
      }catch (Exception e){
           System.err.println(e.getClass().getName() + ":" + e.getMessage());
      }
  }
   /*
   步骤概括:
   1、利用ServerAddress类访问到地址和端口
   2、使用MongoDB的jar包下封装好的证书类(Credential)验证身份信息--->构造器:用户名、数据库名称、密码
   3、使用Mongo客户端类封装好的方法验证是否连接成功--->构造器:Address类的列表、Credential类的列表
   4、use指定的数据库--->使用mongoDatabase类下的方法连接到数据库
    */

   public static void main(String[] args) {
       getConnection();
  }
}

Java使用数据库获取集合

package mongodbtest;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;

/**
* 使用MongoDB连接到服务器环境上
* @since JDK 1.8
* @date 0221/6/29
* @author Lucifer
*/
public class mongoconnection {

   /*配置属性*/
   private static final String MONGO_HOST = "localhost";
//   private static final Integer MONGO_PORT = 27037;
   private static final Integer MONGO_PORT = 27017;
//   private static final String MONGO_USER = "admin";
//   private static final String MONGO_PASSWORD = "r123456";
   private static final String MONGO_DB_NAME = "practice";
   private static final String GATHER_NAME = "teacher";

   public static void getConnection(){
       /*用事务连接到远程的MongoDB服务器上*/
       try {
           /*使用java包下的类构造一个连接对象*/
           ServerAddress serverAddress = new ServerAddress(MONGO_HOST, MONGO_PORT);

           /*集群连接方法--->构造列表*/
           List<ServerAddress> addresses = new ArrayList<ServerAddress>();
           addresses.add(serverAddress);

//           /*使用Mongo包下的类MongoCredential建立连接对象*/
//           //三个参数:用户名、数据库名称、密码
//           MongoCredential credential = MongoCredential.createScramSha1Credential(MONGO_USER,
//                   MONGO_DB_NAME,
//                   MONGO_PASSWORD.toCharArray()); //最后一个形参需要转换成字符类型而不是字符串类型,使用toCharArray方法
//           /*集群连接方法--->构造列表*/
//           List<MongoCredential> credentials = new ArrayList<MongoCredential>();
//           credentials.add(credential);

           /*通过连接认证MongoDB连接--->使用MongoDBClient类下的方法*/
           MongoClient mongoClient = new MongoClient(addresses); //放入列表对象

           System.out.println("Connect to database successfully!");

           /*连接到数据库*/
           MongoDatabase mongoDatabase = mongoClient.getDatabase(MONGO_DB_NAME);

           mongoDatabase.createCollection(GATHER_NAME);
           System.out.println("create gather successfully!");

           /*通过MongoCollection接口获取到集合*/
           MongoCollection<Document> collection = mongoDatabase.getCollection(GATHER_NAME);
           System.out.println("获取集合成功");

      }catch (Exception e){
           System.err.println(e.getClass().getName() + ":" + e.getMessage());
      }
  }
   /*
   步骤概括:
   1、利用ServerAddress类访问到地址和端口
   2、使用MongoDB的jar包下封装好的证书类(Credential)验证身份信息--->构造器:用户名、数据库名称、密码
   3、使用Mongo客户端类封装好的方法验证是否连接成功--->构造器:Address类的列表、Credential类的列表
   4、use指定的数据库--->使用mongoDatabase类下的方法连接到数据库
    */

   public static void main(String[] args) {
       getConnection();
  }
}

 

It's a lonely road!!!
原文地址:https://www.cnblogs.com/JunkingBoy/p/14951864.html