Skip to content

Commit

Permalink
client and shell added
Browse files Browse the repository at this point in the history
  • Loading branch information
fazt committed Dec 4, 2017
1 parent 81d97ed commit 65dd4d3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
37 changes: 37 additions & 0 deletions course/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// the client is the mongo shell

// to watch all databases
show dbs

// to see current databse selected
db

// to select or create a database
use mivirtualstore

// CRUD OPERATIONS
// TO CREATE

const post = {
"title": "My first Post",
"content": "Here is my blog post",
"date": new Date()
}
// this generates an _id
db.blog.insert(post) // WriteResult({ "nInserted" : 1 })

// TO READ
db.posts.find() // to query all collections
db.posts.findOne() // to query a single collection

// TO UPDATE
// we need (criteria, newDocument)
const myNewPost = db.posts.findOne()
myNewPost.comments = [];
db.posts.update(
{_id: ObjectId("5a2490f249c2a502853b7e7a")},
myNewPost
) // this add comments: []

// TO REMOVE
db.posts.remove({title: "My first Post"})
17 changes: 16 additions & 1 deletion course/collections/collection.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
// is a group of documents
// is equivalend of a table in SQL

// have dynamic squemas
// have DYNAMIC SQUEMAS
// means the documents can be different inside of a collection
// for example this could be stored in a single collection
{"greeting": "Hello World"}
{"foo": 5}

// so why we need to separate collections
// Keeping different kinds of documents in the same collection can be a nightmare
// we need to make sure that each query is only returning documents of a certain type or that the application code performing a
// • It is much faster to get a list of collections than to extract a list of the types in a collection. For example, if we had a "type" field in each document that specified whether the document was a “skim,” “whole,” or “chunky monkey,” it would be much
// slower to find those three values in a single collection than to have three separate collections and query the correct collection.
// • Grouping documents of the same kind together in the same collection allows for
// data locality. Getting several blog posts from a collection containing only posts will
// likely require fewer disk seeks than getting the same posts from a collection con‐
// taining posts and author data.
// • We begin to impose some structure on our documents when we create indexes.
// (This is especially true in the case of unique indexes.) These indexes are defined per
// collection. By putting only documents of a single type into the same collection, we
// can index our collections more efficiently.

// these documents have diferent keys and value types

// callection restrictions are:
Expand Down
2 changes: 2 additions & 0 deletions course/readme.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Course
- document/
- collections/
- shell/
22 changes: 22 additions & 0 deletions course/shell/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// to start the shell: mongo

// the shell can be execute javascript
x = 200;
x / 5;

// and we can use the standard javascript libraries
Math.sin(Math.PI / 2)

new Date("2018/1/1")

"Hello World".replace("World, Mongodb");

// and we can define functions
function factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}

const double = n => n * 2;

// and the shell also recongnise incomplete statments with ENTER
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
- orientado a documentos: document

## Comandos Usuales
- to show mongodb version: 'mongod --version'
- to show mongodb shell version: 'mongo --version'
- to show a database: `show dbs`
- to create a database: `use exampledatabase`
- to show the current database: `db`

- how to create a user: `db.createUser({})`
- how to login with my user: `use database`, `db.auth("user","psw")`

0 comments on commit 65dd4d3

Please sign in to comment.