-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathread.js
107 lines (92 loc) · 4.62 KB
/
read.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const { MongoClient } = require('mongodb');
async function main() {
/**
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
* See https://docs.mongodb.com/drivers/node/ for more details
*/
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/sample_airbnb?retryWrites=true&w=majority";
/**
* The Mongo Client you will use to interact with your database
* See https://mongodb.github.io/node-mongodb-native/3.6/api/MongoClient.html for more details
* In case: '[MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated...'
* pass option { useUnifiedTopology: true } to the MongoClient constructor.
* const client = new MongoClient(uri, {useUnifiedTopology: true})
*/
const client = new MongoClient(uri);
try {
// Connect to the MongoDB cluster
await client.connect();
// Make the appropriate DB calls
// Find the listing named "Infinite Views" that we created in create.js
await findOneListingByName(client, "Infinite Views");
// Find up to 5 listings with at least 4 bedrooms and at least 2 bathrooms
// If you recently ran create.js, a listing named Beautiful Beach House should be included in the results
await findListingsWithMinimumBedroomsBathroomsAndMostRecentReviews(client, {
minimumNumberOfBedrooms: 4,
minimumNumberOfBathrooms: 2,
maximumNumberOfResults: 5
});
} finally {
// Close the connection to the MongoDB cluster
await client.close();
}
}
main().catch(console.error);
/**
* Print an Airbnb listing with the given name
* Note: If more than one listing has the same name, only the first listing the database finds will be printed.
* @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database
* @param {String} nameOfListing The name of the listing you want to find
*/
async function findOneListingByName(client, nameOfListing) {
// See https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#findOne for the findOne() docs
const result = await client.db("sample_airbnb").collection("listingsAndReviews").findOne({ name: nameOfListing });
if (result) {
console.log(`Found a listing in the collection with the name '${nameOfListing}':`);
console.log(result);
} else {
console.log(`No listings found with the name '${nameOfListing}'`);
}
}
/**
* Print Airbnb listings with a minimum number of bedrooms and bathrooms.
* Results will be limited to the designated maximum number of results.
* Results will be sorted by the date of the last review in descending order.
* @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database
* @param {object} queryParams The query params object
* @param {number} queryParams.minimumNumberOfBedrooms The minimum number of bedrooms
* @param {number} queryParams.minimumNumberOfBathrooms The minimum number of bathrooms
* @param {number} queryParams.maximumNumberOfResults The maximum number of Airbnb listings to be printed
*/
async function findListingsWithMinimumBedroomsBathroomsAndMostRecentReviews(client, {
minimumNumberOfBedrooms = 0,
minimumNumberOfBathrooms = 0,
maximumNumberOfResults = Number.MAX_SAFE_INTEGER
} = {}) {
// See https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#find for the find() docs
const cursor = client.db("sample_airbnb").collection("listingsAndReviews")
.find({
bedrooms: { $gte: minimumNumberOfBedrooms },
bathrooms: { $gte: minimumNumberOfBathrooms }
}
)
.sort({ last_review: -1 })
.limit(maximumNumberOfResults);
// Store the results in an array
const results = await cursor.toArray();
// Print the results
if (results.length > 0) {
console.log(`Found listing(s) with at least ${minimumNumberOfBedrooms} bedrooms and ${minimumNumberOfBathrooms} bathrooms:`);
results.forEach((result, i) => {
const date = new Date(result.last_review).toDateString();
console.log();
console.log(`${i + 1}. name: ${result.name}`);
console.log(` _id: ${result._id}`);
console.log(` bedrooms: ${result.bedrooms}`);
console.log(` bathrooms: ${result.bathrooms}`);
console.log(` most recent review date: ${date}`);
});
} else {
console.log(`No listings found with at least ${minimumNumberOfBedrooms} bedrooms and ${minimumNumberOfBathrooms} bathrooms`);
}
}