美文网首页我爱编程
MongoDB 数据库和集合(Databases and Col

MongoDB 数据库和集合(Databases and Col

作者: x丶ST | 来源:发表于2018-01-26 16:08 被阅读0次

    On this page

    • Databases
    • Collections

    MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases.

    Databases

    In MongoDB, databases hold collections of documents.

    To select a database to use, in the mongo shell, issue the use <db> statement, as in the following example:

    use myDB
    

    Create a Database

    If a database does not exist, MongoDB creates the database when you first store data for that database. As such, you can switch to a non-existent database and perform the following operation in the mongo shell:

    use myNewDB
    
    db.myNewCollection1.insertOne( { x: 1 } )
    

    The insertOne() operation creates both the database myNewDB and the collection myNewCollection1if they do not already exist.

    For a list of restrictions on database names, see Naming Restrictions.

    Collections

    MongoDB stores documents in collections. Collections are analogous to tables in relational databases.

    Create a Collection

    If a collection does not exist, MongoDB creates the collection when you first store data for that collection.

    db.myNewCollection2.insertOne( { x: 1 } )
    db.myNewCollection3.createIndex( { y: 1 } )
    

    Both the insertOne() and the createIndex() operations create their respective collection if they do not already exist.

    For a list of restrictions on collection names, see Naming Restrictions.

    Explicit Creation

    MongoDB provides the db.createCollection() method to explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules. If you are not specifying these options, you do not need to explicitly create the collection since MongoDB creates new collections when you first store data for the collections.

    To modify these collection options, see collMod.

    Document Validation

    New in version 3.2.

    By default, a collection does not require its documents to have the same schema; i.e. the documents in a single collection do not need to have the same set of fields and the data type for a field can differ across documents within a collection.

    Starting in MongoDB 3.2, however, you can enforce document validation rules for a collection during update and insert operations. See Schema Validation for details.

    Modifying Document Structure

    To change the structure of the documents in a collection, such as add new fields, remove existing fields, or change the field values to a new type, update the documents to the new structure.

    Unique Identifiers

    New in version 3.6.

    NOTE

    The featureCompatibilityVersion must be set to "3.6". For more information, see View FeatureCompatibilityVersion.

    Collections are assigned an immutable UUID. The collection UUID remains the same across all members of a replica set and shards in a sharded cluster.

    To retrieve the UUID for a collection, run either the listCollections command or the db.getCollectionInfos() method.

    MongoDB 在集合中存储BSON文档,即数据记录; 集合在数据库中。

    数据库

    在MongoDB中,数据库拥有文档集合。
    要选择要使用的数据库,请在mongo shell中使用use <db>该语句,如下例所示:

    use myDB
    

    创建数据库

    如果一个数据库不存在,当你第一次存储数据库的数据时,MongoDB会创建数据库。因此,您可以切换到不存在的数据库,并在 mongo shell中执行以下操作 :

    use myNewDB
    db.myNewCollection1.insertOne( { x: 1 } )
    

    insertOne()操作将创建数据库myNewDB和集合(myNewCollection1如果它们尚不存在)。

    有关数据库名称的限制列表,请参阅 命名限制。

    集合

    MongoDB将文档存储在集合中。集合类似于关系数据库中的表

    创建集合

    如果一个集合不存在,当你第一次存储该集合的数据时,MongoDB会创建这个集合。

    db.myNewCollection2.insertOne( { x: 1 } )
    db.myNewCollection3.createIndex( { y: 1 } )
    

    如果它们不存在insertOne()createIndex()操作和 操作都将创建它们各自的集合。

    有关集合名称的限制列表,请参阅 命名限制

    明确创建

    MongoDB提供了db.createCollection()使用各种选项显式创建集合的方法,例如设置最大大小或文档验证规则。如果您没有指定这些选项,那么您不需要显式创建集合,因为在您首次存储集合的数据时,MongoDB会创建新的集合。

    相关文章

      网友评论

        本文标题:MongoDB 数据库和集合(Databases and Col

        本文链接:https://www.haomeiwen.com/subject/pjqwaxtx.html