An Introduction to NoSQL & MongoJS
{ By: "Nirmal V",
Date: "22/11/2014",
Event: "Kerala Javascript User Group Meetup - Nov-2014" }
NoSQL
db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
db.mycol.save(
{
"_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point New Topic", "by":"Tutorials Point"
}
)
db.mycol.remove({'title':'MongoDB Overview'})
Update and Delete Documents
Key Concepts
- Database
- Collection
- Document
Text
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2011,1,20,2,15),
like: 0
},
{
user:'user2',
message: 'My second comments',
dateCreated: new Date(2011,1,25,7,45),
like: 5
}
]
}
Sample Document
MongoDB Data Modelling
{
_id: POST_ID
title: TITLE_OF_POST,
description: POST_DESCRIPTION,
by: POST_BY,
url: URL_OF_POST,
tags: [TAG1, TAG2, TAG3],
likes: TOTAL_LIKES,
comments: [
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
},
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
}
]
}
RDBMS Schema Design
MongoDB Schema Design
MongoDB datatypes
-
String : This is most commonly used datatype to store the data. String in mongodb must be UTF-8 valid.
-
Integer : This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server.
-
Boolean : This type is used to store a boolean (true/ false) value.
-
Double : This type is used to store floating point values.
-
Min/ Max keys : This type is used to compare a value against the lowest and highest BSON elements.
-
Arrays : This type is used to store arrays or list or multiple values into one key.
-
Timestamp : ctimestamp. This can be handy for recording when a document has been modified or added.
-
Object : This datatype is used for embedded documents.
-
Null : This type is used to store a Null value.
-
Symbol : This datatype is used identically to a string however, it's generally reserved for languages that use a specific symbol type.
-
Date : This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it.
-
Object ID : This datatype is used to store the document’s ID.
-
Binary data : This datatype is used to store binay data.
-
Code : This datatype is used to store javascript code into document.
-
Regular expression : This datatype is used to store regular expression
MongoDB datatypes
db.mycol.insert({
_id: ObjectId(7df78ad8902c),
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: 'NoSQL database doesn't have tables',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])
Insert Document
Update document and save method
db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
db.mycol.save(
{
"_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point New Topic", "by":"Tutorials Point"
}
)
Delete and truncate document
db.mycol.remove({'title':'MongoDB Overview'})
db.mycol.remove()
>db.mycol.find({"by":"tutorials point","title": "MongoDB Overview"}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials point",
"url": "http://www.tutorialspoint.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
Query Document
>db.mycol.find({$or:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials point",
"url": "http://www.tutorialspoint.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
AND in MongoDB
OR in MongoDB
MongoDB: Limiting and Sorting Records
>db.mycol.find({},{"title":1,_id:0}).limit(2)
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
>
>db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1)
{"title":"NoSQL Overview"}
>
>db.mycol.find({},{"title":1,_id:0}).sort({"title":-1})
{"title":"Tutorials Point Overview"}
{"title":"NoSQL Overview"}
{"title":"MongoDB Overview"}
>
MongoDB: Indexing
db.mycol.ensureIndex({"title":1,"description":-1})
Introducing MongoJS
Installing MongoJS
$ npm install mongojs
// app.js
var databaseUrl = "mydb"; // "username:password@example.com/mydb"
var collections = ["users", "reports"]
var db = require("mongojs").connect(databaseUrl, collections);
Connect to MongoDB Server
Finding documents
// app.js
db.users.find({sex: "female"}, function(err, users) {
if( err || !users) console.log("No female users found");
else users.forEach( function(femaleUser) {
console.log(femaleUser);
} );
});
Save a document
// app.js
db.users.update({email: "srirangan@gmail.com"}, {$set: {password: "iReallyLoveMongo"}}, function(err, updated) {
if( err || !updated ) console.log("User not updated");
else console.log("User updated");
});
Introducing MongoJS
Update a document
// app.js
db.users.save({email: "srirangan@gmail.com", password: "iLoveMongo", sex: "male"}, function(err, saved) {
if( err || !saved ) console.log("User not saved");
else console.log("User saved");
});
MongoJS vs MongooseJs
MongoJS Vs MongooseJS
Questions?
nirmal@ndimensionz.com
nirmalkamath@gmail.com
Thank you :-)
@nirmalkamath
mongojs
By phpnirmal
mongojs
- 2,164