Hi All,
Today I will show you basic usages of mongodb. First we start with opening mongo console using this command:
$ mongo
If your server have started correctly there's no error show in terminal/console and you will enter mongodb console like this:
MongoDB shell version: 3.2.11
connecting to: test
Server has startup warnings:
2016-12-05T07:17:23.208+0700 I CONTROL [initandlisten]
2016-12-05T07:17:23.208+0700 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>
1. Show Existing Databases
Use below command to show existing databases in your mongodb server.
> show dbs
There's should be showed a listing of existing databases like this:
> show dbs
djamblog 0.078GB
local 0.078GB
zips 0.078GB
>
2. Insert data to collections
To start and create new database, type this command:
> use exampledb
Now you can create and insert collections using this command:
> db.city.insert({citiName:"Jakarta",provName:"DKI Jakarta"})
If insert successful, you can see message below insert command like this:
WriteResult({ "nInserted" : 1 })
Let me explain a little. There are 3 words of insert command separated by dot, they are:
- db: must included in every query
- city: collections name
- insert: query command/syntax
3. Check Collections and Data
To check newly created collections, just type this command:
> show collections
It will show collections, include system collections like this:
city
system.indexes
Query for show data in collections is like this:
> db.city.find()
It will return some document in collections like this:
{ "_id" : ObjectId("584a13d5b65761be678d4dd4"), "citiName" : "Jakarta", "provName" : "DKI Jakarta" }
As you see, result of query returns a JSON formatted data and that is one of mongodb feature is using json like document.
That its for now.
Thanks