I. Operation of MONGODB database
1. Create a database
- syntax:use database name
- Description: Create the database if the database does not exist, or switch to the specified database
- Note: If the database you just created is not in the list, if you want to display it, we need to insert some data into the database just now (Db.student.insert {name: ' Tom ', Age:18,gender:1,address: " Beijing ", isdelete:0}))
2. Delete Database
Prerequisites:Using the current database
Db.dropdatabse ()
3. View all Databases
Show DBS
4. View the database that is currently in use
A,Db
B,Db.getname ()
5. Disconnect the connection
Exit
6. View Command API
Help
second, set operation
1. See which collections are in the current database
Show collections
2. Create a collection
A,
Syntax:Db.createcollection (' collection name ')
Example:Db.createcollection (' class ')
B,
Grammar: Db. Collection name. Insert (document)
Example :Db.student.insert ({name: ' Tom ', Age:18,gender:1,address: "Beijing", isdelete:0})
difference:The difference between the two is that the former creates an empty collection, which creates an empty collection and adds a document.
3. Delete the collection in the current database
Syntax: DB. Collection name. Drop ()
Example: Db.class.drop ()
Third, document Operation
1. Inserting documents
A,Inserting a document using the Insert () method
Syntax:Db. Collection name. Insert (document)
Insert One:Db.student.insert ({name: "Lilei", Age:19,gender:1,address: "Beijing", isdelete:0})
Syntax:Db. Collection name. Insert ([Document 1],[document 2],......, [Document N])
Insert Multiple:Db.student.insert ([{name: "Han Meimei", age:20,gender:1,address: "Shanghai", isdelete:0},
{Name: "Sea Sister", Age:19,gender:1,address: "Beijing", isdelete:0}])
B,Inserting a document using the Save () method
Syntax:Db. Collection name. Save (document)
Description:If you do not specify the _id field, the Save () method is similar to insert () if you specify the _id field, the data for the _id field is updated
Example 1:Db.student.save ({name: "Poi", age:21,gender:1,address: "Beijing", isdelete:0})
Example 2:Db.student.save ({_id:object ("5afe6842286b72ee15913653"), Name: "Poi", age:23,gender:1,address: "Beijing", isdelete:0})
2. Document Update
A,The update () method is used to update a document that already exists
Syntax:
Db. Collection name. Update (
Query
Update
{
Upset:<boolean>
Multi:<boolean>
Writeconcern:<document>
})
parameter Description:
Query: The query condition for update, similar to the where statement in SQL update
Update:
Update the object and some updated operators ($set, $inc), and so on. $set update directly, $inc on the original basis after the cumulative update.
Upset:Optional, if the record for update does not exist, if the new data is inserted, True is insert, false is not inserted, false by default.
Multi:Optional, default false to update only the first record found, or true to update the lookup results all.
Writeconcern:Optional, throws an exception level.
Requirements:Update the age of Lilei to 25
Example
Db.student.update ({name: "Lilei"},{$set: {age25:25}})
Db.student.update ({name: "Lilei"},{$inc: {age25:25}}) accumulate
Db.student.update ({name: "Lilei"},{$inc: {age25:25}},{multi:true}) full change
B,The Save () method replaces an existing document with an incoming document
Syntax:
Db. Collection name. Save (DOCUMENT,{WRITECONCERN:<DOCUMENT>})
parameter Description :
Document : Document data
Writeconcern: Optional, throws an exception level
Note:save is basically not used, update is common
3. Document deletion
4. Document Query
5. Disconnect the connection
6.
7.
8.
9.
MongoDB Basic Syntax