Getting Started with MongoDB---Basic Operations of Database &&& Collections

    MongoDB, as a nosql database, has some differences in its own damage, modification, and creation and display of database collections compared with general databases. Let's take a look at some basic operations of MongoDB today.

   First of all, it is to create and delete the database first. Take a look at the example:

 

>use luyaran  switched to db luyaran
> db luyaran >

    With the above code, we have created a database named luyaran, we can use the following command to see which databases are available:

 

> show dbs
local0.078GB test 0.078GB>   

    Did you not see the database we just created, hey, that is because there is no data in the database we just created, then we can insert some data into it to show the database we just created:

 

> db.luyaran.insert({"name":"luyaran"})WriteResult({"nInserted":1})> show dbs local0.078GB luyaran 0.078GB test 0.078GB>        

        Well, the database is created here. The next step is to practice deleting the database. The syntax is as follows:

 

db.dropDatabase()

     Next, let's take a look at the example, the first is to show all the databases:

 

> show dbs
local0.078GB luyaran 0.078GB test 0.078GB   

     Next we switch to the database luyaran:

 

>use luyaran  switched to db luyaran
>

     Then, we will start executing the delete command:

 

> db.dropDatabase(){"dropped":"luyaran","ok":1}        

     Finally, let's use the following code to check whether the database has been deleted:

 

> show dbs
local0.078GB test 0.078GB>   

     At this point, the operation of the database is basically finished, and then let's take a look at the operation of the collection.

     The first is to create a collection, the specific syntax is as follows:

 

db.createCollection(name, options)

         The name above is the name of the collection, and the latter is an optional parameter that can be used to specify options for memory size and index, as follows:

 

field Types of describe
capped boolean (Optional) If true, creates a fixed collection. A fixed collection is a collection with a fixed size that automatically overwrites the oldest documents when the maximum value is reached.
When the value is true, the size parameter must be specified.
autoIndexId boolean (Optional) If true, automatically create an index on the _id field. Defaults to false.
size Numerical value (Optional) Specify a maximum value (in bytes) for the fixed collection.
This field also needs to be specified if capped is true.
max Numerical value (Optional) Specifies the maximum number of documents to contain in a fixed collection.

        Next, let's get a feel for it through an example:

 

>use luyaran  switched to db luyaran
> db.createCollection("luyaran"){"ok":1}>      

     Through the above code, we have created a collection of luyaran, we can view the collection we created through the following code:

 

> show collections
luyaran
system.indexes

     The following is an example with several key parameters, let's feel it (create a fixed collection luyaran, the entire collection space size is 6142800KB, and the maximum number of documents is 10000):

  

> db.createCollection("luyran",{ capped :true, autoIndexId :true, size :6142800, max :10000}){"ok":1}>             

    There is another way to create a collection, that is to insert the document directly, MongoDB will create it automatically, take a look at the example:

 

> db.luyaran.insert({"name":"luyaran"})> show collections luyaran ...   

     The collection is almost created here, and the next step is to try to delete the collection. The syntax format is:

 

db.collection.drop()

     If the deletion is successful, it will return true, and if it fails, it will return false. Next, it will be explained through an example. In the database luyaran, let's first look at all the collections:

 

>use luyaran
switched to db luyaran
>show collections luyaran system.indexes >

     When you're done, just delete the collection luyaran:

 

>db.luyaran.drop()true>  

    Let's take a look at all the collections in the luyaran database:

 

>show collections
system.indexes >

    OK, no more, hehe, here, the collection is almost the end. Today's sharing is almost here, I declare again, this baby is purely learning by reading the materials this time, there are inevitably shortcomings, everyone, please don't spray, if you think it's good, it's better to like and support more Ha, if you have any good suggestions, you can leave a message. If I see it, I will reply as soon as possible. . .

    Finally, I wish you all a happy day. . .

   Original link: https://blog.csdn.net/luyaran/article/details/79663891

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324915955&siteId=291194637