MongoDB Basic Commands
After the MongoDB is successfully started, you can then open a command line window to enter MONGO to perform some operations on the database.
Enter help to see basic operations commands:
Show DBS: Displaying a list of databases
Show Collections: Displays the collection in the current database (similar to tables in a relational database)
Show Users: displaying user
Use <db name>: Toggle the current database, which is the same as in Ms-sql.
Db.help (): Display database operation commands, there are a lot of commands
Db.foo.help (): Displays the set Operation command, also has a lot of commands, Foo refers to the current database, a collection called Foo, not the true meaning of the command
Db.foo.find (): Data lookup for the Foo collection in the current database (all data is listed because there are no conditions)
Db.foo.find ({a:1}): Finds a collection of Foo in the current database, provided that one of the properties in the data is called a, and A has a value of 1
Database Common Commands
1. Help View command Prompt
Help
Db.help ();
Db.yourColl.help ();
Db.youColl.find (). Help ();
Rs.help ();
2. Switch/CREATE Database
Use Yourdb; The current database is created automatically when a collection (table) is created
MongoDB does not have a command to create the database, but has a similar command.
For example, if you want to create a "myTest" database, run the use myTest command before inserting some data into the Runoob database.
For example
Instance the following instance we created the database Runoob: > Use Runoob switched to D b runoob > DB runoob > If you want to view all databases, you can use show DBS
Command: > Show dbs local 0.078GB test 0.078GB >
As you can see, the database we just created Runoob is not in the list of databases, and to show it, we need to insert some data into the Runoob database.
> Db.runoob.insert ({"Name": "Rookie Tutorial"}) Writeresult ({"ninserted": 1}) > show DBS
Local 0.078GB runoob 0.078GB Test 0.078GB > The default database in MongoDB is test, and if you do not create a new database, the collection will be stored in the test database.
3, query all databases
Show DBS;
4, delete the current use of the database
Db.dropdatabase ();
5. Collection (document)
5.1 Deleting a collection
Db.collection.drop ()
The following instance deletes the collection site from the Runoob database:
> Use Runoob
switched to DB Runoob
> Show Tables
site
> Db.site.drop ()
true
> Show Tables
>
6. Document
The data structure of the document is basically the same as that of JSON.
All data stored in the collection is in Bson format.
Bson is a binary form of class JSON storage format, referred to as binary JSON.
6.1 Creating a Document
Format
Db. Collection_name.insert (document) if the collection is not in the database, MongoDB automatically creates the set and inserts the document.
Example
Db.col.insert ({title: ' MongoDB Tutorial ', Description: ' MongoDB is a Nosql database ', by: ' Rookie tutorial ', url: ' Http://www.runoob . com ', Tags: [' mongodb ', ' database ', ' NoSQL '], likes:100})We can also define the data as a variable, as follows:
>document= ({title: ' MongoDB Tutorial ',
Description: ' MongoDB is a Nosql database ', by
: ' Rookie tutorial ',
URL: ' http:// Www.runoob.com ',
Tags: [' mongodb ', ' database ', ' NoSQL '],
likes:100
});
To perform an insert operation:
> Db.col.insert (document)
Writeresult ({"ninserted": 1})
Insert document You can also use the Db.col.save command. If you do not specify the _id field Save () method is similar to the Insert () method. If you specify a _id field, the data for that _id is updated.
View the inserted document
Db.col.find ()
6.2, update the document updated format
Db.collection.update (
<query>,
<update>,
{
upsert: <boolean>,
Multi: < Boolean>,
writeconcern: <document>
}
)
The
parameter description: query : The query criteria for update, similar to those in the SQL update query. update : The object of the update and some updated operators, such as $, $inc ... , and so on, can also be understood as upsert after set in SQL Update query: Optional, this argument means that if there is no record of update, insert objnew,true as INSERT, The default is false, not inserted. multi : Optional, mongodb default is False, only the first record found is updated, and if this argument is true, multiple records are updated as conditions are checked. writeconcern : Optionally, the level at which the exception is thrown. For example,