Skip to content

Instantly share code, notes, and snippets.

@shekharmayank
Created January 5, 2018 17:26
Show Gist options
  • Save shekharmayank/30839da240983129f1447885cab56fd2 to your computer and use it in GitHub Desktop.
Save shekharmayank/30839da240983129f1447885cab56fd2 to your computer and use it in GitHub Desktop.
learnyoumongo Coding Challenges Solutions
//Solution 9
const url = 'mongodb://localhost:27017/learnyoumongo';
const mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, dbobj){
if(err) throw err;
const lym = dbobj.db('learnyoumongo');
var prices = lym.collection('prices');
prices.aggregate([{
$match: {
size: process.argv[2]
}
},{
$group: {
_id: 'average',
average:{
$avg: '$price'
}
}
}]).toArray(function(err, res){
if(err) throw err;
console.log(Number(+res[0].average).toFixed(2));
});
});
//Solution 8
const url = 'mongodb://localhost:27017/learnyoumongo';
const mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, dbobj){
if(err) throw err;
const lym = dbobj.db('learnyoumongo');
var parrots = lym.collection('parrots');
parrots.count({
age: { $gt: +process.argv[2] }
}, function(err, c){
if(err) throw err;
console.log(c);
});
dbobj.close();
});
//Solution 7
const url = 'mongodb://localhost:27017/learnyoumongo';
const mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, dbobj){
if(err) throw err;
const lym = dbobj.db(process.argv[2]);
var col = lym.collection(process.argv[3]);
col.remove({
_id: process.argv[4]
}, function(err){
if(err) throw err;
})
dbobj.close();
});
//Solution 6
const url = 'mongodb://localhost:27017/learnyoumongo';
const mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, dbobj){
if(err) throw err;
const lym = dbobj.db(process.argv[2]);
var users = lym.collection('users');
users.update({
username: 'tinatime'
}, {
$set: {
age: 40
}
}, function(err, data){
if(err) throw err;
})
dbobj.close();
});
//Solution 5
var url = 'mongodb://localhost:27017/learnyoumongo';
var mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, db) {
if (err) throw err;
// db gives access to the database
const myDb = db.db('learnyoumongo');
var docs = myDb.collection('docs');
var obj = {firstName: process.argv[2], lastName: process.argv[3]};
docs.insert(obj, function(err, res){
if(err) throw err;
console.log(JSON.stringify(obj));
});
db.close();
});
//Solution 4
var url = 'mongodb://localhost:27017/learnyoumongo';
var mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, db) {
if (err) throw err;
// db gives access to the database
const myDb = db.db('learnyoumongo');
var parrots = myDb.collection('parrots');
parrots.find({
age: {
$gt: parseInt(process.argv[2])
}
}, { fields: {
name: 1,
age: 1,
_id: 0
}}).toArray(function(err, documents) {
// Here is where we decide what to do with the query results
if (err) throw err;
console.log(documents);
db.close();
});
});
//Solution 3
var mongo = require('mongodb').MongoClient;
mongo.connect('mongodb://localhost:27017/learnyoumongo', function(err, db) {
if(err) throw err;
const myDb = db.db('learnyoumongo');
var parrots = myDb.collection('parrots');
parrots.find({
age: { $gt: +process.argv[2] }
}).toArray(function(err, docs){
if(err) throw err;
console.log(docs);
db.close();
});
});
@shekharmayank
Copy link
Author

Try yourself first then see here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment