-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from mongodb-developer/aggregation
Add aggregation
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const { MongoClient } = require('mongodb'); | ||
|
||
async function main() { | ||
/** | ||
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster. | ||
* See http://bit.ly/NodeDocs_lauren for more details | ||
*/ | ||
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority"; | ||
|
||
/** | ||
* The Mongo Client you will use to interact with your database | ||
* See bit.ly/Node_MongoClient for more details | ||
*/ | ||
const client = new MongoClient(uri); | ||
|
||
try { | ||
// Connect to the MongoDB cluster | ||
await client.connect(); | ||
|
||
// Make the appropriate DB calls | ||
|
||
// Print the 10 cheapest suburbs in the Sydney, Australia market | ||
await printCheapestSuburbs(client, "Australia", "Sydney", 10); | ||
|
||
} finally { | ||
// Close the connection to the MongoDB cluster | ||
await client.close(); | ||
} | ||
} | ||
|
||
main().catch(console.error); | ||
|
||
/** | ||
* Print the cheapest suburbs for a given market | ||
* @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database | ||
* @param {String} country The country for the given market | ||
* @param {String} market The market you want to search | ||
* @param {number} maxNumberToPrint The maximum number of suburbs to print | ||
*/ | ||
async function printCheapestSuburbs(client, country, market, maxNumberToPrint) { | ||
const pipeline = [ | ||
{ | ||
'$match': { | ||
'bedrooms': 1, | ||
'address.country': country, | ||
'address.market': market, | ||
'address.suburb': { | ||
'$exists': 1, | ||
'$ne': '' | ||
}, | ||
'room_type': 'Entire home/apt' | ||
} | ||
}, { | ||
'$group': { | ||
'_id': '$address.suburb', | ||
'averagePrice': { | ||
'$avg': '$price' | ||
} | ||
} | ||
}, { | ||
'$sort': { | ||
'averagePrice': 1 | ||
} | ||
}, { | ||
'$limit': maxNumberToPrint | ||
} | ||
]; | ||
|
||
// See http://bit.ly/Node_aggregate for the aggregate() docs | ||
const aggCursor = client.db("sample_airbnb").collection("listingsAndReviews").aggregate(pipeline); | ||
|
||
await aggCursor.forEach(airbnbListing => { | ||
console.log(`${airbnbListing._id}: ${airbnbListing.averagePrice}`); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { MongoClient } = require('mongodb'); | ||
|
||
async function main() { | ||
/** | ||
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster. | ||
* See http://bit.ly/NodeDocs_lauren for more details | ||
*/ | ||
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority"; | ||
|
||
/** | ||
* The Mongo Client you will use to interact with your database | ||
* See bit.ly/Node_MongoClient for more details | ||
*/ | ||
const client = new MongoClient(uri); | ||
|
||
try { | ||
// Connect to the MongoDB cluster | ||
await client.connect(); | ||
|
||
// Make the appropriate DB calls | ||
|
||
} finally { | ||
// Close the connection to the MongoDB cluster | ||
await client.close(); | ||
} | ||
} | ||
|
||
main().catch(console.error); | ||
|
||
// Add functions that make DB calls here |