You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
2.6 KiB
137 lines
2.6 KiB
# Databases - Mongo
|
|
|
|
## Lesson Objectives
|
|
1. show
|
|
1. use
|
|
1. create collection
|
|
1. insert
|
|
1. dropping
|
|
1. find
|
|
1. remove
|
|
1. count
|
|
|
|
## Show
|
|
1. `show dbs`
|
|
|
|
## Use
|
|
1. `use learn`
|
|
1. `db`
|
|
- show current database being used
|
|
|
|
## create collection
|
|
1. `db.createCollection('testCollection')`
|
|
|
|
## Insert
|
|
1. `db.testCollection.insert({foo:'true'})`
|
|
1. multi-lines
|
|
1. `show collections`
|
|
1. can use regular javascript with variables and functions
|
|
- watch out for cursors from find()
|
|
1. can insert array of objects
|
|
|
|
## Dropping
|
|
1. `db.dropDatabase();`
|
|
1. `db.collection.drop();`
|
|
|
|
## Find
|
|
```
|
|
db.employees.insert([{
|
|
name: 'Daisy',
|
|
dob: new Date(1992,2,13,7,47),
|
|
loves: ['carrot','papaya'],
|
|
weight: 600,
|
|
gender: 'm',
|
|
salary: 63
|
|
},{
|
|
name: 'Aurora',
|
|
dob: new Date(1991, 0, 24, 13, 0),
|
|
loves: ['carrot', 'grape'],
|
|
weight: 450,
|
|
gender: 'f',
|
|
salary: 43
|
|
},{
|
|
name: 'Unicrom',
|
|
dob: new Date(1973, 1, 9, 22, 10),
|
|
loves: ['energon', 'redbull'],
|
|
weight: 984,
|
|
gender: 'm',
|
|
salary: 182
|
|
},{
|
|
name: 'Roooooodles',
|
|
dob: new Date(1979, 7, 18, 18, 44),
|
|
loves: ['apple'],
|
|
weight: 575,
|
|
gender: 'm',
|
|
salary: 99
|
|
},{
|
|
name: 'Solnara',
|
|
dob: new Date(1985, 6, 4, 2, 1),
|
|
loves:['apple', 'carrot','chocolate'],
|
|
weight:550,
|
|
gender:'f',
|
|
salary:80
|
|
},{
|
|
name:'Ayna',
|
|
dob: new Date(1998, 2, 7, 8, 30),
|
|
loves: ['strawberry', 'lemon'],
|
|
weight: 733,
|
|
gender: 'f',
|
|
salary: 40
|
|
},{
|
|
name:'Kenny',
|
|
dob: new Date(1997, 6, 1, 10, 42),
|
|
loves: ['grape', 'lemon'],
|
|
weight: 690,
|
|
gender: 'm',
|
|
salary: 39
|
|
},{
|
|
name: 'Raleigh',
|
|
dob: new Date(2005, 4, 3, 0, 57),
|
|
loves: ['apple', 'sugar'],
|
|
weight: 421,
|
|
gender: 'm',
|
|
salary: 2
|
|
},{
|
|
name: 'Leia',
|
|
dob: new Date(2001, 9, 8, 14, 53),
|
|
loves: ['apple', 'watermelon'],
|
|
weight: 601,
|
|
gender: 'f',
|
|
salary: 33
|
|
},{
|
|
name: 'Pilot',
|
|
dob: new Date(1997, 2, 1, 5, 3),
|
|
loves: ['apple', 'watermelon'],
|
|
weight: 650,
|
|
gender: 'm',
|
|
salary: 54
|
|
},{
|
|
name: 'Nimue',
|
|
dob: new Date(1999, 11, 20, 16, 15),
|
|
loves: ['grape', 'carrot'],
|
|
weight: 540,
|
|
gender: 'f'
|
|
},{
|
|
name: 'Dunx',
|
|
dob: new Date(1976, 6, 18, 18, 18),
|
|
loves: ['grape', 'watermelon'],
|
|
weight: 704,
|
|
gender: 'm',
|
|
salary: 165}]);
|
|
```
|
|
|
|
1. `db.employees.find({gender:'m'})`
|
|
1. `db.employees.find({weight : { $gt : 700 }})`
|
|
- $lt, $lte, $gt, $gte, $ne, $exists: false, $in: ['orange', 'apple']
|
|
1. `db.employees.find({loves:'energon'})`
|
|
- if field is array, will return search within that array
|
|
1. AND
|
|
- `db.employees.find({gender: 'm', weight: {$gt: 700}})`
|
|
1. OR
|
|
- `db.employees.find({gender: 'f', $or: [{loves: 'apple'},{weight: {$lt: 500}}]})`
|
|
1. value between
|
|
- `db.employees.find( { salary: { $gte : 80, $lte : 165} } );`
|
|
|
|
## Remove
|
|
## Count
|