Basic commands
Show all available databases:
show dbs;
Select a particular database to access, e.g. mydb. This will create mydb if it does not already exist:
use mydb;
Show all collections in the database (be sure to select one first, see above):
show collections;
Show all functions that can be used with the database:
db.mydb.help();
To check your currently selected database, use the command db
> db
mydb
db.dropDatabase() command is used to drop a existing database.
db.dropDatabase()
Hello World
After installation process, the following lines should be entered in mongo shell (client terminal).
> db.world.insert({ "speech" : "Hello World!" });
>cur=db.world.find();x=cur.next();print(x["speech"]);
Hello World!
- In the first line, we have inserted a { key : value } paired document in the default database test and in the collection named world.
- In the second line we retrieve the data we have just inserted. The retrieved data is kept in a javascript variable named cur. Then by the next() function, we retrieved the first and only document and kept it in another js variable named x. Then printed the value of the document providing the key.
Social Plugin