5. Mongo Is Awesome
Data Store for
JSON Objects
{
“Name” : “Rose Tyler”
}
Friday, March 1, 13
6. JSON Objects
A JSON Object is a
collection of key/
value pairs {
"name" : "Rose Tyler",
Keys are simple "race" : "Human",
strings "body parts" : [ "head", "legs"]
}
Values can be:
Numbers, Strings,
Arrays, Other
Objects, and more
Friday, March 1, 13
17. Inserting Data
Use the command insert or save to insert a new object
db.collection.insert( obj );
db.collection.insert( array );
Friday, March 1, 13
18. Inserting Data
Inserting to a new collection creates the collection
Inserting an object with an _id key, it is used as the
object’s id (and must be unique).
Friday, March 1, 13
20. Reading Data
find and findOne perform read operations
Both take a query
find returns a cursor
findOne returns an object Optional: Fields to
fetch
db.collection.find( <query>, <projection> )
Friday, March 1, 13
21. Query Document
An empty (or missing) query document returns
everything
db.collection.find({})
db.collection.find()
Friday, March 1, 13
22. Query Document
Each key/value pair in the query document imposes a
condition on the results (objects that match).
db.movies.find({ “genre” : “indie” });
db.books.find({“pages” : { “$gt” : 100 }});
Friday, March 1, 13
23. Query Document
Query Object
Each key/value pair in the query document imposes a
condition on the results (objects that match).
db.movies.find({ “genre” : “indie” });
db.books.find({“pages” : { “$gt” : 100 }});
Friday, March 1, 13
24. Query Document
A compound query means a logical AND on the
conditions.
db.inventory.find(
{
“type” : “snacks”,
“available” : { “$lt” : 10 }
});
Friday, March 1, 13
25. Quiz: What Is Returned
from alterego publisher
Bruce
{ Earth DC
Wayne
“publisher” :
“DC”
} Peter
Earth Marvel
Parker
Krypton Clark Kent DC
Friday, March 1, 13
26. Quiz: What Is Returned
from alterego publisher
{ Bruce
“publisher” : Earth DC
Wayne
“DC”,
“from” :
“Earth” Peter
Earth Marvel
} Parker
Krypton Clark Kent DC
Friday, March 1, 13
29. Update
The general form for update is:
db.collection.update(
<query>, <update>, <options> )
Which Entries What to do with
to update them
Friday, March 1, 13
30. Update
Some Update Operators
“$set”, “$inc”
“$push”, “$pushAll”, “$addToSet”
“$pop”, “$pull”, “$pullAll”
Friday, March 1, 13
31. Update: set
$set modifies a value or add a new value
Example:
db.posts.update(
{ title: “Why Is Your Cat Unhappy” },
{ $set : { “archived” : true } }
);
Friday, March 1, 13
32. Quiz: $set
Update owners array of the first cat with white color
If you want to update all objects, use multi
db.cats.update(
{ color: “white” },
{ “$set” : { “owners” : [“John”, “Jim”] } }
{ multi : true }
);
Friday, March 1, 13
35. The Driver
Hello MongoDB
Connecting and Authenticating
Querying/Updating Data
Coding Time
Friday, March 1, 13
36. Hello MongoDB
Mike Friedman, Kristina
Chodorow and rafl
MongoDB::Examples
MongoDB::Tutorial
Alternative Driver:
Mango
Friday, March 1, 13
37. Hello Mongo
use strict;
use warnings;
use v5.14;
use MongoDB;
use Data::Printer;
my $client = MongoDB::MongoClient->new;
my $db = $client->get_database('demo');
my $coll = $db->get_collection('highscore');
my @objects = $coll->find->all;
p @objects;
Friday, March 1, 13
38. Inserting Data
use strict;
use warnings;
use v5.14;
use MongoDB;
my $client = MongoDB::MongoClient->new;
my $db = $client->get_database('demo');
my $coll = $db->get_collection('highscore');
$coll->insert({ name => 'Mike', score => 99 });
Friday, March 1, 13
39. Querying Data
my $cursor = $coll->find({name => 'Tom'});
while ( my $next = $cursor->next ) {
say $next->{name}, " Got: ", $next->{score};
}
Friday, March 1, 13
40. Querying Data
Sort, Limit and Skip results using the cursor
my $cursor = $coll->find()->
sort({score => -1})->
limit(3);
while ( my $next = $cursor->next ) {
say $next->{name}, " Got: ", $next->{score};
}
Friday, March 1, 13