mongodb bulkwrite nodejs

Solutions on MaxInterview for mongodb bulkwrite nodejs by the best coders in the world

showing results for - "mongodb bulkwrite nodejs"
Silvia
08 Jan 2021
1const { MongoClient } = require("mongodb");
2// Replace the uri string with your MongoDB deployment's connection string.
3const uri =
4  "mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
5const client = new MongoClient(uri);
6async function run() {
7  try {
8    await client.connect();
9    const database = client.db("sample_mflix");
10    const theaters = database.collection("theaters");
11    const result = await theaters.bulkWrite([
12      { insertOne:
13        {
14          "document": {
15            location: {
16              address: { street1: '3 Main St.', city: 'Anchorage', state: 'AK', zipcode: '99501' },
17            }
18          }
19        }
20      },
21      { insertOne:
22        {
23          "document": {
24            location: {
25              address: { street1: '75 Penn Plaza', city: 'New York', state: 'NY', zipcode: '10001' },
26            }
27          }
28        }
29      },
30      { updateMany:
31        {
32          "filter": { "location.address.zipcode" : "44011" },
33          "update": { $set : { "street2" : "25th Floor" } },
34          "upsert": true
35        }
36      },
37      { deleteOne :
38        { "filter" : { "location.address.street1" : "221b Baker St"} }
39      },
40    ]);
41    console.log(result);
42  } finally {
43    await client.close();
44  }
45}
46run().catch(console.dir)