13.03.2017
Dipl-Inf. (FH) Johannes Hoppe
NoSQL
MongoDB Quick Reference Cards
http://www.10gen.com/reference
“Deployment”
› Standardverzeichnis erstellen:
c:datadb
› Server-Start: mongod.exe
› Shell: mongo.exe
CRUD – Create
In der Shell
› use WebNote
› db.Notes.save(
{
Title: 'Mittag',
Message: 'nicht vergessen‘
}
);
So funktioniert der Befehl
› db.Notes.save
CRUD – Create
…with a bit JavaScript
for(i=0; i<1000; i++) {
['quiz', 'essay', 'exam'].forEach(function(name) {
var score = Math.floor(Math.random() * 50) + 50;
db.scores.save({student: i, name: name, score:
score});
});
}
db.scores.count();
CRUD – Read
Queries werden ebenso im Dokument-Stil spezifiziert
› db.Notes.find();
› db.Notes.find({ Title: /Test/i });
› db.Notes.find(
{ "Categories.Color": "red"}).limit(1);
CRUD – Update
› db.Notes.update({Title: 'Test'},
{'$set': {Categories: []}});
› db.Notes.update({Title: 'Test'},
{'$push': {
Categories:
{Color: 'Red'}
}
});
CRUD – Delete
› db.dropDatabase();
› db.Notes.drop();
› db.Notes.remove();
Hands ON!
Data Import
(hands-on.zip)
cd dump_training
mongorestore -d training -c scores scores.bson
cd dump_digg
mongorestore -d digg -c stories stories.bson
Test
(in the shell)
use digg
db.stories.findOne();
Exercises
1. Find all scores less than 65.
2. Find the lowest quiz score. Find the highest quiz score.
3. Write a query to find all digg stories where the view
count is greater than 1000.
4. Query for all digg stories whose media type is either
'news' or 'images' and where the topic name is
'Comedy’.
5. Find all digg stories where the topic name is
'Television' or the media type is 'videos'. Skip the first 5
results, and limit the result set to 10.
CRUD – Update
› use digg;
› db.people.update({name: 'Smith'},
{'$set': {interests: []}});
› db.people.update({name: 'Smith'},
{'$push': {interests: ['chess']}});
Exercises
1. Set the proper 'grade' attribute for all scores. For
example, users with scores greater than 90 get an 'A.'
Set the grade to ‘B’ for scores falling between 80 and
90.
2. You're being nice, so you decide to add 10 points to
every score on every “final” exam whose score is
lower than 60. How do you do this update?

NoSQL - Hands on

  • 1.
  • 2.
    NoSQL MongoDB Quick ReferenceCards http://www.10gen.com/reference
  • 3.
    “Deployment” › Standardverzeichnis erstellen: c:datadb ›Server-Start: mongod.exe › Shell: mongo.exe
  • 5.
    CRUD – Create Inder Shell › use WebNote › db.Notes.save( { Title: 'Mittag', Message: 'nicht vergessen‘ } ); So funktioniert der Befehl › db.Notes.save
  • 6.
    CRUD – Create …witha bit JavaScript for(i=0; i<1000; i++) { ['quiz', 'essay', 'exam'].forEach(function(name) { var score = Math.floor(Math.random() * 50) + 50; db.scores.save({student: i, name: name, score: score}); }); } db.scores.count();
  • 7.
    CRUD – Read Querieswerden ebenso im Dokument-Stil spezifiziert › db.Notes.find(); › db.Notes.find({ Title: /Test/i }); › db.Notes.find( { "Categories.Color": "red"}).limit(1);
  • 8.
    CRUD – Update ›db.Notes.update({Title: 'Test'}, {'$set': {Categories: []}}); › db.Notes.update({Title: 'Test'}, {'$push': { Categories: {Color: 'Red'} } });
  • 9.
    CRUD – Delete ›db.dropDatabase(); › db.Notes.drop(); › db.Notes.remove();
  • 10.
  • 11.
    Data Import (hands-on.zip) cd dump_training mongorestore-d training -c scores scores.bson cd dump_digg mongorestore -d digg -c stories stories.bson
  • 12.
    Test (in the shell) usedigg db.stories.findOne();
  • 13.
    Exercises 1. Find allscores less than 65. 2. Find the lowest quiz score. Find the highest quiz score. 3. Write a query to find all digg stories where the view count is greater than 1000. 4. Query for all digg stories whose media type is either 'news' or 'images' and where the topic name is 'Comedy’. 5. Find all digg stories where the topic name is 'Television' or the media type is 'videos'. Skip the first 5 results, and limit the result set to 10.
  • 14.
    CRUD – Update ›use digg; › db.people.update({name: 'Smith'}, {'$set': {interests: []}}); › db.people.update({name: 'Smith'}, {'$push': {interests: ['chess']}});
  • 15.
    Exercises 1. Set theproper 'grade' attribute for all scores. For example, users with scores greater than 90 get an 'A.' Set the grade to ‘B’ for scores falling between 80 and 90. 2. You're being nice, so you decide to add 10 points to every score on every “final” exam whose score is lower than 60. How do you do this update?

Editor's Notes

  • #4 mongod.exe --dbpath c:\data\db --port 27017 --logpath c:\data\mongodb.log
  • #6 db.Notes.save({Title: 'Mittag', Message: 'nicht vergessen'});
  • #7  for(i=0; i<1000; i++) { ['quiz', 'essay', 'exam'].forEach(function(name) { var score = Math.floor(Math.random() * 50) + 50; db.scores.save({student: i, name: name, score: score}); }); } db.scores.count();
  • #8 use WebNote db.Notes.find(); db.Notes.find({Title: /Test/i }); db.Notes.find({ "Categories.Color": "red"}).limit(1); Query operators: $gt, $gte, $lt, $lte, $ne, $in, $nin, $mod, $all, $size, $exists, $type, $not, $or, $elemMatch, $where.
  • #9 use WebNote db.Notes.save({Title: 'Test'}); db.Notes.update({Title: 'Test'}, {'$set': {Categories: []}}); db.Notes.update({Title: 'Test'}, {'$push': {Categories: { Color: 'Red' }}}); Update Operators: $set, $unset, $inc, $push, $pushAll, $pull, $pullAll, $pop, $addToSet $push { $push : { field : value } } appends value to field $set { $set : { field : value } } sets field to value.
  • #10 use WebNote db.dropDatabase(); db.Notes.drop(); db.Notes.remove();
  • #12 cd dump_training mongorestore -d training -c scores scores.bson cd dump_digg mongorestore -d digg -c stories stories.bson
  • #13 use digg db.stories.findOne(); db.runCommand({listDatabases: 1}) db.getCollectionNames();
  • #15 Update Operators: $set, $unset, $inc, $push, $pushAll, $pull, $pullAll, $pop, $addToSet $push { $push : { field : value } } appends value to field $set { $set : { field : value } } sets field to value.