MongoDB:创建数据集合

点击查看原文。

官网地址:
https://docs.mongodb.com/manual/reference/method/db.createCollection/#db.createCollection

这节有点混乱,也有点尴尬,因为原本MongoDB就有些“没有规矩”。

快速创建一个集合,参考插入这一节。

集合不存在的情况下,插入一条记录就会创建集合。

稍微啰嗦一点,如下:

db.test.insert({
    "_id" : ObjectId("5abb3b5bce69c048be080199"),
    "meta" : {
        "createAt" : ISODate("2018-03-28T06:51:07.579Z"),
        "updateAt" : ISODate("2018-03-28T06:51:07.579Z")
    },
    "a" : "1",
    "b" : "1",
})
WriteResult({ "nInserted" : 1 })

这样即会创建集合test,又会给这个集合插入一条记录。

非要规矩地创建(其实是可以设置一些选项),那么:

db.createCollection(<name>, { capped: <boolean>,
                              autoIndexId: <boolean>,
                              size: <number>,
                              max: <number>,
                              storageEngine: <document>,
                              validator: <document>,
                              validationLevel: <string>,
                              validationAction: <string>,
                              indexOptionDefaults: <document>,
                              viewOn: <string>,
                              pipeline: <pipeline>,
                              collation: <document>,
                              writeConcern: <document>} )
参数 类型 描述
name 字符串 要创建的集合的名称。
options 文档 可选。一大堆选项,暂时没用到,将来再补充了。

猜你喜欢

转载自blog.csdn.net/chaiyu2002/article/details/80862154