0 - Roll Back Database
C:\data>mongodump --collection users --db newUser
2018-05-14T17:56:53.475+0300 writing newUser.users to
2018-05-14T17:56:53.515+0300
done dumping newUser.users (844 documents)
done dumping newUser.users (844 documents)
Zero query
C:\>mongo
MongoDB shell version v3.4.2-rc0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.2-rc0
Server has startup warnings:
2018-05-14T01:45:40.133+0300 I CONTROL [initandlisten]
2018-05-14T01:45:40.133+0300 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-05-14T01:45:40.133+0300 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-05-14T01:45:40.134+0300 I CONTROL [initandlisten]
2018-05-14T01:45:40.134+0300 I CONTROL [initandlisten] Hotfix KB2731284 or later update is not installed, will zero-out data files.
2018-05-14T01:45:40.135+0300 I CONTROL [initandlisten]
MongoDB server version: 3.4.2-rc0
Server has startup warnings:
2018-05-14T01:45:40.133+0300 I CONTROL [initandlisten]
2018-05-14T01:45:40.133+0300 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-05-14T01:45:40.133+0300 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-05-14T01:45:40.134+0300 I CONTROL [initandlisten]
2018-05-14T01:45:40.134+0300 I CONTROL [initandlisten] Hotfix KB2731284 or later update is not installed, will zero-out data files.
2018-05-14T01:45:40.135+0300 I CONTROL [initandlisten]
1 - Find the average age in Alaska
var resAverageAgeByAll = db.users.aggregate( {$group: {'_id': true, 'averageAgeByAll': {$avg: '$age'}}}).toArray()
resAverageAgeByAll[0]
{ "_id" : true, "averageAgeByAll" : 30.377224199288257 }
2 - Find the average age of people in the system
var resAverageAgeByAlaska = db.users.aggregate({$match : { 'address': /Alaska/ }}, {$group: { '_id':true, 'averageAgeByAlaska': {$avg: '$age'}}} ).toArray()
resAverageAgeByAlaska[0]
{ "_id" : true, "averageAgeByAlaska" : 31.5 }
3 - Starting from Math.ceil (avg + avg_alaska) (ordinal number of the document in the database) find the first person with a friend named Dennis
var resFirstDennis = db.users.aggregate({$skip : 62} , // some trouble with adding
{$match:{'friends.name': /Dennis/}},
{$group: { '_id': '$name', '_id': '$address' }},
{ $limit : 1 }).toArray()
resFirstDennis[0]
{ "_id" : "147 Brighton Avenue, Gila, Oregon, 8928" }
4 - Find active people from the same state as the previous person and see what kind of fruit they love the most in this state (aggregation)
var resFruit = db.users.aggregate({$match:{ 'address': /Oregon/}}, {$group : { _id:'$favoriteFruit' , lovedFruit:{ $sum: 1}}}, {$sort: {lovedFruit:-1}}, {$limit:1}).toArray()
resFruit[0]
{ "_id" : "strawberry", "lovedFruit" : 5 }
5 - Find the oldest registered user with such a favorite fruit
var resEarlyUser = db.users.aggregate({$match:{ favoriteFruit: resFruit[0]._id}}, {$group: { '_id': '$_id'}}, {$sort:{_id:1}}, {$limit:1}).toArray()
resEarlyUser[0]
{ "_id" : ObjectId("5adf3c1544abaca147cdd34c") }
6 - Add this user your own: {features: 'first apple eater'}
db.users.update({_id:resEarlyUser[0]._id} , {$set: { features: 'first apple eater'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
7 - Remove all strawberry lovers (write the number of remote users)
db.users.remove({favoriteFruit: 'strawberry' } )
WriteResult({ "nRemoved" : 253 })