Skip to content

Commit

Permalink
Merge branch 'copy-push-files-action-9962554850-upload-functions' int…
Browse files Browse the repository at this point in the history
…o development
  • Loading branch information
MongoCaleb committed Jul 16, 2024
2 parents bbbe1e0 + 0d9fc40 commit 4e0fd17
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 0 deletions.
48 changes: 48 additions & 0 deletions snippets/functions/mongodb-crud/crud_DeleteMany.js
Original file line number Diff line number Diff line change
@@ -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
}
})
*/
49 changes: 49 additions & 0 deletions snippets/functions/mongodb-crud/crud_DeleteOne.js
Original file line number Diff line number Diff line change
@@ -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
}
})
*/
49 changes: 49 additions & 0 deletions snippets/functions/mongodb-crud/crud_Find.js
Original file line number Diff line number Diff line change
@@ -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
}
});
*/
56 changes: 56 additions & 0 deletions snippets/functions/mongodb-crud/crud_FindOne.js
Original file line number Diff line number Diff line change
@@ -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
}
});
*/
47 changes: 47 additions & 0 deletions snippets/functions/mongodb-crud/crud_InsertMany.js
Original file line number Diff line number Diff line change
@@ -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"]
}
})
*/
45 changes: 45 additions & 0 deletions snippets/functions/mongodb-crud/crud_InsertOne.js
Original file line number Diff line number Diff line change
@@ -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"]
}
})
*/
49 changes: 49 additions & 0 deletions snippets/functions/mongodb-crud/crud_Project.js
Original file line number Diff line number Diff line change
@@ -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"]
}
})
*/
Loading

0 comments on commit 4e0fd17

Please sign in to comment.