Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5cab381

Browse files
Merge pull request #2 from mongodb-developer/create
Add read.js
2 parents ceeb1c5 + d6ce398 commit 5cab381

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

‎read.js

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /