



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
abc - abc - abc - abc
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!
MongoDB is one of several database types to arise in the mid-2000s under the
NoSQL banner. Instead of using tables and rows as in relational databases,
MongoDB is built on an architecture of collections and documents. Documents
comprise sets of key-value pairs and are the basic unit of data in MongoDB.
MongoDB stores data in JSON documents (which we serialize to
BSON). JSON provides a rich data model that seamlessly maps to
native programming language types, and the dynamic schema
makes it easier to evolve your data model than with a system with
enforced schemas such as a RDBMS.
MongoDB provides a lot of the features of a traditional RDBMS
such as secondary indexes, dynamic queries, sorting, rich updates,
upserts (update if document exists, insert if it doesn’t), and easy
aggregation. This gives you the breadth of functionality that you
are used to from an RDBMS, with the flexibility and scaling
capability that the non-relational model allows.
By keeping related data together in documents, queries can be
much faster than in a relational database where related data is
separated into multiple tables and then needs to be joined later.
MongoDB also makes it easy to scale out your database.
Autosharding allows you to scale your cluster linearly by adding
more machines. It is possible to increase capacity without any
downtime, which is very important on the web when load can
increase suddenly and bringing down the website for extended
maintenance can cost your business large amounts of revenue.
MongoDB works hard to be very easy to install, configure,
maintain, and use. To this end, MongoDB provides few
configuration options, and instead tries to automatically do the
“right thing” whenever possible. This means that MongoDB works
right out of the box, and you can dive right into developing your
application, instead of spending a lot of time fine-tuning obscure
database configurations.
Why should use MongoDB?
CREATE TABLE users ( id MEDIUMINT NOT NULL AUTO_INCREMENT, user_id Varchar(30), age Number, status char(1), PRIMARY KEY (id) )
db.users.insert( { user_id: "abc123", age: 55, status: "A" } )
db.createCollection("users") ALTER TABLE users ADD join_date DATETIME
db.users.update( { }, { $set: { join_date: new Date() } }, { multi: true } ) ALTER TABLE users DROP COLUMN join_date
db.users.update( { }, { $unset: { join_date: "" } }, { multi: true } ) CREATE INDEX idx_user_id_asc ON users(user_id)
db.users.createIndex( { user_id: 1 } )
idx_user_id_asc_age_desc ON users(user_id, age DESC)
db.users.createIndex( { user_id: 1, age: -1 } )
DROP TABLE users db.users.drop()
Insert
INSERT INTO users(user_id, age,
db.users.insert( { user_id: "bcd001", age: 45, status: "A" }
status) VALUES ("bcd001", 45, "A")
Select
FROM users
db.users.find()
SELECT id, user_id, status FROM users
db.users.find( { }, { user_id: 1, status: 1 } ) SELECT user_id, status FROM users
db.users.find( { }, { user_id: 1, status: 1, _id: 0 } ) SELECT * FROM users WHERE status = "A"
db.users.find( { status: "A" } ) SELECT user_id, status FROM users WHERE status = "A"
db.users.find( { status: "A" }, { user_id: 1, status: 1, _id: 0 } ) SELECT * FROM users WHERE status != "A"
db.users.find( { status: { $ne: "A" } } ) SELECT * FROM users WHERE status = "A" AND age = 50
db.users.find( { status: "A", age: 50 } )
SELECT * FROM users WHERE status = "A" OR age = 50
db.users.find( { $or: [ { status: "A" } , { age: 50 } ] } )
SELECT * FROM users WHERE age > 25
db.users.find( { age: { $gt: 25 } } ) SELECT * FROM users WHERE age < 25
db.users.find( { age: { $lt: 25 } } ) SELECT * FROM users WHERE age > 25 AND age <= 50
db.users.find( { age: { $gt: 25, $lte: 50 } } )
FROM users WHERE user_id like "%bc
db.users.find( { user_id: /bc/ } )
UPDATE users SET age = age + 3 WHERE status = "A"
db.users.update( { status: "A" } , { $inc: { age: 3 } }, { multi: true } )
Delete Records
DELETE FROM users WHERE status = "D"
db.users.remove( { status: "D" } )
DELETE FROM users db.users.remove({})
BIGDATA