zoukankan      html  css  js  c++  java
  • MongoDB

    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
    switched to db myNewDB
    > db.myNewCollection1.insert( { x: 1 } )
    ...
    WriteResult({ "nInserted" : 1 })

    The insert() operation creates both the database myNewDB and the collection myNewCollection1 if 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.insert( { x: 1 } )
    WriteResult({ "nInserted" : 1 })
    > db.myNewCollection3.createIndex( { y: 1 } )
    2016-11-30T11:29:16.665+0800 I INDEX    [conn6] build index on: myNewDB.myNewCollection3 properties: { v: 1, key: { y: 1.0 }, name: "y_1", ns: "myNewDB.myNewCollection3" }
    2016-11-30T11:29:16.665+0800 I INDEX    [conn6] 	 building index using bulk method
    2016-11-30T11:29:16.667+0800 I INDEX    [conn6] build index done.  scanned 0 total records. 0 secs
    {
    	"createdCollectionAutomatically" : true,
    	"numIndexesBefore" : 1,
    	"numIndexesAfter" : 2,
    	"ok" : 1
    }
    > 

    Both the insert() 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 Document 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.

  • 相关阅读:
    算法设计之hash---hash 函数、hash表
    图像处理之换脸---手把手教你使用 Deepfakes 换脸
    图像处理之搜图---实现以图搜图
    机器学习之python---Python实现逻辑回归(LogisticRegression)
    嵌入式开发之usb 转 net --- 支持持USB 网络适配器方案
    嵌入式开发之网卡--- Ethernet 以太网 MAC、MII、PHY、MDIO、IEEE802.3 详解
    机器学习之RNN ---LSTM原理及实现详解
    Docker的学习
    网络7层 4层 5层 协议
    netstat 查看端口、进程占用
  • 原文地址:https://www.cnblogs.com/huey/p/6117328.html
Copyright © 2011-2022 走看看