Mongodb-introduction to MongoDB, Databases and collections

Source: Internet
Author: User
Tags createindex



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 theMONGOShell, 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 theMONGOShell:


> use myNewDB
switched to db myNewDB
> db.myNewCollection1.insert( { x: 1 } )
...
WriteResult({ "nInserted" : 1 })


TheInsert ()operation creates both the databasemynewdband the collectionMyNewCollection1if They does not already exist.



For a list of restrictions on the database names, see naming restrictions.





Collections


MongoDB stores documents in collections. Collections is 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 theInsert ()and theCreateIndex ()operations create their respective collection if they do not ALR Eady exist.



For a list of restrictions in collection names, see naming restrictions.


Explicit Creation


MongoDB provides thedb.createcollection ()method to explicitly create a collection with various options, such as Setting the maximum size or the documentation validation rules. If you aren't specifying these options, you don't need to explicitly create the collection since MongoDB creates new Col Lections when your first store data for the collections.



To modify these collection options, seecollmod.


Document Validation


New in version 3.2.



By default, a collection does is not require it documents to the same schema; i.e. the documents in a single collection does not need to has the same set of fields and the data type for a field can dif Fer across documents within a collection.



Starting in MongoDB 3.2, however, your can enforce document validation rules for a collection during update and insert Oper Ations. See Document Validation for details.


modifying Document Structure


The structure of the documents in a collection, such as Add new fields, remove existing fields, or change the fi Eld values to a new type, update the documents to the new structure.






Mongodb-introduction to MongoDB, Databases and collections


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.