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