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 5e452a4

Browse files
Merge pull request #1 from mongodb-developer/create
Add create.js
2 parents b222f09 + 8c7f226 commit 5e452a4

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

‎create.js‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
// Create a single new listing
23+
await createListing(client,
24+
{
25+
name: "Lovely Loft",
26+
summary: "A charming loft in Paris",
27+
bedrooms: 1,
28+
bathrooms: 1
29+
}
30+
);
31+
32+
// Create 3 new listings
33+
await createMultipleListings(client, [
34+
{
35+
name: "Infinite Views",
36+
summary: "Modern home with infinite views from the infinity pool",
37+
property_type: "House",
38+
bedrooms: 5,
39+
bathrooms: 4.5,
40+
beds: 5
41+
},
42+
{
43+
name: "Private room in London",
44+
property_type: "Apartment",
45+
bedrooms: 1,
46+
bathroom: 1
47+
},
48+
{
49+
name: "Beautiful Beach House",
50+
summary: "Enjoy relaxed beach living in this house with a private beach",
51+
bedrooms: 4,
52+
bathrooms: 2.5,
53+
beds: 7,
54+
last_review: new Date()
55+
}
56+
]);
57+
} finally {
58+
// Close the connection to the MongoDB cluster
59+
await client.close();
60+
}
61+
}
62+
63+
main().catch(console.error);
64+
65+
/**
66+
* Create a new Airbnb listing
67+
* @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database
68+
* @param {Object} newListing The new listing to be added
69+
*/
70+
async function createListing(client, newListing){
71+
// See http://bit.ly/Node_InsertOne for the insertOne() docs
72+
result = await client.db("sample_airbnb").collection("listingsAndReviews").insertOne(newListing);
73+
console.log(`New listing created with the following id: ${result.insertedId}`);
74+
}
75+
76+
/**
77+
* Create multiple Airbnb listings
78+
* @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database
79+
* @param {Object[]} newListings The new listings to be added
80+
*/
81+
async function createMultipleListings(client, newListings){
82+
// See http://bit.ly/Node_InsertMany for the insertMany() docs
83+
result = await client.db("sample_airbnb").collection("listingsAndReviews").insertMany(newListings);
84+
85+
console.log(`${result.insertedCount} new listing(s) created with the following id(s):`);
86+
console.log(result.insertedIds);
87+
}

0 commit comments

Comments
(0)

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