Today I'll show you what I lerned at Mongo University online course for C# developers after first lesson and passing all homework.
I'll show you how you can:
- install MongoDB on Windows workstation
- switch between databases
- display all elements from a collection
- add new document to a collection
- restore Mongo database from a dump folder.
Get MongoDB installer from MongoDB download center:
https://www.mongodb.com/download-center
After installation of 64 bit version go to C:\Program Files\MongoDB\Server\3.4\bin.
You should see there 2 files:
-mongo - this is a console application, you can think of it as a client application
-mongod - this is a server of database
At this point it's useful to add 'C:\Program Files\MongoDB\Server\3.4\bin' to PATH environment variable.
Now open the command line and go to C drive.
Create the folder by using command:
mkdir data\db
And now run the server:
mondog
Important: If you forget adding the data\db folder then server will stop just after starting.
It's very important directory. Mongo needs it to be created because this is where Mongo stores its files including your collections.
It's important to note that data are stored by MongoDB in BSON(binary JSON) format.
Now as mongo server is running, we can run this from different window:
db.users.insert({"name" : "Dariusz Kacban" })
db.users.insert({"name" : "Someone Else" })
We simply add 2 JSON objects to "users" collection. In our example all values are strings.
Fortunately JSON allow for exactly 4 data types: string, number, array or JSON object itself.
And to confirm that 2 rows were added to the 'users' collection run this command:
db.users.find()
Useful commands after running mongo client application are available when you type in help.
The are commands like:
show dbs - show all databases
use test - use specific database
db.users.insertOne({JSON OBJECT HERE}) - if test db doesn't exist - mongo wil create it for us.
db.users.find().pretty() - display entire 'users' collection
db.users.find({"name":"Dariusz"}).pretty() - display only users with name "Dariusz"
It's worth to mention than 16MB is maximum size od a document in Mongo. It must be considered when you make a decision if to embed data in a collection or create a separate collection. This is MongoDB requirement.
Another useful function od MongoDB is: mongoestore. It helps you to restore data from dump file that you've previously created.
Example use: mongorestore dump_directory
Then you need to switch to database like this:
use database_name
then you call collections like this:
db.colelction_name.find()
One important note: expressing string in JSON demands that you use " instead of '.
That's all for today. I hope you like to learn Mongo and want to learn how to design a database in Mongo and how Mongo work in comparison to relational database like MySQL or Oracle.
If you're interested find the course and join the next edition: https://university.mongodb.com/
I'll show you how you can:
- install MongoDB on Windows workstation
- switch between databases
- display all elements from a collection
- add new document to a collection
- restore Mongo database from a dump folder.
Get MongoDB installer from MongoDB download center:
https://www.mongodb.com/download-center
After installation of 64 bit version go to C:\Program Files\MongoDB\Server\3.4\bin.
You should see there 2 files:
-mongo - this is a console application, you can think of it as a client application
-mongod - this is a server of database
At this point it's useful to add 'C:\Program Files\MongoDB\Server\3.4\bin' to PATH environment variable.
Now open the command line and go to C drive.
Create the folder by using command:
mkdir data\db
And now run the server:
mondog
Important: If you forget adding the data\db folder then server will stop just after starting.
It's very important directory. Mongo needs it to be created because this is where Mongo stores its files including your collections.
It's important to note that data are stored by MongoDB in BSON(binary JSON) format.
Now as mongo server is running, we can run this from different window:
db.users.insert({"name" : "Dariusz Kacban" })
db.users.insert({"name" : "Someone Else" })
We simply add 2 JSON objects to "users" collection. In our example all values are strings.
Fortunately JSON allow for exactly 4 data types: string, number, array or JSON object itself.
And to confirm that 2 rows were added to the 'users' collection run this command:
db.users.find()
Useful commands after running mongo client application are available when you type in help.
The are commands like:
show dbs - show all databases
use test - use specific database
db.users.insertOne({JSON OBJECT HERE}) - if test db doesn't exist - mongo wil create it for us.
db.users.find().pretty() - display entire 'users' collection
db.users.find({"name":"Dariusz"}).pretty() - display only users with name "Dariusz"
It's worth to mention than 16MB is maximum size od a document in Mongo. It must be considered when you make a decision if to embed data in a collection or create a separate collection. This is MongoDB requirement.
Another useful function od MongoDB is: mongoestore. It helps you to restore data from dump file that you've previously created.
Example use: mongorestore dump_directory
Then you need to switch to database like this:
use database_name
then you call collections like this:
db.colelction_name.find()
One important note: expressing string in JSON demands that you use " instead of '.
That's all for today. I hope you like to learn Mongo and want to learn how to design a database in Mongo and how Mongo work in comparison to relational database like MySQL or Oracle.
If you're interested find the course and join the next edition: https://university.mongodb.com/
Komentarze
Prześlij komentarz