From 0d9fc40397acdf4f4b9547029bb7fb492da18f52 Mon Sep 17 00:00:00 2001 From: MongoCaleb Date: Tue, 16 Jul 2024 18:58:28 +0000 Subject: [PATCH] Commit performed using Copy Push Files action --- .../functions/mongodb-crud/crud_DeleteMany.js | 48 ++++++++++++++++ .../functions/mongodb-crud/crud_DeleteOne.js | 49 ++++++++++++++++ snippets/functions/mongodb-crud/crud_Find.js | 49 ++++++++++++++++ .../functions/mongodb-crud/crud_FindOne.js | 56 ++++++++++++++++++ .../functions/mongodb-crud/crud_InsertMany.js | 47 +++++++++++++++ .../functions/mongodb-crud/crud_InsertOne.js | 45 +++++++++++++++ .../functions/mongodb-crud/crud_Project.js | 49 ++++++++++++++++ .../functions/mongodb-crud/crud_Replace.js | 49 ++++++++++++++++ .../functions/mongodb-crud/crud_UpdateMany.js | 57 +++++++++++++++++++ .../functions/mongodb-crud/crud_UpdateOne.js | 54 ++++++++++++++++++ 10 files changed, 503 insertions(+) create mode 100644 snippets/functions/mongodb-crud/crud_DeleteMany.js create mode 100644 snippets/functions/mongodb-crud/crud_DeleteOne.js create mode 100644 snippets/functions/mongodb-crud/crud_Find.js create mode 100644 snippets/functions/mongodb-crud/crud_FindOne.js create mode 100644 snippets/functions/mongodb-crud/crud_InsertMany.js create mode 100644 snippets/functions/mongodb-crud/crud_InsertOne.js create mode 100644 snippets/functions/mongodb-crud/crud_Project.js create mode 100644 snippets/functions/mongodb-crud/crud_Replace.js create mode 100644 snippets/functions/mongodb-crud/crud_UpdateMany.js create mode 100644 snippets/functions/mongodb-crud/crud_UpdateOne.js diff --git a/snippets/functions/mongodb-crud/crud_DeleteMany.js b/snippets/functions/mongodb-crud/crud_DeleteMany.js new file mode 100644 index 0000000..532481d --- /dev/null +++ b/snippets/functions/mongodb-crud/crud_DeleteMany.js @@ -0,0 +1,48 @@ +exports = async function(changeEvent){ + // Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab) + var serviceName = "mongodb-atlas"; + + // Update these to reflect your db/collection + var dbName = "sample_supplies"; + var collName = "sales"; + + // Get a collection from the context + var collection = context.services.get(serviceName).db(dbName).collection(collName); + + const deleteFilter = { "storeLocation": changeEvent.fullDocument.storeLocation }; + + try { + deleteResult = await collection.deleteMany(deleteFilter); + return deleteResult["deletedCount"]; + } catch(err) { + console.log("Failed to delete item: ", err.message); + return { error: err.message }; + } +}; + +// In the Testing Console tab, paste the code below and click Run: +/* +exports({ + _id: {_data: '62548f79e7f11292792497cc' }, + operationType: 'insert', + clusterTime: { + "$timestamp": { + t: 1649712420, + i:6 + } + }, + ns: { + db: 'engineering', + coll: 'users' + }, + documentKey: { + storeLocation: 'East Appleton', + _id: "62548f79e7f11292792497cc" + }, + fullDocument: { + _id: "599af247bb69cd89961c986d", + storeLocation: 'East Appleton', + couponUsed: false + } +}) +*/ \ No newline at end of file diff --git a/snippets/functions/mongodb-crud/crud_DeleteOne.js b/snippets/functions/mongodb-crud/crud_DeleteOne.js new file mode 100644 index 0000000..56afd1c --- /dev/null +++ b/snippets/functions/mongodb-crud/crud_DeleteOne.js @@ -0,0 +1,49 @@ +exports = async function(changeEvent){ + console.log(JSON.stringify(changeEvent)); + + // Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab) + var serviceName = "mongodb-atlas"; + + // Update these to reflect your db/collection + var dbName = "sample_supplies"; + var collName = "sales"; + + // Get a collection from the context + var collection = context.services.get(serviceName).db(dbName).collection(collName); + + const deleteFilter = { "_id": changeEvent._id._data }; + try { + deleteResult = await collection.deleteOne(deleteFilter); + return deleteResult["deletedCount"]; + } catch(err) { + console.log("Failed to delete item: ", err.message); + return { error: err.message }; + } +}; + +// In the Testing Console tab, paste the code below and click Run: +/* +exports({ + _id: {_data: '62548f79e7f11292792497cc' }, + operationType: 'insert', + clusterTime: { + "$timestamp": { + t: 1649712420, + i:6 + } + }, + ns: { + db: 'engineering', + coll: 'users' + }, + documentKey: { + storeLocation: 'East Appleton', + _id: "62548f79e7f11292792497cc" + }, + fullDocument: { + _id: "599af247bb69cd89961c986d", + storeLocation: 'East Appleton', + couponUsed: false + } +}) +*/ \ No newline at end of file diff --git a/snippets/functions/mongodb-crud/crud_Find.js b/snippets/functions/mongodb-crud/crud_Find.js new file mode 100644 index 0000000..e46a93d --- /dev/null +++ b/snippets/functions/mongodb-crud/crud_Find.js @@ -0,0 +1,49 @@ +exports = async function(changeEvent){ + // Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab) + var serviceName = "mongodb-atlas"; + + // Update these to reflect your db/collection + var dbName = "sample_supplies"; + var collName = "sales"; + + // Get a collection from the context + var collection = context.services.get(serviceName).db(dbName).collection(collName); + + // To test this example, uncomment the following line: + // await collection.insertOne({"storeLocation":changeEvent.fullDocument.storeLocation, "couponUsed":true}); + + try { + findResults = await collection.find({storeLocation: changeEvent.fullDocument.storeLocation}); + return findResults; + } catch(err) { + console.log("Failed to find item: ", err.message); + return { error: err.message }; + } +} + +// In the Testing Console tab, paste the code below and click Run: +/* +exports({ + _id: {_data: '599af247bb69cd89961c986d' }, + operationType: 'insert', + clusterTime: { + "$timestamp": { + t: 1649712420, + i:6 + } + }, + ns: { + db: 'engineering', + coll: 'users' + }, + documentKey: { + storeLocation: 'East Appleton', + _id: "599af247bb69cd89961c986d" + }, + fullDocument: { + _id: "599af247bb69cd89961c986d", + storeLocation: 'East Appleton', + couponUsed: false + } +}); +*/ \ No newline at end of file diff --git a/snippets/functions/mongodb-crud/crud_FindOne.js b/snippets/functions/mongodb-crud/crud_FindOne.js new file mode 100644 index 0000000..893012b --- /dev/null +++ b/snippets/functions/mongodb-crud/crud_FindOne.js @@ -0,0 +1,56 @@ +exports = async function(changeEvent){ + // Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab) + var serviceName = "mongodb-atlas"; + + // Update these to reflect your db/collection + var dbName = "sample_supplies"; + var collName = "sales"; + + // Get a collection from the context + var collection = context.services.get(serviceName).db(dbName).collection(collName); + + // To test this example, uncomment the following line: + // collection.updateOne({_id:"599af247bb69cd89961c986d", "storeLocation":"East Appleton", "couponUsed":false}, {upsert:true}); + + const query = { "_id": changeEvent._id._data }; + + try { + doc = await collection.findOne(query); + return doc; + + } catch(err) { + console.log("Failed to find item: ", err.message); + return { error: err.message }; + } +}; + +// In the Testing Console tab, paste the code below and click Run: +/* +exports({ + _id: {_data: '599af247bb69cd89961c986d' }, + operationType: 'insert', + clusterTime: { + "$timestamp": { + t: 1649712420, + i:6 + } + }, + ns: { + db: 'engineering', + coll: 'users' + }, + documentKey: { + storeLocation: 'East Appleton', + _id: { + "$oid": "599af247bb69cd89961c986d" + } + }, + fullDocument: { + _id: { + "$oid": "599af247bb69cd89961c986d" + }, + storeLocation: 'East Appleton', + couponUsed: false + } +}); +*/ diff --git a/snippets/functions/mongodb-crud/crud_InsertMany.js b/snippets/functions/mongodb-crud/crud_InsertMany.js new file mode 100644 index 0000000..1c66e93 --- /dev/null +++ b/snippets/functions/mongodb-crud/crud_InsertMany.js @@ -0,0 +1,47 @@ +exports = async function (changeEvent) { + var collection = context.services + .get("mongodb-atlas") + .db("sample_supplies") + .collection("sales"); + + try { + var result = await collection.insertMany([ + {"storeLocation":changeEvent.fullDocument.storeLocation, "items":changeEvent.fullDocument.items}, + ]); + return result; + } catch (err) { + console.log("Failed to insert items: ", err.message); + return { error: err.message }; + } +} + +// In the Testing Console tab, paste the code below and click Run: +/* +exports({ + _id: {_data: '62548f79e7f11292792497cc' }, + operationType: 'insert', + clusterTime: { + "$timestamp": { + t: 1649712420, + i:6 + } + }, + ns: { + db: 'engineering', + coll: 'users' + }, + documentKey: { + storeLocation: 'East Appleton', + _id: { + "$oid": "62548f79e7f11292792497cc" + } + }, + fullDocument: { + _id: { + "$oid": "599af247bb69cd89961c986d" + }, + storeLocation: 'East Appleton', + items: ["envelopes"] + } +}) +*/ \ No newline at end of file diff --git a/snippets/functions/mongodb-crud/crud_InsertOne.js b/snippets/functions/mongodb-crud/crud_InsertOne.js new file mode 100644 index 0000000..f5df3d8 --- /dev/null +++ b/snippets/functions/mongodb-crud/crud_InsertOne.js @@ -0,0 +1,45 @@ +exports = async function (changeEvent) { + + console.log(JSON.stringify(changeEvent)); + + var collection = context.services + .get("mongodb-atlas") + .db("sample_supplies") + .collection("sales"); + + try { + var result = await collection.insertOne({"storeLocation":changeEvent.fullDocument.storeLocation, + "items":changeEvent.fullDocument.items}); + return result; + } catch (err) { + console.log("Failed to insert item: ", err.message); + return { error: err.message }; + } +} + +// In the Testing Console tab, paste the code below and click Run: +/* +exports({ + _id: {_data: '62548f79e7f11292792497cc' }, + operationType: 'insert', + clusterTime: { + "$timestamp": { + t: 1649712420, + i:6 + } + }, + ns: { + db: 'engineering', + coll: 'users' + }, + documentKey: { + storeLocation: 'East Appleton', + _id: "62548f79e7f11292792497cc" + }, + fullDocument: { + _id: "599af247bb69cd89961c986d", + storeLocation: 'East Appleton', + items: ["envelopes"] + } +}) +*/ \ No newline at end of file diff --git a/snippets/functions/mongodb-crud/crud_Project.js b/snippets/functions/mongodb-crud/crud_Project.js new file mode 100644 index 0000000..6a20fc7 --- /dev/null +++ b/snippets/functions/mongodb-crud/crud_Project.js @@ -0,0 +1,49 @@ +exports = async function(changeEvent){ + var serviceName = "mongodb-atlas"; + var dbName = "sample_supplies"; + var collName = "sales"; + + var collection = context.services.get(serviceName).db(dbName).collection(collName); + const query = { "_id": changeEvent.documentKey._id }; + + const projection = { + _id:0, + storeLocation:1, + items: 1 + } + + try { + doc = await collection.findOne(query, projection); + return doc; + } catch(err) { + console.log("Failed to find item: ", err.message); + return { error: err.message }; + } +}; + +// In the Testing Console tab, paste the code below and click Run: +/* +exports({ + _id: {_data: '62548f79e7f11292792497cc' }, + operationType: 'insert', + clusterTime: { + "$timestamp": { + t: 1649712420, + i:6 + } + }, + ns: { + db: 'engineering', + coll: 'users' + }, + documentKey: { + storeLocation: 'East Appleton', + _id: '62548f79e7f11292792497cc' + }, + fullDocument: { + _id: "599af247bb69cd89961c986d", + storeLocation: 'East Appleton', + items: ["envelopes"] + } +}) +*/ \ No newline at end of file diff --git a/snippets/functions/mongodb-crud/crud_Replace.js b/snippets/functions/mongodb-crud/crud_Replace.js new file mode 100644 index 0000000..227493d --- /dev/null +++ b/snippets/functions/mongodb-crud/crud_Replace.js @@ -0,0 +1,49 @@ +exports = async function (changeEvent) { + var collection = context.services + .get("mongodb-atlas") + .db("sample_supplies") + .collection("sales"); + + // To test the above example, insert the following document into your collection: + //await collection.insertOne({"storeLocation":"East Appleton","couponUsed":false}, {upsert:true}); + + const query = { storeLocation: changeEvent.fullDocument.storeLocation }; + + const replacement = { + storeLocation: "Orangeville", + couponUsed: false, + }; + + const options = { returnNewDocument: true }; + + try { + return await collection.findOneAndReplace(query, replacement, options); + } catch (err) { + console.log("Failed to replace item: ", err.message); + return { error: err.message }; + } +}; + +// In the Testing Console tab, paste the code below and click Run: +/* +exports({ + _id: {_data: '62548f79e7f11292792497cc' }, + operationType: 'insert', + clusterTime: { + "$timestamp": { + t: 1649712420, + i:6 + } + }, + ns: { + db: 'engineering', + coll: 'users' + }, + documentKey: { + _id: "62548f79e7f11292792497cc" + }, + fullDocument: { + _id: "599af247bb69cd89961c986d", + storeLocation: 'East Appleton' + } +})*/ \ No newline at end of file diff --git a/snippets/functions/mongodb-crud/crud_UpdateMany.js b/snippets/functions/mongodb-crud/crud_UpdateMany.js new file mode 100644 index 0000000..73465ab --- /dev/null +++ b/snippets/functions/mongodb-crud/crud_UpdateMany.js @@ -0,0 +1,57 @@ +exports = async function (changeEvent) { + var collection = context.services + .get("mongodb-atlas") + .db("sample_supplies") + .collection("sales"); + + // To test this example, uncomment the following line: + // collection.updateOne({"storeLocation":"East Appleton","couponUsed":false}) + + const query = { storeLocation: changeEvent.fullDocument.storeLocation }; + + // Example update filter: + const updateFilter = { + "$set": { + "storeLocation": "Langley", + "purchaseMethod" : "credit" + } + }; + + const options = { "upsert": true }; + + try { + updateResult = await collection.updateMany(query, updateFilter, options); + return updateResult; + + } catch(err) { + console.log("Failed to update item(s): ", err.message); + return { error: err.message }; + } +} + +// In the Testing Console tab, paste the code below and click Run: +/* +exports({ + _id: {_data: '62548f79e7f11292792497cc' }, + operationType: 'insert', + clusterTime: { + "$timestamp": { + t: 1649712420, + i:6 + } + }, + ns: { + db: 'engineering', + coll: 'users' + }, + documentKey: { + storeLocation: 'East Appleton', + _id: "62548f79e7f11292792497cc" + }, + fullDocument: { + _id: "599af247bb69cd89961c986d", + storeLocation: 'East Appleton', + couponUsed: false + } +}) +*/ \ No newline at end of file diff --git a/snippets/functions/mongodb-crud/crud_UpdateOne.js b/snippets/functions/mongodb-crud/crud_UpdateOne.js new file mode 100644 index 0000000..aeb1e2a --- /dev/null +++ b/snippets/functions/mongodb-crud/crud_UpdateOne.js @@ -0,0 +1,54 @@ +exports = async function (changeEvent) { + + console.log(JSON.stringify(changeEvent)); + + var collection = context.services + .get("mongodb-atlas") + .db("sample_supplies") + .collection("sales"); + + // To test this example, uncomment the following line: + // await collection.updateOne({"_id":changeEvent._id._data,"storeLocation":"East Appleton","couponUsed":false}, {upsert:true}); + + const query = { _id: changeEvent._id._data }; + + const updateFields = { + storeLocation: "West Appleton", + couponUsed: true, + }; + + try { + await collection.updateOne(query, updateFields); + return await collection.findOne(query); + } catch (err) { + console.log("Failed to update item: ", err.message); + return { error: err.message }; + } +} + +// In the Testing Console tab, paste the code below and click Run: +/* +exports({ + _id: {_data: '62548f79e7f11292792497cc' }, + operationType: 'insert', + clusterTime: { + "$timestamp": { + t: 1649712420, + i:6 + } + }, + ns: { + db: 'engineering', + coll: 'users' + }, + documentKey: { + storeLocation: 'East Appleton', + _id: "62548f79e7f11292792497cc" + }, + fullDocument: { + _id: "62548f79e7f11292792497cc", + storeLocation: 'East Appleton', + couponUsed: false + } +}) +*/ \ No newline at end of file