2 - Import - Export - Querying Data
2 - Import - Export - Querying Data
Software Engineering
2
1. How Does MongoDB Store Data?
..
.. { }
..
Presentation Correct syntax
How are What is the
documents correct syntax for
presented in documents?
memory?
3
2. JSON (Javascript Standard Object Notation)
• When you view or update documents in MongoDB Shell →you are
working with JSON
• JSON format:
• Start and end with { }
• Separate each key and value with colon :
• Separate each key:value pair with comma ,
• Key must be surrounded by quotation mark “”
• In MongoDB “keys” also called “fields”.
4
JSON Values
Numbers: no quotes.
String: in double quotes.
Boolean: true, false.
Name Value
Nested JSON object.
Array. {
Null. "id": 14,
"firstName": "Mario",
"lastName": "Rossi",
"active": true
}
5
Nested JSON Objects
JSON Arrays
6
Pros of JSON Cons of JSON
Friendly Text–based
Readable Space-Consuming
7
3. BSON (Binary JSON)
• Bridges the gap between binary representation and JSON format
• A binary representation to store data in JSON format
• Optimized for:
• Speed
• Space
• Flexibility
• To achieve high performance
8
JSON BSON
Encoding Encoding
UTF-8 String Binary
Readability Readability
Human and Machine Machine only
9
Summary
{ }
10
4. Import/Export Data
11
Interacting with Atlas Cluster
{
Export to a
different system
} Import from a
local machine
12
Interacting with Atlas Cluster
Data is stored in BSON but viewed in JSON → Which format we’re going to use? (export)
JSON BSON
mongoexport mongodump
mongoimport mongorestore
13
mongoimport --help
<options>:
-d database to use,
-c collection to use,
-u username for authentication,
-p password for authentication,
-o output directory,
--host mongodb host to connect,
--port server port (can also use –h hostname:port),
…
14
Import (JSON)
Syntax: mongoimport <options> <file to import>
1. From localhost:
Mongoimport --drop -d backup -c restaurants res.json
Mongoimport -h localhost:27017 --drop -d backup -c restaurants res.json
import the JSON data form the res.json file into the collection restaurants in the backup db to a local mongod instance
running on port 27017.
--drop: before restoring the collections from the dumped backup, drop the collections from the target database. does
not drop collections that are not in the backup.
2. From Atlas:
mongoimport --uri mongodb+srv://mongobasic:[email protected]/backup --drop -c
restaurants res.json
15
Export (JSON)
Syntax: mongoexport <options>
1. From localhost:
mongoexport -d dbtest -c restaurants -o res.json
export the restaurants collection of dbtest db to the res.json output file from a local MongoDB instance running on
port 27017.
2. From Atlas:
mongoexport --uri
mongodb+srv://mongobasic:[email protected]/sample_restaurants -c restaurants
-o res.json
connect to a MongoDB Atlas Cluster and export the restaurants collection of sample_restaurant db to res.json output
file.
16
Import (BSON)
Syntax: mongorestore <options> <directory or file to restore>
1. From localhost:
mongorestore -d backup -c restaurants backup/dbtest/restaurants.bson
restores the collection named restaurants in the database backup from the corresponding files located in the
backup/dbtest/restaurants.bson
2. From Atlas:
mongorestore --uri mongodb+srv://mongobasic:[email protected] -d backup -c
restaurants backup/dbtest/restaurants.bson
17
Export (BSON)
Syntax: mongodump <options>
1. From localhost:
mongodump -d dbtest -c restaurants -o backup
connect to a local MongoDB instance running on port 27017 and use the default settings to export the content (no
parameters with all databases and collections)
2. From Atlas:
mongodump –uri
mongodb+srv://mongobasic:[email protected]/sample_restaurants -c restaurants
–o backup
creates a dump file that contains only the collection named restaurants of sample_restaurants database
18
5- Data Explorer
19
Data Interaction using the Atlas UI
20
Data Interaction using the Atlas UI
21
Data Interaction using the Atlas UI
22
Data Interaction using the Atlas UI
Cop
Edit Delete
Clone
y
23
Data Interaction using the Atlas UI
24
Data Interaction using the Atlas UI
25
Data Interaction using the Atlas UI
26
Data Interaction using the Atlas UI
27
6- MongoDB Shell (mongosh)
Query Documents
28
MongoDB Shell (mongosh)
- A fully functional JavaScript and Node.js (support editing and running scripts).
- You can use the MongoDB Shell to queries and operations directly with your
database.
Connect to a Deployment:
- Connect to local MongoDB instance on default port:
▪ mongosh
- Connect to local MongoDB instance on non default port:
▪ mongosh --port 28015 or mongosh --host localhost:28015
- Connect mongoDB instance on a remote host:
▪ mongosh “mongodb://mongodb0.example.com:28015”
- Connecting to Atlas:
▪ mongosh "mongodb+srv://cluster0.msr5i.mongodb.net/myFirstDatabase" -u mongobasic
29
Mongosh Usage
• List the databases available to the user:
• show dbs
• Switch/Create databases:
• use db_name
• List of all collections for current database:
• show collections
30
Find Method
• find() selects documents in a collection or view and returns a cursor to the selected
documents
Syntax: db.collectionname.find(query, projection)
31
Find Examples
• The examples in this section use documents from the bios collection where the
documents generally have the form:
{
“_id” : value,
“name” : { “first” : string, “last” : string }, // embedded document
“birth” : ISODate,
“death” : ISODate,
“contribs” : [ string, ... ], // Array of Strings
“awards” : [
{ “award” : string, “year”: number, “by”: string } // Array of embedded
documents
...
]
}
32
Find Examples
• Find all documents in a collection:
• db.bios.find()
• Find all documents in the bios collection where _id equals 5:
• db.bios.find( { _id: 5 } )
• Find all documents in the bios collection where the field last in the name embedded document equals "Hopper":
33
Find Examples
• To return all documents in the bios collection where the embedded document name contains a field first with
the value "Yukihiro" and a field last with the value "Matsumoto“:
• To returns documents in the bios collection where the array field contains the element "UNIX“:
• db.bios.find( { “contribs” : "UNIX" } )
34
Find Examples
• To query for all documents where the field tags value is an array with exactly two elements, “A" and “B", in the
specified order:
• db.inventory.find( { tags: [“A", “B"] } ) //match an array
• To find an array that contains both the elements “A" and “B", without regard to order or other elements in the
array, use the $all operator
• db.inventory.find( { tags: { $all: [“A", “B”] } } )
• To returns documents in the bios collection where the awards array contains an element with award field
equals "Turing Award":
• db.bios.find({ "awards.award": "Turing Award" })
35
Query Operators
Name Syntax Description and Example
$eq { field: { $eq: value } } db.inventory.find( { qty: { $eq: 20 } } )
Matches values that are equal to a specified value.
$gt {field: {$gt: value} } db.inventory.find( { qty: { $gt: 20 } } )
Matches values that are greater than a specified value.
$gte {field: {$gte: value} } db.inventory.find( { qty: { $gte: 20 } } )
Matches values that are greater than or equal to a
specified value.
$in {field: { $in: [value1, value2, ... valueN ] } } db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
db.bios.find( { contribs: { $in: [ "ALGOL", "Lisp" ] } } )
Matches any of the values specified in an array.
$lt {field: {$lt: value} } db.inventory.find( { qty: { $lt: 20 } } )
Matches values that are less than a specified value.
$lte {field: {$lte: value} } db.inventory.find( { qty: { $lte: 20 } } )
Matches values that are less than or equal to a
specified value.
36
Query Operators
Name Syntax Description and Example
$ne {field: { $ne: value } } db.inventory.find( { qty: { $ne: 20 } } )
Matches all values that are not equal to a specified value.
$nin {field: { $nin: [value1, value2, ... valueN db.inventory.find( { qty: { $nin: [ 5, 15 ] } } )
]}} Matches none of the values specified in an array.
$and { $and: [ { exp1 }, { exp2 }, ..., { expN } ] } db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] }
)
implicit AND operation
- db.inventory.find( { price: { $ne: 1.99, $exists: true } } )
Joins query clauses with a logical AND returns all documents
that match the conditions of both clauses.
$or { $or: [ { exp1 }, { exp2 } , ... , { expN } ] } db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] }
)
Joins query clauses with a logical OR returns all documents
that match the conditions of either clause.
37
Query Operators
Name Syntax Description and Example
$not { field: { $not: { operator-expression } } } db.inventory.find( { price: { $not: { $gt: 1.99 } } } )
Inverts the effect of a query expression and returns
documents that do not match the query expression.
$nor { $nor: [ { exp1 }, { exp2 }, ... { expN } ] } db.inventory.find( { $nor: [ { price: 1.99 }, { qty: 2 } ] } )
Joins query clauses with a logical NOR returns all
documents that fail to match both clauses.
$exists { field: { $exists: <boolean> } } db.inventory.find( { qty: { $exists: true, $nin: [ 5, 15 ] } }
)
When <boolean> is true, $exists matches the
documents that contain the field, including documents
where the field value is null
38
Query Operators
Name Syntax Description and Example
$regex { field: { $regex: /pattern/<options> } } - matche all documents where the sku field is like
or "%789":
{ field: { $regex: /pattern/, $options: '<options>' } db.inventory.find( { sku: { $regex: /789$/ } } )
}
or - use the i option perform a case-insensitive match
{ field: { $regex: 'pattern', $options: '<options>' } for documents with sku value that starts with ABC
} db.inventory.find( { sku: { $regex: /^ABC/i } } )
39
Query Operators
Name Syntax Description and Example
$all { field: { $all: [ value1, value2 ... ] } - To find an array that contains both the elements “A” and “B”,
} without regard to order or other elements in the array:
db.inventory.find( { tags: { $all: [“A”, “B”] } } )
$size { field: { $size: number } } db.inventory.find( { tags: { $size: 3 } } )
Selects documents if the array field is a specified size.
40
Query Operators
Name Syntax Description and Example
$elemMatch { field: { $elemMatch: {exp1, exp2, ..., - . The following find() operation queries for all
expN } } documents where the value of the zipcode field
is 63109. The $elemMatch projection returns only the
first matching element of the students array where the
school field has a value of 102:
db.schools.find( { zipcode: “63109” }, { students: { $elemMatch: {
school: 102 } } } )
41
count()
• Counts the number of documents referenced by a cursor. Append the count() method to a find()
query to return the number of matching documents.
• Example:
• db.restaurants.find({ ‘address.zipcode’: ‘11369’}).count() // the result is 5
or
• db. restaurants.count({‘address.zipcode’: ‘11369’}) // the result is 5
limit()
- Use limit() to maximize performance and prevent MongoDB from returning more results than
required for processing.
- Example:
▪ db.restaurants.find({ ‘address.zipcode’: ‘11369’}).limit(3) // the result is 3
42
skip()
• The skip() method controls the starting point of the results set.
• Example:
• db.restaurants.find({ ‘address.zipcode’: ‘11369’}).skip(1) // the result is 4
sort()
- The sort() method orders the documents in the result set
- Example:
▪ db.restaurants.find({ ‘address.zipcode’: ‘11369’}).sort({name: 1})
▪ db.restaurants.find({ ‘address.zipcode’: ‘11369’}, {name: 1}).sort({name: 1})
projection
1: ascending
-1: descending
43
Projection
• The projection parameter determines which fields are returned in the matching documents. The
projection parameter takes a document of the following form:
{ field1: value, field2: value, ... }
field: <1 or true> Specifies the inclusion of a field. Non-zero integers are also treated as true.
field: <0 or false> Specifies the exclusion of a field.
You cannot mix zeros and ones in a single projection. you can mix ones and zeros is when you're
specifically asking to exclude the _id field
• Example:
• db.restaurants.find({ ‘address.zipcode’: ‘11369’}, {name: 1})
• db.restaurants.find({ ‘address.zipcode’: ‘11369’}, {_id: 0, _id: 1, name: 1})
• db.restaurants.find({ ‘address.zipcode’: ‘11369’}, {_id: 1, _id: 0, name: 1})
• db.restaurants.find({ ‘address.zipcode’: ‘11369’}, {name: 1, name: 0})
44
Question?
45