Skip to content

Instantly share code, notes, and snippets.

@Bilguun132
Last active February 23, 2019 18:51
Show Gist options
  • Select an option

  • Save Bilguun132/62328ee9e2e0ad3bd0a54aebd4eb0c8c to your computer and use it in GitHub Desktop.

Select an option

Save Bilguun132/62328ee9e2e0ad3bd0a54aebd4eb0c8c to your computer and use it in GitHub Desktop.
MongoDemo-Tutorial-Index.js with queries
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/demo', { useNewUrlParser: true })
.then(() => console.log('Connected to MongoDB...'))
.catch((err) => console.error('Could not connect to MongoDB...', err));
//create MovieSchema
const movieSchema = new mongoose.Schema({
name: String,
director: String,
genre: [String],
releaseDate: {
type: Date,
default: Date.now()
},
price: Number
})
//Create Movie Class
//Compile Schema into a Model
const Movie = mongoose.model('Movie', movieSchema);
async function createMovie() {
const avengersMovie = new Movie({
name: 'Avengers',
director: 'Joss Whedon',
genre: ['action, adventure, fantasy'], //NoSQL Db -> Document can be complex object unlike MySQL
price: 10
});
await avengersMovie.save()
console.log(avengersMovie);
const spideyMovie = new Movie({
name: 'Spider-Man: Into the Spider-Verse',
director: 'Peter Ramsey',
genre: ['animation, action, adventure'], //NoSQL Db -> Document can be complex object unlike MySQL
price: 15
});
await spideyMovie.save()
console.log(spideyMovie);
getAllMovies();
getAllMoviesCount();
getMoviesFiltered();
getMoviesCompared();
getMoviesAndOr();
getMoviesRegex();
}
async function getAllMovies() {
//gets all the movies in the database with all the fields
const movies = await Movie.find();
console.log('getting all movies');
console.log(movies);
}
async function getAllMoviesCount() {
const movies = await Movie.find().countDocuments();
console.log('getting all movies count');
console.log(movies);
}
async function getMoviesFiltered() {
//pass filter here, eg only find those director 'Joss Whedon', set limit to 10, sort (1 ascending -1 descending)
//and pick name and genre only
const movies = await Movie
.find({
director: 'Joss Whedon'
})
.limit(10)
.sort({ name: 1 }) //sort ascending
.select('name genre -_id') //pick name genre but not _id
console.log('getting movies filtered');
console.log(movies);
}
async function getMoviesCompared() {
//eq equal
//ne not equal
//gt greater than
//gte greater than or equal to
//lt less than
//lte less than or equal to
//in
//nin not in
const movies = await Movie
.find({
price: { $lte: 10 } //get only those with price <=10
})
.limit(10)
.sort({ name: 1 }) //sort ascending
.select('name genre -_id') //pick name genre but not _id
console.log('getting movies with comparison query');
console.log(movies);
}
async function getMoviesAndOr() {
const movies = await Movie
.find()
.or([{ director: 'Joss Whedon' }, { director: 'Peter Ramsey' }]) //director is either or
.and({ price: { $gte: 10 } }) //and price is greater than 10
.limit(10)
.sort({ name: 1 }) //sort ascending
.select('name genre -_id') //pick name genre but not _id
console.log('getting with logical query');
console.log(movies);
}
async function getMoviesRegex() {
//gets all the movies in the database with all the fields
const movies = await Movie.find({
director: /.*Peter.*/i //contains peter
});
console.log('getting movies with regex expressions');
console.log(movies);
}
createMovie();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment