MongoDB is a NoSQL document database, that stores JSON documents. Using NoSQL speeds up application development and reduces complexity.
MongoDB is also used to store binary data, e.g., images.
The mongo command line provides a JavaScript shell, and an API for database operations– save(), find(), remove(), count(), … .
MongoDB as a NoSQL DB
- MongoDB stores its data using BSON (binary JSON) format
- MongoDB is also used to store binary data, e.g., images.
Important Terms
Database: a set of collections.
Collections: Collections in Mongo are equivalent to tables in relational databases. They can hold multiple JSON documents.
Documents: A document is a JSON object, and is equivalent to a record or row in an SQL table.
Schema: While Mongo is “schema-less”, Mongoose is a Node.js module that provides a schema and data modeling tool for MongoDB, and a higher-level API for storing JSON documents in MongoDB.
How Data is Stored in Mongo vs. SQL Database
1. Start mongod — the MongoDB Server
To start the MongoDB server (aka daemon, hence mongod):
Windows
- Open a Windows Command Prompt window.
- Start the server:
“C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe” –dbpath=c:\data\db
- To stop the server: type
ctrl-c
Remember to do this when you are done using the server. Leaving it running could drain your battery.
MacOS
- Open a second terminal app window.
- Start the server:
mongod --config /usr/local/etc/mongod.conf
- To stop the server: type
ctrl-c
Remember to do this when you are done using the server. Leaving it running could drain your battery.
2. Start mongo — the Mongo Shell
The mongo CLI provides a JavaScript shell, and an API for database operations.
- Windows: Open a second Windows Command Prompt window.
Mac: open a second terminal window (or tab).- Start the Mongo shell with this command: mongo
- Quit the Mongo shell with this command: exit
3. Interacting with the MongoDB Command-Line Client
See Ch. 7, p. 223ff, in our textbook, Learning Web Application Development.
//List all databases: > show dbs local 0.03125GB //Display the database you are in (test is the default db):
> db test //Switch to a new db: > use myDB switched to db myDB //creates myDB, if it does not exist > use test switched to db test //List all collections in current database > db.getCollectionNames() ["cards", "names"] //Examples from our textbook
> let card = { "rank":"ace", "suit":"clubs" }; > card { "rank" : “ace”, “suit” : “clubs” } > let clubs = []; > ["two", "three", "four", "five"].forEach(function (rank){clubs.push( { "rank":rank, "suit":"clubs" } ) });
//Create new cards collection and add documents: > db.cards.insert(clubs); //Retrieve all documents: > db.cards.find(); //Retrieve a single document: > db.cards.find({rank:"four"}) //Add a new document: > db.cards.insert({rank:"one", suit:"clubs"}); > db.cards.find() //Update a document: > db.cards.update({rank:"ace"}, {rank:"ace", suit:"clubs"}); //db.collection.save(): Can both insert a new document, or update an existing document. > let aceOfHearts = {rank:"ace", suit:"hearts"} > db.cards.save(aceOfHearts) > db.cards.find() //note values of _id field > db.cards.save({"_id":ObjectId("5ec43b4eacd017d2f13d6332"), rank:"one", "suit":"hearts"}) > db.cards.find() //document updated //Delete a document: > db.cardsColl.remove({rank:"four"});
Misc Notes
Save vs Insert in MongoDB
- If you use “insert” with an ID that was previously used in the same collection you will get a duplicate key error.
- If you use “save” with an ID that is already in the same collection, it will get updated/overwritten.
To drop a Database
use fubar;
db.dropDatabase();
To drop a Collection
db.fubaz.drop()