MongoDB Learning notes-common commands

Source: Internet
Author: User

Here's a record of what MongoDB often commands

Database related

Create a database

Use database_name

If the database does not exist, create the database, or switch to the specified database.

Delete database : Switch to the database you want to delete, execute the command.

Db.dropdatabase ()

View all databases

Show DBS

Collection related

View all Collections

Show tables

Delete the specified collection : Replace the collection_name of the following command with the execution

Db. collection_name. Drop ()

Document related

Insert Document

Db. Collection_name.insert (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.

Update document

db.collection.update (   <query>,   <update>,   {     <  Boolean>,     <boolean>,     <document>   }) 

Parameter description:

    • Query: The search condition for update, similar to where in SQL update query.
    • Update: Update object and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
    • upsert : Optional, this parameter means that if there is no record of update, insert objnew,true as INSERT, default is False, do not insert.
    • multi : Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.
    • writeconcern : Optional, throws an exception level.

Delete a document

db.collection.remove (   <query>,   {     <boolean>,     <document>   })

Parameter description:

    • query : (optional) The condition of the deleted document.
    • justone : (optional) If set to TRUE or 1, only one document is deleted.
    • Writeconcern : (optional) the level at which the exception is thrown.

Querying documents

Db.collection.find (query, projection)

Parameter description:

    • 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).

MongoDB Learning notes-common commands

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.