Git Product home page Git Product logo

zcu-workshop-mongo's Introduction

zcu-workshop-mongo

A workshop about MongoDB. Created for University of West Bohemia.

Starting MongoDB

Server

# linux
mongod --dbpath /data/db

# windows
mongod.exe --dbpath D:\Database\MongoDB\data

Client

# linux
mongo

# windows
mongo.exe

Creating database

use friendface

Inserting documents

Syntax: db.<collection-name>.insert(<document>)

// INSERT INTO users (username, name, sex, age) VALUES ('jon.doe', 'Jon Doe', 'male', 34);
db.users.insert({"username":"jon.doe","name":"Jon Doe","sex":"male","age":34);
db.users.insert({"username":"elison_heart","name":"Elison Heart","sex":"female","age":18);

// it's not problem to change schema - because there's not schema
db.users.insert({"username":"fanda","name":"František Vopršálek","sex":"Příležitostně", "hobbies":["NoSQL","BigData","pivo"]);

// it's possible to nest documents
db.users.insert({"username":"jim.kirk","name":"James T. Kirk","setup":{"subscribe":true,"email":"[email protected]"}});

MongoDB automatically creates field _id (equivalent to auto-increment in MySQL)

Querying documents

Syntax: db.<collection-name>.find([<query-document>],[<select-document>])

// SELECT * FROM users;
db.users.find();

// SELECT * FROM users WHERE username = 'fanda';
db.users.find({"username":"fanda"});

// SELECT * FROM users WHERE age < 20;
db.users.find({"age":{"$lt": 20}});

// SELECT username FROM users WHERE sex IN ('male','female');
db.users.find({"sex":{"$in":["male","female"]}});

// SELECT * FROM users LIMIT 1;
db.users.find().limit(1);

// SELECT * FROM users ORDER BY age DESC,name;
db.users.find().sort({"age":-1,"name":1});

Modifying documents

Syntax: db.<collection-name>.update(<query-document>,<set-document>);

// UPDATE users SET age = 35 WHERE username = 'jon.doe';
db.users.update({"username":"jon.doe"},{"$set":{"age":35}});

Removing documents

Syntax: db.<collection-name>.remove(<query-document>);

// DELETE FROM users WHERE age >= 50;
db.users.remove({"age":{"$gte":50}});

Importing collections

# linux
mongoimport --db friendface --collection users < users.json

# windows
mongoimport.exe /d friendface /c users /file users.json

Indexes

Syntax: db.<collection-name>.ensureIndex(<index-document>);

// simple index
db.users.ensureIndex({"username":1});

// compound index
db.users.ensureIndex({"username":1, age: 1});

Aggregation

Aggregation pipeline

example image

// count average age by gender
db.users.aggregate([
    {"$group":{"_id":"$sex","average":{"$avg":"$age"}}}
]);

Map-Reduce

example image

// map name (first name and last name separately) as key, value always 1
var map = function () {
    this.name.split(' ').forEach(function (name) {
        emit(name, 1);
    });
};

// sum up all given values
var reduce = function (key, values) {
    return Array.sum(values);
}

db.users.mapReduce(map, reduce, {"out":"users_names"});

This repo

http://bit.ly/23SY4Na

Sources

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.