47
Ludwig-Maximilians-Universität München Online Multimedia Tutorial 08 – Databases and Authentication Winter Semester 2019/20 Online Multimedia WS 2019/20 - Tutorial 08 1

Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Online Multimedia

Tutorial 08 – Databases and Authentication

Winter Semester 2019/20

Online Multimedia WS 2019/20 - Tutorial 08 1

Page 2: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Today’s Agenda

• Authentication and Authorization

• NodeJS + MongoDB

Online Multimedia WS 2019/20 - Tutorial 08 2

Page 3: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München Online Multimedia WS 2019/20 - Tutorial 08 3

Digital Rights Management:Access Control

Page 4: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Restricted Content

• On many occasions, web apps need to restrict access to resources(e.g. media, sensitive data)

• Usual Solution: User authentication and authorization

• Differentiation:• Authentication: ≈ Verification (you are who you claim to be)• Authorization: ≈ Grant access (allowed to use a resource)

Online Multimedia WS 2019/20 - Tutorial 08 4

Page 5: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität MünchenLudwig-Maximilians-Universität München

Quick Quiz

Are the following error messages related to authentication orauthorization?

1. „Permission to URL denied to user johndoe12“2. „Comments are disabled for this video“3. „This site is marked private by its owner“4. „Sorry, this action requires being logged in.“

Online Multimedia WS 2019/20 - Tutorial 08 5

Page 6: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Authentication in Web Apps• Access Tokens, e.g. API Keys

• URL Parameter• HTTP Header• HTTP Message Body

• Cookies • Contains (authentication) session ID• Server handles authorization based on this ID (deserialization)

• Third Party Authentication• OAuth• OpenID

Online Multimedia WS 2019/20 - Tutorial 08 6

Page 7: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Basic-Auth Middleware in NodeJS

Online Multimedia WS 2019/20 - Tutorial 08 7

app.use(logger('dev'));app.use(express.json());app.use(express.urlencoded({ extended: false }));app.use(cookieParser());

// ---- basic auth middleware ----app.use((req,res,next) => {

console.log(req.headers.authorization)if (req.headers.authorization !== 'Basic c3R1ZGVudDpvbW1pc2F3ZXNvbWU=') {

res.set('WWW-Authenticate', 'Basic realm="401"')res.status(401).send()return

}else {

next();}

})

app.use(express.static(path.join(__dirname, 'public')));app.use('/', indexRouter);app.use('/users', usersRouter);

Base64 encoding ofstudent:ommisawesome

basic-auth-app/app.js

Page 8: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Traditional Authentication: with Sessions

Online Multimedia WS 2019/20 - Tutorial 08 8

ServerClient

Session Data

Request

Response

Lookup

Page 9: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Just Another Authentication Strategy

HTTP is stateless => further user properties (e.g. roles, permissions) have to be looked up on every request

JWT allows encoding of properties in a token• The server encrypts user id, roles, … into a token and hands it to the

client• The client sends this token alongside each request• The server encrypts it to authenticate the user and can ensure that

the encoded properties are not manipulated

Online Multimedia WS 2019/20 - Tutorial 08 9

Page 10: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München Online Multimedia WS 2019/20 - Tutorial 08 10

Code Along: JWT Middleware

Page 11: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

JWT Middleware

Two parts:• Login route: BasicAuth login with username + password required, returning a

JWT token• Middleware: Verify the JWT token in a middleware on each following request

Online Multimedia WS 2019/20 - Tutorial 08 11

Page 12: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Step 1: Login route

Online Multimedia WS 2019/20 - Tutorial 08 12

• Create a new route matching /login

• Check for Basic-Auth credentials

Page 13: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Step 2: Create a JWT token

Online Multimedia WS 2019/20 - Tutorial 08 13

const jwt = require('njwt')const claims = {permission: 'read-data', username: 'student '}const token = jwt.create(claims, 'something-top-secret')token.setExpiration(new Date().getTime() + 60 * 1000)const jwtTokenString = token.compact()

Import a library to do all the encryption stuff

Properties to be encrypted into the JWT token

Encryption with a secretthat should only beknown by the server

Specify an expiration time (optional)

Page 14: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Step 3: Authorize endpoints by verifying theJWT token• Similar to the Basic-Auth middleware in the previous example app

• Replace check for correct Authentication header with JWT tokenverification

Online Multimedia WS 2019/20 - Tutorial 08 14

jwt.verify(jwtTokenString, 'something-top-secret', (err, verifiedJwt) => {if(err){// the given token is not valid – respond with 401!

}else{// authentication successful! Continue in the middleware chain

}})

Decryption, with the same secret

Object containingthe encryted

properties

Page 15: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München Online Multimedia WS 2019/20 - Tutorial 08 15

Jwt {header: JwtHeader { typ: 'JWT', alg: 'HS256' },body:JwtBody {

permission: 'read-data',username: 'student',jti: '27afc6fa-40ab-456e-8de7-c4264b9771a2',iat: 1574929725,exp: 1574929785 },

toString: [Function] }

The verifiedJwt object, logged to the console:

