├── .gitignore ├── 02-Connecting-to-MongoDB ├── index.js ├── mongo.js └── package.json ├── 03-Storing-Data-to-MongoDB ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 04-Reading-Data ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 05-Updating-Data ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 06-Deleting-Data ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 07-Find-and-Update ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 08-Increasing-and-Decreasing-Numbers ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 09-Add-Remove-from-Array ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 10-Sorting ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 11-Timestamps-Defaults-and-Ranges ├── index.js ├── mongo.js ├── package.json └── schemas │ ├── message-schema.js │ └── user-schema.js ├── 12-Pluralized-Collection-Names ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 13-Insert-and-Delete-Many ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 14-Nested-Objects ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 15-Rename-and-Unset-Operators ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 16-Conditional-Searches ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js ├── 17-ElemMatch-All-and-Size ├── index.js ├── mongo.js ├── package.json └── schemas │ └── user-schema.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | config.json 3 | package-lock.json -------------------------------------------------------------------------------- /02-Connecting-to-MongoDB/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | 3 | const connectToMongoDB = async () => { 4 | await mongo().then((mongoose) => { 5 | try { 6 | console.log('Connected to mongodb!') 7 | } finally { 8 | mongoose.connection.close() 9 | } 10 | }) 11 | } 12 | 13 | connectToMongoDB() 14 | -------------------------------------------------------------------------------- /02-Connecting-to-MongoDB/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPass } = require('./config.json') 3 | const mongoPath = `mongodb+srv://tutorial:${mongoPass}@mongodb-tutorial.hbbee.mongodb.net/test-db?retryWrites=true&w=majority` 4 | 5 | module.exports = async () => { 6 | await mongoose.connect(mongoPath, { 7 | useNewUrlParser: true, 8 | useUnifiedTopology: true, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /02-Connecting-to-MongoDB/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /03-Storing-Data-to-MongoDB/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | const user = { 10 | email: 'test@email.com', 11 | username: 'Joe', 12 | password: 'Password1!', 13 | } 14 | 15 | await new userSchema(user).save() 16 | } finally { 17 | mongoose.connection.close() 18 | } 19 | }) 20 | } 21 | 22 | connectToMongoDB() 23 | -------------------------------------------------------------------------------- /03-Storing-Data-to-MongoDB/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPass } = require('./config.json') 3 | const mongoPath = `mongodb+srv://tutorial:${mongoPass}@mongodb-tutorial.hbbee.mongodb.net/test-db?retryWrites=true&w=majority` 4 | 5 | module.exports = async () => { 6 | await mongoose.connect(mongoPath, { 7 | useNewUrlParser: true, 8 | useUnifiedTopology: true, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /03-Storing-Data-to-MongoDB/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /03-Storing-Data-to-MongoDB/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const userSchema = mongoose.Schema({ 9 | email: reqString, 10 | username: reqString, 11 | password: reqString, 12 | }) 13 | 14 | module.exports = mongoose.model('users', userSchema) 15 | -------------------------------------------------------------------------------- /04-Reading-Data/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | const result = await userSchema.findOne({ 10 | password: 'Password1!', 11 | }) 12 | console.log('Result:', result) 13 | } finally { 14 | mongoose.connection.close() 15 | } 16 | }) 17 | } 18 | 19 | connectToMongoDB() 20 | -------------------------------------------------------------------------------- /04-Reading-Data/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPass } = require('./config.json') 3 | const mongoPath = `mongodb+srv://tutorial:${mongoPass}@mongodb-tutorial.hbbee.mongodb.net/test-db?retryWrites=true&w=majority` 4 | 5 | module.exports = async () => { 6 | await mongoose.connect(mongoPath, { 7 | useNewUrlParser: true, 8 | useUnifiedTopology: true, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /04-Reading-Data/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /04-Reading-Data/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const userSchema = mongoose.Schema({ 9 | email: reqString, 10 | username: reqString, 11 | password: reqString, 12 | }) 13 | 14 | module.exports = mongoose.model('users', userSchema) 15 | -------------------------------------------------------------------------------- /05-Updating-Data/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | await userSchema.updateMany( 10 | { 11 | password: 'Password1!', 12 | }, 13 | { 14 | password: 'Abc123', 15 | } 16 | ) 17 | } finally { 18 | mongoose.connection.close() 19 | } 20 | }) 21 | } 22 | 23 | connectToMongoDB() 24 | -------------------------------------------------------------------------------- /05-Updating-Data/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPass } = require('./config.json') 3 | const mongoPath = `mongodb+srv://tutorial:${mongoPass}@mongodb-tutorial.hbbee.mongodb.net/test-db?retryWrites=true&w=majority` 4 | 5 | module.exports = async () => { 6 | await mongoose.connect(mongoPath, { 7 | useNewUrlParser: true, 8 | useUnifiedTopology: true, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /05-Updating-Data/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /05-Updating-Data/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const userSchema = mongoose.Schema({ 9 | email: reqString, 10 | username: reqString, 11 | password: reqString, 12 | }) 13 | 14 | module.exports = mongoose.model('users', userSchema) 15 | -------------------------------------------------------------------------------- /06-Deleting-Data/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | await userSchema.deleteMany({ 10 | email: 'test@email.com', 11 | }) 12 | } finally { 13 | mongoose.connection.close() 14 | } 15 | }) 16 | } 17 | 18 | connectToMongoDB() 19 | -------------------------------------------------------------------------------- /06-Deleting-Data/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPass } = require('./config.json') 3 | const mongoPath = `mongodb+srv://tutorial:${mongoPass}@mongodb-tutorial.hbbee.mongodb.net/test-db?retryWrites=true&w=majority` 4 | 5 | module.exports = async () => { 6 | await mongoose.connect(mongoPath, { 7 | useNewUrlParser: true, 8 | useUnifiedTopology: true, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /06-Deleting-Data/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /06-Deleting-Data/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const userSchema = mongoose.Schema({ 9 | email: reqString, 10 | username: reqString, 11 | password: reqString, 12 | }) 13 | 14 | module.exports = mongoose.model('users', userSchema) 15 | -------------------------------------------------------------------------------- /07-Find-and-Update/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | let password = 'abc123' 10 | 11 | const result = await userSchema.findOneAndUpdate( 12 | { 13 | username: 'Frank', 14 | }, 15 | { 16 | email: 'test@email.com', 17 | username: 'Frank', 18 | password, 19 | }, 20 | { 21 | upsert: true, 22 | new: true, 23 | } 24 | ) 25 | 26 | console.log('RESULT:', result) 27 | } finally { 28 | mongoose.connection.close() 29 | } 30 | }) 31 | } 32 | 33 | connectToMongoDB() 34 | -------------------------------------------------------------------------------- /07-Find-and-Update/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPass } = require('./config.json') 3 | const mongoPath = `mongodb+srv://tutorial:${mongoPass}@mongodb-tutorial.hbbee.mongodb.net/test-db?retryWrites=true&w=majority` 4 | 5 | module.exports = async () => { 6 | await mongoose.connect(mongoPath, { 7 | useNewUrlParser: true, 8 | useUnifiedTopology: true, 9 | useFindAndModify: false, 10 | }) 11 | 12 | return mongoose 13 | } 14 | -------------------------------------------------------------------------------- /07-Find-and-Update/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /07-Find-and-Update/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const userSchema = mongoose.Schema({ 9 | email: reqString, 10 | username: reqString, 11 | password: reqString, 12 | }) 13 | 14 | module.exports = mongoose.model('users', userSchema) 15 | -------------------------------------------------------------------------------- /08-Increasing-and-Decreasing-Numbers/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | let password = 'abc123' 10 | 11 | const result = await userSchema.findOneAndUpdate( 12 | { 13 | username: 'Frank', 14 | }, 15 | { 16 | email: 'test@email.com', 17 | username: 'Frank', 18 | password, 19 | $inc: { 20 | messages: -2, 21 | }, 22 | }, 23 | { 24 | upsert: true, 25 | new: true, 26 | } 27 | ) 28 | 29 | console.log('RESULT:', result) 30 | } finally { 31 | mongoose.connection.close() 32 | } 33 | }) 34 | } 35 | 36 | connectToMongoDB() 37 | -------------------------------------------------------------------------------- /08-Increasing-and-Decreasing-Numbers/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPass } = require('./config.json') 3 | const mongoPath = `mongodb+srv://tutorial:${mongoPass}@mongodb-tutorial.hbbee.mongodb.net/test-db?retryWrites=true&w=majority` 4 | 5 | module.exports = async () => { 6 | await mongoose.connect(mongoPath, { 7 | useNewUrlParser: true, 8 | useUnifiedTopology: true, 9 | useFindAndModify: false, 10 | }) 11 | 12 | return mongoose 13 | } 14 | -------------------------------------------------------------------------------- /08-Increasing-and-Decreasing-Numbers/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /08-Increasing-and-Decreasing-Numbers/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const userSchema = mongoose.Schema({ 9 | email: reqString, 10 | username: reqString, 11 | password: reqString, 12 | messages: Number, 13 | }) 14 | 15 | module.exports = mongoose.model('users', userSchema) 16 | -------------------------------------------------------------------------------- /09-Add-Remove-from-Array/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | const newName = 'joe' 10 | 11 | await userSchema.findOneAndUpdate( 12 | { 13 | email: 'test@email.com', 14 | }, 15 | { 16 | $pull: { 17 | nameHistory: newName, 18 | }, 19 | } 20 | ) 21 | } finally { 22 | mongoose.connection.close() 23 | } 24 | }) 25 | } 26 | 27 | connectToMongoDB() 28 | -------------------------------------------------------------------------------- /09-Add-Remove-from-Array/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPath } = require('./config.json') 3 | 4 | module.exports = async () => { 5 | await mongoose.connect(mongoPath, { 6 | useNewUrlParser: true, 7 | useUnifiedTopology: true, 8 | useFindAndModify: false, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /09-Add-Remove-from-Array/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /09-Add-Remove-from-Array/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const userSchema = mongoose.Schema({ 9 | email: reqString, 10 | username: reqString, 11 | password: reqString, 12 | messages: Number, 13 | nameHistory: [String], 14 | }) 15 | 16 | module.exports = mongoose.model('users', userSchema) 17 | -------------------------------------------------------------------------------- /10-Sorting/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | const results = await userSchema 10 | .find({}) 11 | .sort({ 12 | messages: -1, 13 | }) 14 | .limit(10) 15 | 16 | console.log('RESULTS:', results) 17 | } finally { 18 | mongoose.connection.close() 19 | } 20 | }) 21 | } 22 | 23 | connectToMongoDB() 24 | -------------------------------------------------------------------------------- /10-Sorting/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPath } = require('./config.json') 3 | 4 | module.exports = async () => { 5 | await mongoose.connect(mongoPath, { 6 | useNewUrlParser: true, 7 | useUnifiedTopology: true, 8 | useFindAndModify: false, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /10-Sorting/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /10-Sorting/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const userSchema = mongoose.Schema({ 9 | email: reqString, 10 | username: reqString, 11 | password: reqString, 12 | messages: Number, 13 | nameHistory: [String], 14 | }) 15 | 16 | module.exports = mongoose.model('users', userSchema) 17 | -------------------------------------------------------------------------------- /11-Timestamps-Defaults-and-Ranges/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const message = require('./schemas/message-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | await new message({ 10 | text: 'hello world', 11 | }).save() 12 | } finally { 13 | mongoose.connection.close() 14 | } 15 | }) 16 | } 17 | 18 | connectToMongoDB() 19 | -------------------------------------------------------------------------------- /11-Timestamps-Defaults-and-Ranges/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPath } = require('./config.json') 3 | 4 | module.exports = async () => { 5 | await mongoose.connect(mongoPath, { 6 | useNewUrlParser: true, 7 | useUnifiedTopology: true, 8 | useFindAndModify: false, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /11-Timestamps-Defaults-and-Ranges/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /11-Timestamps-Defaults-and-Ranges/schemas/message-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const messageSchema = mongoose.Schema({ 4 | text: String, 5 | }) 6 | 7 | module.exports = mongoose.model('message', messageSchema, 'message') 8 | -------------------------------------------------------------------------------- /11-Timestamps-Defaults-and-Ranges/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const userSchema = mongoose.Schema( 9 | { 10 | email: reqString, 11 | username: reqString, 12 | password: reqString, 13 | messages: { 14 | type: Number, 15 | default: 5, 16 | min: 0, 17 | max: 10, 18 | }, 19 | nameHistory: [String], 20 | }, 21 | { 22 | timestamps: true, 23 | } 24 | ) 25 | 26 | module.exports = mongoose.model('users', userSchema) 27 | -------------------------------------------------------------------------------- /12-Pluralized-Collection-Names/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | const newUser = await new userSchema({ 10 | email: 'test@testing.com', 11 | username: 'New user', 12 | password: 'Pass123', 13 | messages: 10, 14 | }) 15 | 16 | const valid = await new Promise((resolve) => { 17 | newUser.validate((err) => { 18 | if (err) { 19 | console.log('ERROR:', err) 20 | resolve(false) 21 | } else { 22 | resolve(true) 23 | } 24 | }) 25 | }) 26 | 27 | if (valid) { 28 | await newUser.save() 29 | console.log('Saved the new user') 30 | } 31 | 32 | // await userSchema.findOneAndUpdate( 33 | // { 34 | // username: 'New user', 35 | // }, 36 | // { 37 | // messages: 5, 38 | // } 39 | // ) 40 | } finally { 41 | mongoose.connection.close() 42 | } 43 | }) 44 | } 45 | 46 | connectToMongoDB() 47 | -------------------------------------------------------------------------------- /12-Pluralized-Collection-Names/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPath } = require('./config.json') 3 | 4 | module.exports = async () => { 5 | await mongoose.connect(mongoPath, { 6 | useNewUrlParser: true, 7 | useUnifiedTopology: true, 8 | useFindAndModify: false, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /12-Pluralized-Collection-Names/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /12-Pluralized-Collection-Names/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const userSchema = mongoose.Schema( 9 | { 10 | email: reqString, 11 | username: reqString, 12 | password: reqString, 13 | messages: { 14 | type: Number, 15 | default: 5, 16 | min: 0, 17 | max: 10, 18 | }, 19 | nameHistory: [String], 20 | }, 21 | { 22 | timestamps: true, 23 | } 24 | ) 25 | 26 | module.exports = mongoose.model('users', userSchema) 27 | -------------------------------------------------------------------------------- /13-Insert-and-Delete-Many/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | // Inserting multiple documents 10 | await userSchema.insertMany([ 11 | { 12 | email: 'test1@email.com', 13 | username: 'test 1', 14 | password: 'passsword', 15 | }, 16 | { 17 | email: 'test2@email.com', 18 | username: 'test 2', 19 | password: 'passsword', 20 | }, 21 | { 22 | email: 'test3@email.com', 23 | username: 'test 3', 24 | password: 'passsword', 25 | }, 26 | ]) 27 | 28 | // Deleting multiple documents 29 | await userSchema.deleteMany({ 30 | username: ['test 1', 'test 2'], 31 | }) 32 | } finally { 33 | mongoose.connection.close() 34 | } 35 | }) 36 | } 37 | 38 | connectToMongoDB() 39 | -------------------------------------------------------------------------------- /13-Insert-and-Delete-Many/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPath } = require('./config.json') 3 | 4 | module.exports = async () => { 5 | await mongoose.connect(mongoPath, { 6 | useNewUrlParser: true, 7 | useUnifiedTopology: true, 8 | useFindAndModify: false, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /13-Insert-and-Delete-Many/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /13-Insert-and-Delete-Many/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const userSchema = mongoose.Schema( 9 | { 10 | email: reqString, 11 | username: reqString, 12 | password: reqString, 13 | messages: { 14 | type: Number, 15 | default: 5, 16 | min: 0, 17 | max: 10, 18 | }, 19 | nameHistory: [String], 20 | }, 21 | { 22 | timestamps: true, 23 | } 24 | ) 25 | 26 | module.exports = mongoose.model('users', userSchema) 27 | -------------------------------------------------------------------------------- /14-Nested-Objects/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | const email = 'test@test.com' 10 | 11 | // Insert nested documents 12 | await new userSchema({ 13 | email, 14 | username: 'testing', 15 | password: 'pass', 16 | messages: [ 17 | { 18 | userId: email, 19 | text: 'hello world', 20 | }, 21 | { 22 | userId: email, 23 | text: 'hello world 2', 24 | }, 25 | { 26 | userId: email, 27 | text: 'hello world 3', 28 | }, 29 | ], 30 | }).save() 31 | 32 | // Search for nested documents 33 | const results = await userSchema.findOne({ 34 | 'messages.text': 'hello world 2', 35 | }) 36 | 37 | console.log('RESULTS:', results) 38 | } finally { 39 | mongoose.connection.close() 40 | } 41 | }) 42 | } 43 | 44 | connectToMongoDB() 45 | -------------------------------------------------------------------------------- /14-Nested-Objects/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPath } = require('./config.json') 3 | 4 | module.exports = async () => { 5 | await mongoose.connect(mongoPath, { 6 | useNewUrlParser: true, 7 | useUnifiedTopology: true, 8 | useFindAndModify: false, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /14-Nested-Objects/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /14-Nested-Objects/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const messageSchema = mongoose.Schema( 9 | { 10 | userId: reqString, 11 | text: reqString, 12 | }, 13 | { 14 | timestamps: true, 15 | } 16 | ) 17 | 18 | const userSchema = mongoose.Schema( 19 | { 20 | email: reqString, 21 | username: reqString, 22 | password: reqString, 23 | messages: [messageSchema], 24 | nameHistory: [String], 25 | }, 26 | { 27 | timestamps: true, 28 | } 29 | ) 30 | 31 | module.exports = mongoose.model('users', userSchema) 32 | -------------------------------------------------------------------------------- /15-Rename-and-Unset-Operators/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | await userSchema.updateMany( 10 | {}, 11 | { 12 | $unset: { 13 | pass: '', 14 | }, 15 | } 16 | ) 17 | } finally { 18 | mongoose.connection.close() 19 | } 20 | }) 21 | } 22 | 23 | connectToMongoDB() 24 | -------------------------------------------------------------------------------- /15-Rename-and-Unset-Operators/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPath } = require('./config.json') 3 | 4 | module.exports = async () => { 5 | await mongoose.connect(mongoPath, { 6 | useNewUrlParser: true, 7 | useUnifiedTopology: true, 8 | useFindAndModify: false, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /15-Rename-and-Unset-Operators/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /15-Rename-and-Unset-Operators/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const messageSchema = mongoose.Schema( 9 | { 10 | userId: reqString, 11 | text: reqString, 12 | }, 13 | { 14 | timestamps: true, 15 | } 16 | ) 17 | 18 | const userSchema = mongoose.Schema( 19 | { 20 | email: reqString, 21 | username: reqString, 22 | pass: reqString, 23 | messages: [messageSchema], 24 | nameHistory: [String], 25 | }, 26 | { 27 | timestamps: true, 28 | } 29 | ) 30 | 31 | module.exports = mongoose.model('users', userSchema) 32 | -------------------------------------------------------------------------------- /16-Conditional-Searches/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | // Creating a test user 10 | // await new userSchema({ 11 | // email: 'testing@test.com', 12 | // username: 'Test', 13 | // pass: 'abc123', 14 | // level: 10, 15 | // }).save() 16 | 17 | // Search for users with a level > 10 18 | const results = await userSchema.find({ 19 | level: { 20 | $exists: false, 21 | }, 22 | }) 23 | 24 | console.log('RESULTS:', results) 25 | } finally { 26 | mongoose.connection.close() 27 | } 28 | }) 29 | } 30 | 31 | connectToMongoDB() 32 | -------------------------------------------------------------------------------- /16-Conditional-Searches/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPath } = require('./config.json') 3 | 4 | module.exports = async () => { 5 | await mongoose.connect(mongoPath, { 6 | useNewUrlParser: true, 7 | useUnifiedTopology: true, 8 | useFindAndModify: false, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /16-Conditional-Searches/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /16-Conditional-Searches/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const messageSchema = mongoose.Schema( 9 | { 10 | userId: reqString, 11 | text: reqString, 12 | }, 13 | { 14 | timestamps: true, 15 | } 16 | ) 17 | 18 | const userSchema = mongoose.Schema( 19 | { 20 | email: reqString, 21 | username: reqString, 22 | pass: reqString, 23 | level: Number, 24 | messages: [messageSchema], 25 | nameHistory: [String], 26 | }, 27 | { 28 | timestamps: true, 29 | } 30 | ) 31 | 32 | module.exports = mongoose.model('users', userSchema) 33 | -------------------------------------------------------------------------------- /17-ElemMatch-All-and-Size/index.js: -------------------------------------------------------------------------------- 1 | const mongo = require('./mongo') 2 | const userSchema = require('./schemas/user-schema') 3 | 4 | const connectToMongoDB = async () => { 5 | await mongo().then(async (mongoose) => { 6 | try { 7 | console.log('Connected to mongodb!') 8 | 9 | // await new userSchema({ 10 | // email: 'test@test.com', 11 | // username: 'User', 12 | // pass: 'abc123', 13 | // testScore: [85, 90, 77], 14 | // }).save() 15 | 16 | let result 17 | 18 | // result = await userSchema.find({ 19 | // testScore: { 20 | // $all: [100], 21 | // }, 22 | // }) 23 | 24 | // result = await userSchema.find({ 25 | // testScore: { 26 | // $size: 4, 27 | // }, 28 | // }) 29 | 30 | result = await userSchema.find({ 31 | testScore: { 32 | $elemMatch: { 33 | $lte: 80, 34 | }, 35 | }, 36 | }) 37 | 38 | console.log('RESULT:', result) 39 | } finally { 40 | mongoose.connection.close() 41 | } 42 | }) 43 | } 44 | 45 | connectToMongoDB() 46 | -------------------------------------------------------------------------------- /17-ElemMatch-All-and-Size/mongo.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | const { mongoPath } = require('./config.json') 3 | 4 | module.exports = async () => { 5 | await mongoose.connect(mongoPath, { 6 | useNewUrlParser: true, 7 | useUnifiedTopology: true, 8 | useFindAndModify: false, 9 | }) 10 | 11 | return mongoose 12 | } 13 | -------------------------------------------------------------------------------- /17-ElemMatch-All-and-Size/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2-Connecting-to-MongoDB", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mongoose": "^5.9.25" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /17-ElemMatch-All-and-Size/schemas/user-schema.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose') 2 | 3 | const reqString = { 4 | type: String, 5 | required: true, 6 | } 7 | 8 | const messageSchema = mongoose.Schema( 9 | { 10 | userId: reqString, 11 | text: reqString, 12 | }, 13 | { 14 | timestamps: true, 15 | } 16 | ) 17 | 18 | const userSchema = mongoose.Schema( 19 | { 20 | email: reqString, 21 | username: reqString, 22 | pass: reqString, 23 | level: Number, 24 | messages: [messageSchema], 25 | nameHistory: [String], 26 | testScore: [Number], 27 | }, 28 | { 29 | timestamps: true, 30 | } 31 | ) 32 | 33 | module.exports = mongoose.model('users', userSchema) 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | If you need help with anything then feel free to ask in the Worn Off Keys Discord server: 2 | 3 | http://wornoffkeys.com/discord?from=mongo-repo 4 | 5 | If you found my content helpful then consider becoming a Patron. You'll receive cool perks and will help support me as a content creator for just a few dollars a month: 6 | 7 | http://wornoffkeys.com/patreon?from=mongo-repo 8 | --------------------------------------------------------------------------------