30
loading...
This website collects cookies to deliver better user experience
NoSQL
(not only SQL) database. In NoSQL databases, we store our data in a collection of documents. These documents are very similar to JavaScript objects
or JSON objects
. So if you are familiar with JavaScript, then you are going to like Mongo, because it will be easier to get the hang of it.MySQL
, where you have to create all your tables and all your columns, Mongo gives you the freedom to structure your data however we want through our application. This doesn't mean it's better than relational databases, it all depends on how you need to use your database.Homebrew
. If you don't have Homebrew installed, just run the code below in your terminal and that will install Homebrew./bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew tap mongodb/brew
brew install [email protected]
brew services start [email protected]
mongo
db
show dbs
use
followed by the database name.use db_name
use
command again, followed by your new DBs name.use new_db_name
show dbs
after creating a DB, the new DB won't show because it doesn't have anything in it, so don't worry when it doesn't show up after running the show dbs
.use
command, and after that, you need to type in the following command:db.dropDatabase()
db.createCollection('collection_name')
show
command followed by collections
and that should print out the collections in your DB.show collections
db
followed by the name of the collection that you want to insert the row in, and then by insert
, which is then followed by parentheses()
. And in those parentheses, you pass in an object with whatever you want.db.users.insert({
username: 'Batman_fan_44',
followers: 3,
following: [
'Alred',
'Robin'
],
status: {
name: 'Bruce Wayne',
status: 'active'
}
})
insert
after the collections name, type in insertMany
and then insert your rows in an array like this:db.users.insertMany([
{
username: 'Tony Montana',
followers: 1234
},
{
username: 'Hannibal Lecter',
followers: 5
},
{
username: 'Rick Grimes',
followers: 21
}
])
db.collection_name.find()
pretty()
, which formats all the rows.db.collection_name.find().pretty()