MongoDB Base Operation

Source: Internet
Author: User
Tags mongodb collection mongodb query

1. Create a database use Data_name

Use database name

Returns an existing database if the specified database already exists, creates a new database if it does not exist

Create a database with the name newdb

Use newdb

View a list of databases: Show DBS

As you can see, the newdb you just created is not in the list, to display the database, you need to insert at least one empty document, and the empty database is not displayed.

The default database in MongoDB is: test . If you have not created any databases, the collections/documents will be stored in the test database.

2. Delete Database Db.dropdatabase ()

Db.dropdatabase () command to delete the current database (default delete test)

3. Create a collection createcollection ()

db.createCollection(name,options)method is used to create a collection in MongoDB.

In the command, name is the name of the collection to be created. optionsis a document that specifies the configuration of the collection.

>use testswitched to db test>db.createCollection("mycollection"){ "ok" : 1 }>

You can use show collections the command to check the created collection.

>show collectionsmycollection

The following example shows the syntax of the createCollection() method, with several important options-

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

In MongoDB, you do not need to create a collection. When you insert some documents, MongoDB automatically creates the collection.

>db.newcollection.insert({"name" : "yiibaitutorials"})>show collectionsmycolnewcollectionmycollection>
4. Delete Collection Drop ()

Db. Collection_name.drop ()

The show Collections command looks at the current collection and removes the collection named Lcgcol:

If the selected collection is successfully deleted, the drop() method returns true , otherwisefalse

5. Data type

MongoDB supports many data types. Some of them are-

  • string -This is the most common data type used to store data. The string in MongoDB must be UTF-8 .
  • integer -This type is used to store numeric values. Integers can be 32 bits or 64 bits, depending on the server.
  • Boolean type -this type is used to store Boolean values ( true / false ).
  • double-precision floating point number-this type is used to store floating-point values.
  • min/Max key -This type is used to compare values to the minimum and maximum BSON elements.
  • Array -This type is used to store an array or list or multiple values into a single key.
  • Timestamp-When the ctimestamp document is modified or added, it is easy to record.
  • Object -This data type is used for embedded documents.
  • Object -This data type is used for embedded documents.
  • Null -This type is used to store Null values.
  • symbol -The data type is the same as the string; However, the language that is used to use a particular symbol type is usually reserved.
  • Date -This data type is used to store the current date or time in the UNIX time format. You can specify the date time that you want by creating a Date object and specifying the date, month, and year of the day.
  • Object ID -This data type is used to store the document ID.
  • Binary Data -This data type is used to store binary data.
  • code -This data type is used to store JavaScript code in a document.
  • Regular Expression -this data type is used to store regular expressions.
6. Inserting a document insert ()

Db. Collection_name.insert (document)

To insert data into a MongoDB collection, you need to use the Insert () or Save () method in MongoDB.

The following documents can be stored in the Lcgcollection collection of the HELLODB database:

In the above example, Lcgcollection is our collection name, and if the collection is not in the database, MongoDB automatically creates the set and inserts the document.

The find () command to view the inserted document:

We can also define the data as a variable, as follows:

After execution, the results are displayed as follows:

To perform an insert operation:

In the inserted document, if you do not specify _id a parameter, MongoDB assigns a unique objectid to the document.

Insert document You can also use the Db.col.save command. If you do not specify the _id field, the Save () method is similar to the Insert () method. If the _id field is specified, the data for the _id is updated.

To insert multiple documents in a single query, you can insert() pass an array of documents in the command.

Other ways to insert a document

The Db.collection.insertOne () method inserts a single document into the collection

The Db.collection.insertMany () method inserts multiple documents into the collection

7. Querying the document find ()

The syntax format for MongoDB query data is as follows:

DB. Collection. Find(query, projection)

    • Query: Optional, use the query operator to specify the criteria
    • Projection: optional, use the projection operator to specify the returned key. Returns all the key values in the document when queried, just omit the argument (omitted by default).

The Find () method can display all documents in an unstructured manner.

To display the results in a formatted manner, you can use the pretty () method.

As follows:

Db.lcgcollection.find ()

Db.lcgcollection.find (). Pretty ()

In addition find() to the method, there is a findOne() method that returns only one document.

----have to go to dinner after the----

MongoDB Base Operation

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.