MongoDB Common operations

Source: Internet
Author: User

1.MongoDB operations commands for common operations 1.1 databases

1. Create the database and use the command with the database name , such as using SXF.

* Note: 1.the name of the database followed by the use command, if present, is entered into this database and created if it does not exist, so this creation is also known as an implicit creation

2. After creating the database with the command using SXF, the corresponding data file is not actually generated, and if you exit at this point, the database will be deleted and the data file will not actually be generated until the collection is created in this database

2. Delete the current database, using the command db.dropdatabase ()

3. View all databases, use the command show DBS

4. View the database in which you are currently using command DB

1.2 Integrated Operation Command 1.2.1 display collection

View all collections in the current database, use the command show collections or use show tables

1.2.2 Creating a collection   

1. There are two ways to create a collection, showing creation and implicit creation

2. Display creation can use command db.createcollection ("collection name")

3. Implicit creation can use the command DB. Set name. Insert ({}), which refers to creating a collection and inserting data into the collection at the same time, for example: Db.customer.insert ({name: "Jack"})

1.2.3 Deleting a collection

    The db.dropdatabase () command is used to delete an existing collection. This will delete the selected collection. If no database has been selected, then it will delete the default ' Test ' collection.

1.3 Document Operations Command 1.3.1 Add a document

Add a document to the collection, using the command db. Collection name. Insert ({}), for example: Db.user1.insert ({name: "Jack", age:20})

   1.3.2 Deleting a document 

Delete the document from the collection, using the command db. remove ({delete condition}), without deleting the condition to delete all documents in the collection,

For example:db.c1.remove () to delete all documents in the C1 collection, Db.c1.remove ({name: "user1"}) to delete a document in C1 collection with name User1

1.3.3 Querying documents

1. Query the collection for documents that meet the criteria, you can use the command db. Collection name. Find ({condition})

2. Query the first document db. Collection name. FindOne ()

    

MongoDB Common operations

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.