MongoDB is a NoSQL database that stores data in flexible JSON like documents. Unlike relational databases, MongoDB does not require a fixed schema. This makes it very convenient for applications where structure may evolve over time.
First install MongoDB. On Ubuntu you typically add the official MongoDB repository and then install:
To show databases:
To insert a document:
If you have not explored NoSQL databases yet, MongoDB is a good place to begin. Happy exploring!
First install MongoDB. On Ubuntu you typically add the official MongoDB repository and then install:
sudo apt install mongodbOnce installed, check service status:
sudo systemctl status mongodbIf it is not running, start it:
sudo systemctl start mongodbTo access MongoDB shell:
mongoYou should now be inside the MongoDB interactive shell.
To show databases:
show dbsTo create or switch to a database:
use mydbMongoDB creates the database automatically when you first store data.
To insert a document:
db.users.insertOne({ name: "John", email: "[john@example.com](mailto:john@example.com)", age: 30 })
To find documents:
db.users.find()To pretty print results:
db.users.find().pretty()To update a document:
db.users.updateOne(
{ name: "John" },
{ $set: { age: 31 } }
)
To delete a document:
db.users.deleteOne({ name: "John" })
To create an index:
db.users.createIndex({ email: 1 })
To exit the shell:
exitMongoDB is simple to start with and powerful at scale. Its document model makes it easy to work with application data directly without complex joins.
If you have not explored NoSQL databases yet, MongoDB is a good place to begin. Happy exploring!
Disclaimer: If you follow the information here, there is no warranty, I am not liable if it deletes your data, gets you hacked, burns your house down or anything else. If you follow the information contained here you do so entirely at your own risk. My views and opinions are my own and not necessarily represent the views of my employer. © Raheel Hameed and www.raheelhameed.com, 2017. Unauthorized use and/or duplication of this material without express and written permission from this site’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to author and this website with appropriate and specific direction to the original content.
Disclaimer: If you follow the information here, there is no warranty, I am not liable if it deletes your data, gets you hacked, burns your house down or anything else. If you follow the information contained here you do so entirely at your own risk. My views and opinions are my own and not necessarily represent the views of my current or former employers. © Raheel Hameed and www.raheelhameed.com, 2017. Unauthorized use and/or duplication of this material without express and written permission from this site’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to author and this website with appropriate and specific direction to the original content.
Comments
Post a Comment