Basic MongoDB Usage
Differences between Oracle and MongoDB
MongoDB is a database commonly called NoSQL. It stores data called documents, which are similar to JSON, in groups called collections. Using Excel and Oracle as familiar relational database examples, the corresponding names in MongoDB are as follows.
| Excel | Oracle | MongoDB |
|---|---|---|
| Book | Schema | Database |
| Sheet | Table | Collection |
| Row | Row | Document |
| Column | Column | Field |
| Shell | Field | Value |
Server
The server command is mongod. This section explains the commands for starting and stopping the server with mongod. For mongod options not covered here, see “How to use the MongoDB mongod command.”
Starting and stopping the server
- Start the server
- Assume that the location for storing database files is prepared at
C:\mongodb\server\data. - Start Command Prompt and run the following command to start the MongoDB server.
mongod --dbpath "C:\mongodb\server\data"
- Assume that the location for storing database files is prepared at
- Stop the server
- Press
Ctrl+Cin the running Command Prompt. A shutdown command is issued and MongoDB exits.
- Press
Client
After starting MongoDB in the previous step, connect with the client command mongo. If you are connecting from the same server as the MongoDB server, start a separate Command Prompt from the one used to start the server. For other mongo options not covered here, see “How to use the MongoDB mongo command.”
Connecting to and disconnecting from the server
- Connect to the server
If you are on the same machine where MongoDB is running, start Command Prompt and execute the command above to connect. If the server is on another host, specify the target with the
mongo--host <hostname>option. - Disconnect from the server
To disconnect from MongoDB, pressCtrl+C. You can also disconnect by running the following command.> exit
Creating, listing, changing, and deleting databases
-
Create a database
> use sample_databaseConnects to the specified database. If the database does not exist, it is created.
-
List databases
> show dbsBy default, the three databases
admin,local, andtestexist.A database created immediately after running
use <database name>is not shown in the list. It must contain at least one record before it appears inshow dbs. -
Rename a database
> db.copyDatabase('old_database', 'new_database') > use old_database > db.dropDatabase()Unfortunately, there is no command to rename a database, so you must copy the old database to a new database and then delete the old one.
-
Delete a database
> use sample_database > db.dropDatabase();Connect to the database to delete, then run
db.dropDatabase().
Creating, listing, renaming, and deleting collections
-
Create a collection
> db.createCollection(products);Creates a collection with the name specified in the argument.
-
List collections
> show collectionsDisplays the list of collections in the currently connected database.
-
Rename a collection
> db.old_collection_name.renameCollection(new_collection_name, true);Renames
old_collection_nametonew_collection_name. If you want to drop an existing collection namedold_collection_nameduring the rename, specifytrueas the second argument. -
Delete a collection
> db.products.drop();Deletes the specified collection.
Adding, querying, updating, and deleting documents
-
Add a document
> db.products.insert( {name: 'sample book', price: 1280} )Adds a document to the specified collection. When
insertis executed, the_idfield is added automatically. -
Query documents
> db.products.find( {name: {$eq: 'sample book'}} )The basic format of a query argument is
{ field: { operator: value }}. Operators are documented in MongoDB - Query and Projection Operators. If no argument is specified, all data is returned. -
Update documents
> db.products.update( {name: {$eq: 'sample book'}}, {$set: {name: 'Hack MongoDB'}}, {upsert: false, multi: true} )Updates documents that match the condition specified in the first argument with the document content specified in the second argument. In the third argument object,
upsertspecifies whether to insert a document when no matching document is found, andmultispecifies whether all matching documents should be updated. -
Delete documents
> db.products.remove( {name: {$eq: 'sample book'}} )Deletes all documents that match the query specified in the first argument.
References
- MongoDB - copydb
- MongoDB - db.dropDatabase()
- MongoDB - db.createCollection()
- MongoDB - db.collection.insert()
- MongoDB - db.collection.find()
- MongoDB - db.collection.update()
- MongoDB - db.collection.remove()