Commit 5011e343 authored by Kristian Freeman's avatar Kristian Freeman

mongodb-queries exercise

parent 61078b2d
const connect = require("./utils/connection");
const mongoose = require("mongoose");
const assert = require("assert");
const seed = require("./utils/seed");
// Import the User model from './user'. Make sure to check
// the file for anything that needs to be done!
// A number of functions have been defined, which need
// functionality filled in before they can be used. Each
// function includes an assertion to confirm that the
// query you provide correctly passes the requirements.
async function getAllUsers() {
// Set the variable users to the result of finding
// _all_ the users.
console.log(`Found ${users.length} users`);
assert.equal(users.length, 101, "Expected to find 101 users!");
}
async function getVerifiedUsers() {
// Set the variable users to the result of finding
// all users where verified is true.
console.log(`Found ${users.length} verified users`);
users.forEach(user =>
assert.equal(
user.verified,
true,
"A non-verified user was found in the getVerifiedUsers function"
)
);
}
async function getAdultUsers() {
// Set the variable users to the result of finding
// all users where age is greater than 21.
// Queries of this format are done by setting the field as a key,
// and then using the $gt operator, like so:
//
// Event.find({ user_count: { $gt: 100 } });
console.log(`Found ${users.length} users over 21`);
users.forEach(user =>
assert.equal(
user.age > 21,
true,
"A user under 21 was found in the getAdultUsers function"
)
);
}
async function getUsersWithLongLastNames() {
// More complicated lookups can be done by passing the $where condition
// into .find. Set the value of users to the result of any user where
// name.last.length is greater than 9. For instance, if we were looking
// for an event name, we would do something like...
//
// Event.find({ $where: 'this.name.length > 20' })
console.log(`Found ${users.length} users with long last names`);
users.forEach(user =>
assert.equal(
user.name.last.length > 9,
true,
"A user with a last name shorter than 9 characters was found in the getUsersWithLongLastNames function"
)
);
}
async function getKristian() {
// Find one user in the database by calling .findOne. Lookup the user
// by passing a name object, with first set to "Kristian" and last set to
// "Freeman". Set the value of this function call to user.
console.log(`Found ${user.name.first}!`);
assert.equal(
user.name.first,
"Kristian",
"The user's name in getKristian is incorrect"
);
assert.equal(
user.name.last,
"Freeman",
"The user's name in getKristian is incorrect"
);
assert.equal(user.age, 25, "The user's age in getKristian is incorrect");
}
// Once you've filled in all the queries, you should call them in the
// initialize function below.
async function initialize() {
await connect();
await mongoose.connection.dropDatabase();
await seed();
// Call the defined functions, ensuring that each is prefixed
// with await:
// - getAllUsers
// - getVerifiedUsers
// - getAdultUsers
// - getUsersWithLongLastNames
// - getKristian
// End your code here!
console.log("OK :)");
await mongoose.connection.close();
}
initialize();
{
"name": "mongodb-queries",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"async": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/async/-/async-2.6.1.tgz",
"integrity": "sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ==",
"requires": {
"lodash": "^4.17.10"
}
},
"bluebird": {
"version": "3.5.1",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz",
"integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA=="
},
"bson": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/bson/-/bson-1.1.0.tgz",
"integrity": "sha512-9Aeai9TacfNtWXOYarkFJRW2CWo+dRon+fuLZYJmvLV3+MiUp0bEI6IAZfXEIg7/Pl/7IWlLaDnhzTsD81etQA=="
},
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"requires": {
"ms": "2.0.0"
}
},
"faker": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/faker/-/faker-4.1.0.tgz",
"integrity": "sha1-HkW7vsxndLPBlfrSg1EJxtdIzD8="
},
"kareem": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.0.tgz",
"integrity": "sha512-6hHxsp9e6zQU8nXsP+02HGWXwTkOEw6IROhF2ZA28cYbUk4eJ6QbtZvdqZOdD9YPKghG3apk5eOCvs+tLl3lRg=="
},
"lodash": {
"version": "4.17.11",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz",
"integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg=="
},
"lodash.get": {
"version": "4.4.2",
"resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz",
"integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk="
},
"memory-pager": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.1.0.tgz",
"integrity": "sha512-Mf9OHV/Y7h6YWDxTzX/b4ZZ4oh9NSXblQL8dtPCOomOtZciEHxePR78+uHFLLlsk01A6jVHhHsQZZ/WcIPpnzg==",
"optional": true
},
"mongodb": {
"version": "3.1.8",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.1.8.tgz",
"integrity": "sha512-yNKwYxQ6m00NV6+pMoWoheFTHSQVv1KkSrfOhRDYMILGWDYtUtQRqHrFqU75rmPIY8hMozVft8zdC4KYMWaM3Q==",
"requires": {
"mongodb-core": "3.1.7",
"safe-buffer": "^5.1.2"
}
},
"mongodb-core": {
"version": "3.1.7",
"resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.1.7.tgz",
"integrity": "sha512-YffpSrLmgFNmrvkGx+yX00KyBNk64C0BalfEn6vHHkXtcMUGXw8nxrMmhq5eXPLLlYeBpD/CsgNxE2Chf0o4zQ==",
"requires": {
"bson": "^1.1.0",
"require_optional": "^1.0.1",
"safe-buffer": "^5.1.2",
"saslprep": "^1.0.0"
}
},
"mongoose": {
"version": "5.3.11",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.3.11.tgz",
"integrity": "sha512-LrsatxtUfZAerAxyFiaw/8T8M4T9Ff9+6wFkJwfTVDz9skX+HVOs70QOzTDawz0k0Q0I6qt9xv/ZYQi2Paykgw==",
"requires": {
"async": "2.6.1",
"bson": "~1.1.0",
"kareem": "2.3.0",
"lodash.get": "4.4.2",
"mongodb": "3.1.8",
"mongodb-core": "3.1.7",
"mongoose-legacy-pluralize": "1.0.2",
"mpath": "0.5.1",
"mquery": "3.2.0",
"ms": "2.0.0",
"regexp-clone": "0.0.1",
"safe-buffer": "5.1.2",
"sliced": "1.0.1"
}
},
"mongoose-legacy-pluralize": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz",
"integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ=="
},
"mpath": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/mpath/-/mpath-0.5.1.tgz",
"integrity": "sha512-H8OVQ+QEz82sch4wbODFOz+3YQ61FYz/z3eJ5pIdbMEaUzDqA268Wd+Vt4Paw9TJfvDgVKaayC0gBzMIw2jhsg=="
},
"mquery": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.0.tgz",
"integrity": "sha512-qPJcdK/yqcbQiKoemAt62Y0BAc0fTEKo1IThodBD+O5meQRJT/2HSe5QpBNwaa4CjskoGrYWsEyjkqgiE0qjhg==",
"requires": {
"bluebird": "3.5.1",
"debug": "3.1.0",
"regexp-clone": "0.0.1",
"safe-buffer": "5.1.2",
"sliced": "1.0.1"
}
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"regexp-clone": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-0.0.1.tgz",
"integrity": "sha1-p8LgmJH9vzj7sQ03b7cwA+aKxYk="
},
"require_optional": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz",
"integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==",
"requires": {
"resolve-from": "^2.0.0",
"semver": "^5.1.0"
}
},
"resolve-from": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz",
"integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c="
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"saslprep": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.2.tgz",
"integrity": "sha512-4cDsYuAjXssUSjxHKRe4DTZC0agDwsCqcMqtJAQPzC74nJ7LfAJflAtC1Zed5hMzEQKj82d3tuzqdGNRsLJ4Gw==",
"optional": true,
"requires": {
"sparse-bitfield": "^3.0.3"
}
},
"semver": {
"version": "5.6.0",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz",
"integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg=="
},
"sliced": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/sliced/-/sliced-1.0.1.tgz",
"integrity": "sha1-CzpmK10Ewxd7GSa+qCsD+Dei70E="
},
"sparse-bitfield": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz",
"integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=",
"optional": true,
"requires": {
"memory-pager": "^1.0.2"
}
}
}
}
{
"name": "mongodb-queries",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"faker": "^4.1.0",
"lodash": "^4.17.11",
"mongoose": "^5.3.11"
}
}
const mongoose = require("mongoose");
// Define userSchema by calling the function
// mongoose.Schema, passing in an object where:
// - name is an object, with first and last as Strings
// - description is a string
// - location is a string
// - age is a number
// - verified is a boolean
// - created_at is an object, with a type of Date and
// default set to Date.now
// - updated_at is an object, with a type of Date and
// default set to Date.now
//
// Define User to the value of the function mongoose.model,
// passing in the string "User" and userSchema.
//
// Export User using module.exports.
const mongoose = require("mongoose");
const port = process.env.PORT || 27017;
const url = `mongodb://localhost:${port}/training-mongodb-queries`;
const connect = () =>
mongoose.connect(
url,
{ useNewUrlParser: true }
);
module.exports = connect;
const mongoose = require("mongoose");
const User = require("../user");
const _ = require("lodash");
const faker = require("faker");
// This is the seed function! There's nothing to do here,
// but this function sets up a number of random users, using
// the faker library to pass in fake names, locations, and numbers.
//
// There's a single non-random user (hello!) that you'll look up
// as part of the exercise. Notice that this function is designed
// to be asynchronous, by wrapping the looped User.create call
// with Promise.all, so that we can chain off it using await when
// the function is used.
const seed = async () => {
const arr = _.times(100);
await User.create({
name: { first: "Kristian", last: "Freeman" },
description: "Software engineer and technical trainer",
location: "USA",
age: 25,
verified: true
});
return Promise.all(
arr.map(async () =>
User.create({
name: {
first: faker.name.firstName(),
last: faker.name.lastName()
},
description: faker.lorem.sentences(4),
location: faker.address.country(),
age: faker.random.number(80),
verified: faker.random.boolean()
})
)
);
};
module.exports = seed;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment