Professional Documents
Culture Documents
Now that we’ve defined MongoDB CRUD operations, we can take a look at
how to carry out the individual operations and manipulate documents in a
MongoDB database. Let’s go into the processes of creating, reading, updating,
and deleting documents, looking at each operation in turn.
Create Operations
MongoDB provides two different create operations that you can use to insert
documents into a collection:
● db.collection.insertOne()
● db.collection.insertMany()
insertOne()
As the namesake, insertOne() allows you to insert one document into the
collection. For this example, we’re going to work with a collection called
RecordsDB. We can insert a single entry into our collection by calling the
insertOne() method on RecordsDB. We then provide the information we want to
insert in the form of key-value pairs, establishing the schema.
db.RecordsDB.insertOne({
name: "Bruno",
age: "5 years",
species: "Dog",
ownerAddress: "Kullu, Himachal Pradesh",
chipped: true
})
> db.RecordsDB.insertOne({
... name: "Bruno",
... age: "5 years",
... species: "Dog",
... ownerAddress: "Kullu, Himachal Pradesh",
... chipped: true
... })
{
"acknowledged" : true,
"insertedId" :
ObjectId("8665c57d5e6b9ceb866b9ceb")
}
insertMany()
It’s possible to insert multiple items at one time by calling
the insertMany() method on the desired collection. In this case, we pass multiple
items into our chosen collection (RecordsDB) and separate them by commas.
Within the parentheses, we use brackets to indicate that we are passing in a list
of multiple entries. This is commonly referred to as a nested method.
db.RecordsDB.insertMany([{
name: "Bruno",
age: "5 years",
species: "Dog",
ownerAddress: "Kullu, Himachal Pradesh",
chipped: true},
{name: "Mitthu",
age: "3 years",
species: "Parrot",
ownerAddress: "Kullu, Himachal Pradesh",
chipped: true}])
handle a 404 response: app. use((req, res, next) => { res. status(404).
query the movies collection to
1. get all documents
6. get all movies released before the year 2000 or after 2010
MongoDB Practice
create database
year : 1999
actors : [
Brad Pitt
Edward Norton
year : 1994
actors : [
John Travolta
Uma Thurman
year : 2009
actors : [
Brad Pitt
Diane Kruger
Eli Roth
year : 2012
year : 2013
year : 2012
title : Avatar
db.movies.insert({title:"Avatar"})
db.movies.find()
db.movies.find({writer:"Quentin Tarantino"})
db.movies.find({actors:"Brad Pitt"})
db.movies.find({franchise:"The Hobbit"})
db.movies.find({year:{$gt:"1990", $lt:"2000"}})
get all movies released before the year 2000 or after 2010
db.movies.find({$or:[{year:{$gt:"2010"}},{year: {$lt:"2000"}}]})
Update Documents
db.movies.update({_id:ObjectId("6c6d8e9f3c5a2c89d8e6d9c1a5")}, {$set:
{synopsis:"A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain
with a spirited group of dwarves to reclaim their mountain home - and the gold
within it - from the dragon Smaug."}})
add a synopsis to "The Hobbit: The Desolation of Smaug" : "The dwarves, along
with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim
Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a
mysterious and magical ring."
db.movies.update({_id:ObjectId("6c6d8e9f3c5a2c89d8e6d9c1a6")}, {$set:
{synopsis:"The dwarves, along with Bilbo Baggins and Gandalf the Grey,
continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo
Baggins is in possession of a mysterious and magical ring."}})
db.movies.update({_id:ObjectId("6c6d8e9f3c5a2c89d8e6d9c1a5")}, {$push:
{actors:"Samuel L. Jackson"}})
Text Search
find all movies that have a synopsis that contains the word "Bilbo"
db.movies.find({synopsis:{$regex:"Bilbo"}})
find all movies that have a synopsis that contains the word "Gandalf"
db.movies.find({synopsis:{$regex:"Gandalf"}})
find all movies that have a synopsis that contains the word "Bilbo" and not the
word "Gandalf"
db.movies.find({$and:[{synopsis:{$regex:"Bilbo"}},
{synopsis:{$not:/Gandalf/}}]})
find all movies that have a synopsis that contains the word "dwarves" or
"hobbit"
db.movies.find({$or:[{synopsis:{$regex:"dwarves"}}, {synopsis:
{$regex:"hobbit"}}]})
find all movies that have a synopsis that contains the word "gold" and
"dragon"
db.movies.find({$and:[{synopsis:{$regex:"gold"}}, {synopsis:
{$regex:"dragon"}}]})
Delete Documents
db.movies.remove({_id:ObjectId("6c6d8e9f3c5a2c89d8e6d9c1a4")})
db.movies.remove({_id:ObjectId("6c6d8e9f3c5a2c89d8e6d9c1a5")})
Relationships
username : GoodGuyGreg
last_name : "Greg"
username : ScumbagSteve
full_name :
first : "Scumbag"
last : "Steve"
username : GoodGuyGreg
username : GoodGuyGreg
username : GoodGuyGreg
username : ScumbagSteve
body : Sells it
db.posts.insert({ username:"ScumbagSteve", title:"Borrows something",
body:"Sells it"})
username : ScumbagSteve
username : ScumbagSteve
username : GoodGuyGreg
post : [post_obj_id]
username : GoodGuyGreg
post : [post_obj_id]
where [post_obj_id] is the ObjectId of the posts document: "Borrows
everything"
username : GoodGuyGreg
post : [post_obj_id]
where [post_obj_id] is the ObjectId of the posts document: "Forks your repo
on github
username : ScumbagSteve
post : [post_obj_id]
username : ScumbagSteve
post : [post_obj_id]
db.users.find().pretty()
db.posts.find().pretty()
db.posts.find({username:"GoodGuyGreg"})
db.posts.find({username:"ScumbagSteve"})
db.comments.find().pretty()
db.comments.find({username:"GoodGuyGreg"})
db.comments.find({username:"ScumbagSteve"})
find all comments belonging to the post "Reports a bug in your code"
Show name and population of the cities where the population is over 10000
Show the name and population of the state based on the cities shown
Show the _id, city, name of the capital city of each state with a popultaion
greater than 20,000.