-
Notifications
You must be signed in to change notification settings - Fork 239
/
changeStreamsTestData.js
134 lines (116 loc) · 4.87 KB
/
changeStreamsTestData.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* This script can be used to create, update, and delete sample data.
* This script is especially helpful when testing change streams.
*/
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
const operaHouseViews = await createListing(client, {
name: "Opera House Views",
summary: "Beautiful apartment with views of the iconic Sydney Opera House",
property_type: "Apartment",
bedrooms: 1,
bathrooms: 1,
beds: 1,
address: {
market: "Sydney",
country: "Australia"
}
});
const privateRoomInLondon = await createListing(client, {
name: "Private room in London",
property_type: "Apartment",
bedrooms: 1,
bathroom: 1
});
const beautifulBeachHouse = await createListing(client, {
name: "Beautiful Beach House",
summary: "Enjoy relaxed beach living in this house with a private beach",
bedrooms: 4,
bathrooms: 2.5,
beds: 7,
last_review: new Date()
});
await updateListing(client, operaHouseViews, { beds: 2 });
await updateListing(client, beautifulBeachHouse, {
address: {
market: "Sydney",
country: "Australia"
}
});
const italianVilla = await createListing(client, {
name: "Italian Villa",
property_type: "Entire home/apt",
bedrooms: 6,
bathrooms: 4,
address: {
market: "Cinque Terre",
country: "Italy"
}
});
const sydneyHarbourHome = await createListing(client, {
name: "Sydney Harbour Home",
bedrooms: 4,
bathrooms: 2.5,
address: {
market: "Sydney",
country: "Australia"
}
});
await deleteListing(client, sydneyHarbourHome);
} finally {
// Close the connection to the MongoDB cluster
await client.close();
}
}
main().catch(console.error);
/**
* Create a new Airbnb listing
* @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database
* @param {Object} newListing The new listing to be added
* @returns {String} The id of the new listing
*/
async function createListing(client, newListing) {
// See http://bit.ly/Node_InsertOne for the insertOne() docs
const result = await client.db("sample_airbnb").collection("listingsAndReviews").insertOne(newListing);
console.log(`New listing created with the following id: ${result.insertedId}`);
return result.insertedId;
}
/**
* Update an Airbnb listing
* @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database
* @param {String} listingId The id of the listing you want to update
* @param {object} updatedListing An object containing all of the properties to be updated for the given listing
*/
async function updateListing(client, listingId, updatedListing) {
// See http://bit.ly/Node_updateOne for the updateOne() docs
const result = await client.db("sample_airbnb").collection("listingsAndReviews").updateOne({ _id: listingId }, { $set: updatedListing });
console.log(`${result.matchedCount} document(s) matched the query criteria.`);
console.log(`${result.modifiedCount} document(s) was/were updated.`);
}
/**
* Delete an Airbnb listing
* @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database
* @param {String} listingId The id of the listing you want to delete
*/
async function deleteListing(client, listingId) {
// See http://bit.ly/Node_deleteOne for the deleteOne() docs
const result = await client.db("sample_airbnb").collection("listingsAndReviews").deleteOne({ _id: listingId });
console.log(`${result.deletedCount} document(s) was/were deleted.`);
}