Mongodb (2) Create a database, delete a database, create a collection, delete a collection, display the contents of a document

Source: Internet
Author: User

Show all Databases list: Show DBS

> show dbslocal   0. 078GBrunoob  0. 078GB

Show current database: DB

> Dbrunoob

Show All collections: Show Collections--Show all collections of the current database

> Show Collectionscol_1col_2runoobsystem.indexes

CREATE DATABASE: Use database_name-Create a database named database_name and switch to the database you created, or you can switch directly to a database that already exists

> Use mydbswitched to db mydb> dbmydb>show dbs   #新创建的不会显示 because there is no data inside
Local 0.078GB
Runoob 0.078GB
> db.movie.insert ({"Name": "Tutorials Yiibai"}) #向当前数据库插入一个集合movie (if automatic creation is not present) and inserts a document into the collection
Writeresult ({"ninserted": 1})
> Show DBS
Local 0.078GB
MyDB 0.078GB
Runoob 0.078GB
> Show Collections #显示当前数据库中的集合
Movie
System.indexes
> Db.movie.find () #展示集合中的内容
{"_id": ObjectId ("578de9095dadb19a13cd9062"), "name": "Tutorials Yiibai"}
>

Delete database: Db.dropdatabase ()

> show dbslocal   0.078GBmydb    0.078GBrunoob  0.078GB> dbmydb>"  dropped""mydb""OK" : 1 }> show dbslocal   0.078GBrunoob  0.078GB

Create a collection:db. CreateCollection(name, options)

Name is the name of the collection, options (optional) specifies the memory size and index

>show dbslocal 0.078GBrunoob 0.078GB>DB #上步删除之后还会显示mydb>Use Runoob #切换数据库switched to DB Runoob>Dbrunoob>Show Collections #显示当前的集合col_1col_2runoobsystem. indexes
> db.createcollection ("mycollection") #创建集合
{"OK": 1}
> Show Collections #显示所有的集合
Col_1
col_2
mycollection
Runoob
system.indexes
>

Important options for creating collections: CreateCollection ()

> Db.createcollection ("MyCol", {capped:true, autoindexid:true, size:6142800, max:10000 } ){ "OK": 1 }>Show Collectionscol_1col_2mycolmycollectionrunoobsystem.indexes> Db.yiibai.insert ({"name":"Yiibai"}) Writeresult ({"ninserted": 1 })>Show Collectionscol_1col_2mycolmycollectionrunoobsystem.indexesyiibai>

Delete collection:db. Collection_name. Drop()

> show Collectionscol_1col_2mycolmycollectionrunoobsystem.indexesyiibai> db.mycollection.drop ( True> Db.yiibai.drop ()  # successfully deleted the already existing collection true> db.mycol.drop () True > show collectionscol_1col_2runoobsystem.indexes# Delete nonexistent collection, error false 

Use the Find command to view the contents of the document: Db.collection_name.find ()

>Dbrunoob>Db.rundb.runCommand (Db.runoob>Db.runoob.find () {"_id": ObjectId ("578dc96c5dadb19a13cd905e"),"Word":"Hello","Chinese":"It's a Nice day today!","中文版":"Today the weather is well!" }>Show Collectionscol_1col_2runoobsystem.indexes>Db.col_2.find () {"_id": ObjectId ("578dd2b95dadb19a13cd9061"),"name":"Xiaohua"," Age":" the" }>Db.col_1.find () {"_id": ObjectId ("578dd2255dadb19a13cd905f"),"name":"Chenugnag" }{ "_id": ObjectId ("578dd23a5dadb19a13cd9060"),"name":"xiaoming"," Age":" -" }>

Mongodb (2) Create a database, delete a database, create a collection, delete a collection, display the contents of a document

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.