MongoDB common Operations--collection 1

Source: Internet
Author: User

1. Create a Collection
There are two ways to display create and implicitly create

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

Implicit creation can use the command DB. Collection 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"})

To delete a collection using the command: DB. Collection name. Drop ()

2. Adding a document to a collection

Use the command DB. Collection name. Insert ({}), for example: Db.user1.insert ({name: "Jack", age:20})

You can insert multiple documents in bulk using the following looping statement
for (Var i=0;i<1000;i++) {
Db.customer.insert ({name: "name" +i, age:i});
}
You can also use the DB. Collection name. Save ({}) to add a document to the collection.
Save is different from insert: Use Insert if _id already exists in the inserted document, it cannot be inserted, similar to primary key conflict in relational database Save method if _id already exists in the inserted document, the update is performed

3. Delete a document from the collection
Use the command DB. Collection name. 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 with name User1 in the C1 collection

MongoDB common Operations--collection 1

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.