Page 16: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Using MongoDB with NodeJS

Online Multimedia WS 2019/20 - Tutorial 08 16

Page 17: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Why a database?

Online Multimedia WS 2019/20 - Tutorial 08 17

app.use('/login', (req,res,next) => {if (req.headers.authorization !== 'Basic c3R1ZGVudDpvbW1pc2F3ZXNvbWU=‚) {

res.set('WWW-Authenticate', 'Basic realm="401"')

Page 18: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Motivation

Web applications need a database to

• … store data that does not fit into working memory

• … keep data after application restart

• … structure and organize data

Online Multimedia WS 2019/20 - Tutorial 08 18

Page 19: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Again we have a client / server relationship

Online Multimedia WS 2019/20 - Tutorial 08 19Online Multimedia WS 2018/19 - Tutorial 08 - 19

Server

mongod

Client

mongo

Request Database query

Response Data (or

some message)

Page 20: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Is NodeJS client or server now?

Online Multimedia WS 2019/20 - Tutorial 08 20

Server

mongodNodeJS

Request Database query

Response Data (or

some message)

Client

Web-browser

Is NodeJS client orserver now? Well, it‘s a matter ofperspective…

(Web-)Request

Response (HTML, JSON, …)

Webserver

Page 21: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

NodeJS and MongoDB

• There are a couple of implementations for NoSQL/MongoDB middleware inNodeJS

• For MongoDB, the most prevalent examples are– monk– mongoose

• In the tutorial, we use monk, but mongoose works quitesimilar.

Online Multimedia WS 2019/20 - Tutorial 08 21

Page 22: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Using MongoDB middleware

• const db = require('monk')('localhost/omm-1920');

• This has to come very early in the middleware chain:app.use(function(req,res,next){ req.db = db;

next();});

On the CIP Pool: Not possible. But you could use an online-hosted database „Database-as-a-service“: https://www.mongodb.com/cloud/atlas

Online Multimedia WS 2019/20 - Tutorial 08 22

Page 23: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

MongoDB using Monk

Online Multimedia WS 2019/20 - Tutorial 08 23

Page 24: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Accessing the database with monk

const express = require('express');const router = express.Router();router.get('/users',function(req,res){

const db = req.db;const users = db.get('users'); users.find({},{}).then((docs) => res.json(docs)).catch((e) => res.status(500).send())

})module.exports = router;

Note: it’s not necessary to require monk here! Why?

Online Multimedia WS 2019/20 - Tutorial 08 24

123

Page 25: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Accessing the database with monk

const express = require('express');const router = express.Router();router.get('/users',function(req,res){

const db = req.db;const users = db.get('users'); users.find({username: 'student'},{}).then((docs) => res.json(docs)).catch((e) => res.status(500).send())

})module.exports = router;

Note: it’s not necessary to require monk here! Why?

Online Multimedia WS 2019/20 - Tutorial 08 25

Options "SELECT *"

Query "WHERE username = 'student'"

Page 26: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität MünchenLudwig-Maximilians-Universität München

Breakout

User Database

• Setup MongoDB on your computer

• Create a MongoDB collection for users

• In the login route, check for a user in the database instead ofcomparing against a hardcoded Basic-Auth header

Online Multimedia WS 2019/20 - Tutorial 08 26

Page 27: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Round-up Quiz1. Does Authentication usually come before or after Authorization?

2. What is the benefit of JWTs over Sessions?

3. What is the canonical way to add authentication in NodeJS?

4. Which parameters are common for a … Monk query?1. Find2. Update

5. Is Monk client or server?

Online Multimedia WS 2019/20 - Tutorial 08 27

Page 28: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Thanks!What are your questions?

Online Multimedia WS 2019/20 - Tutorial 08 28

Page 29: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Appendix

Online Multimedia WS 2019/20 - Tutorial 08 29

Page 30: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Install MongoDB (Windows)• Download MongoDB Community

Server:• https://www.mongodb.com/download-

center/community

• Uncheck Install MongoD as a Service

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 30

Page 31: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Install MongoDB (Mac OS)

• Easiest: Use Homebrew

• Step 1 (optional): Update Homebrew’s packagedatabase.> brew update

• Step 2: Install MongoDB> brew install mongodb

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 31

Page 32: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Start MongoDB (Windows)

• In a shell, start the daemon:C:\Program Files\MongoDB\Server\4.0\bin>mongod

• Launch mongo client:C:\Program Files\MongoDB\Server\4.0\bin>mongo

• Create a database:> use mmn1819

• Verify:> show dbs

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 32

Page 33: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Start MongoDB (Mac OS)

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 33

• In the terminal, start the daemon (using the defaultdata directory /data/db):> mongod

• Launch mongo client:> mongo

Create a database:> use mmn1819

• Verify by checking the process output for the following line:> [initandlisten] waiting for connections on port 27017

Page 34: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Terminology

SQL MongoDBdatabase databasetable collectionrow documentcolumn fieldindex indextable joins embedded documents and linkingprimary key primary keyUNIQUE column Automatically generated _id field

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 34

Page 35: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Creating Collections

• Collections are created implicitly (as are databases)• Alternative:

db.createCollection("collectionName")

SQL MongoDB

CREATE TABLE users (id INT NOT NULLAUTO_INCREMENT PRIMARY KEY,user_id VARCHAR(30), age INT, status CHAR(1)

)

db.users.insert({user_id: "abc123",age: 55,status: "A"

})

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 36

Page 36: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Inserting Data

• Inserts are JSON Objects• Multiple objects can be wrapped into an array and then

inserted

SQL MongoDBINSERT INTO users (user_id, age, status)

VALUES("bcd001", 45, "A")

db.users.insert({user_id: "bcd001", age: 45,status: "A",

})

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 37

Page 37: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Multiple types in the same collection

• db.collection.insert({ foo: 'bar' }); db.collection.insert({ lorem: 'ipsum' });

• Developers have to take care about what objects to put in a collection

• Yet, Mongo is really flexible!

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 38

Page 38: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Querying

Ludwig-Maximilians-Universität München Online Multimedia WS 2018/19 - Tutorial 07 -

http://docs.mongodb.org/manual/reference/sql-comparison/

SQL MongoDBSELECT * FROM users db.users.find()

SELECT id, user_id, status FROMusers

db.users.find({ }, { user_id: 1, status: 1 })

SELECT * FROM users WHERE status = "A"

db.users.find({ }, { user_id: 1, status: 1, _id: 0 })

SELECT user_id, status FROM usersWHERE 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" }})

Page 39: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Update & DeleteSQL MongoDBUPDATE users SET status = 'C'WHERE age > 25

db.users.update({ age: { $gt: 25 }},{ $set: { status: "C" } }, { multi: true })

UPDATE users SET age = age + 3WHERE status = 'A'

db.users.update({ status: "A" },{ $inc: { age: 3 } },{ multi: true })

SQL MongoDBDELETE FROM users WHERE status = "D"

db.users.remove( { status: "D" } )

DELETE FROM users db.users.remove({})

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 40

Page 40: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Operators

• Operators are special keys inside queries in MongoDB• You can’t write ‘someKey’ != ‘someValue’.• Most common operators:

– $ne, $gt, $lt, $gte, $lte– $and, $or– $elemMatch

• Example:SQL MongoDB

SELECT * FROM users WHEREstatus = "A" OR age = 50

db.users.find({ $or: [ { status: "A" } , { age: 50 } ]})

http://docs.mongodb.org/manual/reference/operator/

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 41

Page 41: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

SQL-Like Modelling in Mongo - References

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 42

Page 42: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

The Mongo Way – Embedded Documents

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 43

Page 43: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

References vs. Embedded Data

• Pros Embedded Data:• No ORM mapping required. JSON interface fits well to web technologies• No tons of empty db fields in case of only sometimes set properties• Scalability (e.g. for embedded documents less queries are needed to select)

• Pros References:• Structure of a data record is not checked on insert (but you can define and

enforce document validation rules)• „duplicate content“ in case of many relationships

Ludwig-Maximilians-Universität München

Online Multimedia WS 2018/19 - Tutorial 07 - 44

Page 44: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Monk - Operations

• Monk wraps statements to JS Functions:– find()– findOne()– update()– updateById()

• All (!) queries are asynchronous! Callbacks!

router.get('/users',function(req,res){var db = req.db;var users = db.get('users');users.find({status : {$ne : 'A'}},function(e,docs){ res.json(docs);});

});

Online Multimedia WS 2019/20 - Tutorial 08 45

Page 45: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

The Query

• Select documents by criteria

• … see last week‘s tutorial

Online Multimedia WS 2019/20 - Tutorial 08 46

The OptionsWhat of and how the documentsshould be returned

– Select fields to return

– Sorting

users.find({}, '-name‚}).then…// everything except the field 'name‘

});

users.find({}, {sort:{name:1}}).then…// order by name ascending

});

Page 46: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Translating terminal queries to Javascript

Usually not that difficult – in most cases both is the same

Terminal:

db.albums.find({artists:{$elemMatch:{name:"Queen"}}})

Javascript:

db.get('albums').find({artists:{$elemMatch:{name:"Queen"}}).then…

Online Multimedia WS 2019/20 - Tutorial 08 47

Page 47: Online Multimedia - Medieninformatik · 2020-04-11 · • There are a couple of implementations for NoSQL/MongoDB middleware in NodeJS • For MongoDB, the most prevalent examples

Ludwig-Maximilians-Universität München

Further Tips & Tricks

• MongoDB does not allow you have keys containing dots, eg:db.users.insert({'127.0.0.1': 'up'}); // not allowed

• Solution: Create a unique hash of the key and store it.String.prototype.hashCode = function() {

var hash = 0, i, chr, len;if (this.length == 0) return hash;for (i = 0, len = this.length; i < len; i++) {

chr = this.charCodeAt(i);hash = ((hash << 5) - hash) + chr; hash |= 0; // Convert to 32bit integer

}return hash;

};

Online Multimedia WS 2019/20 - Tutorial 08 48