Mongo
Mongo from "humongous" (very big).
Motivation
- High performance read and write
- Flexible schema
- Need replication
- Start small and fast and grow easy
- High availability
- Strong data consistency
- mongoDB scales horizontally (Vertical scale: Get a better server, Horizontal scale: Get more servers)
- Replication is easy and good
Install and Start the database
Download and unpack or install it to a folder. You also need a folder where mongodb may store the data, in this example we created a folder at p:mongo_test_db
This needs to be running the whole time.
Now start in a different window a client
And try the Getting Started Tutorial.
Connect to differnt computer
Command to connect to a single database in Mongo
mongos> use mydb
switched to db mydb
mongos> show collections
GUI
mongoDB Commands
Create to documents (in SQL: insert new rows)
> k = { x : 3 }
and store them into a collection (in SQL: store them into a table)
WriteResult({ "nInserted" : 1 })
WriteResult({ "nInserted" : 1 })
What collections do we have
testData
Query
Show content of collection
{ "_id" : ObjectId("54bd1995fe284fb914531bbc"), "name" : "mongo" }
{ "_id" : ObjectId("54bd199afe284fb914531bbd"), "x" : 3 }
Show documents where x=3
{ "_id" : ObjectId("54bd199afe284fb914531bbd"), "x" : 3 }
You can also interate over the result with the a cursor
> while ( c.hasNext() ) printjson( c.next() )
See also Iterate the Returned Cursor
Jump directly to a result at a given position
> printjson( c [ 2 ] )
{ "_id" : ObjectId("54bd2adefe284fb914531bbe"), "x" : 10 }
> printjson( c [ 99 ] )
undefined
Maybe you want to sort the data
Find exactly one document
Regular expressions (like search)
Find in sub fields
Find all rows where a field exists
Output only some fields This will only print the name field found within the product field. It will even exclude the id which is normally always included.
JavaScript in Mongo
Use a JavaScript For Loop to insert data, e.g. for tests
db.testData.insert( { x : i } )
}
> db.testData.find()
You can store JavaScript functions in your .mongorc.js file to have it started when you start the mongoDB shell.
Export / Import
mongoimport -h bar.example.com --db myDb --collection myCollection --file /tmp/myexport.json
Links
Referencing
Like in relational databases you can reference in one attribute another entry
"_id": 42,
"name": "John Doe",
"address_id": 37
}
a={
"_id": 37,
"city": "New York",
"country": "USA",
"zip-code": "1234",
}
> db.testData.insert( a )
> db.testData.insert( c )
But mongoDB does not provide joins (you have to resolve them yourself) and this very common strategy in the relational world is often discouraged here.
Embedding
Consider to avoid referencing but use embedding. This will improve the performance but has the disadvantage that you might have to change the same information in different places. E.g. when the zipcode changes you have to change in all the affected Persons instead of only fixing one Address entry. An embedded version might look like this
"_id": 42,
"name": "John Doe",
"address": {
"_id": 37,
"city": "New York",
"country": "USA",
"zip-code": "1234",
}
}
One to Many
One person may have many phone numbers. You may use arrays for this
"_id": 42,
"name": "John Doe",
"phone": [
{
"type": "work",
"number": "12345"
},
{
"type": "home",
"number": "2468"
}
]
}
Many-To-Many
Add an Array at least to one side and reference the others.