Skip to content

Commit 372b1c1

Browse files
authored
Development (mongodb#23)
* Commit performed using Copy Push Files action * remove old files --------- Co-authored-by: MongoCaleb <[email protected]>
1 parent 6c4417c commit 372b1c1

13 files changed

+90
-163
lines changed

snippets/functions/mongodb-crud/InsertMany.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

snippets/functions/mongodb-crud/InsertOne.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

snippets/functions/mongodb-crud/DeleteMany.js renamed to snippets/functions/mongodb-crud/crud_DeleteMany.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
exports = async function(productIdToDelete){
2-
// Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab)
1+
exports = async function(args){
2+
console.log(JSON.stringify(args));
3+
34
var serviceName = "mongodb-atlas";
4-
5-
// Update these to reflect your db/collection
65
var dbName = "sample_supplies";
76
var collName = "sales";
87

9-
// Get a collection from the context
108
var collection = context.services.get(serviceName).db(dbName).collection(collName);
119

1210
try {
13-
const query = { "product": productIdToDelete }
14-
deleteResult = await collection.deleteMany(query);
11+
deleteResult = await collection.deleteMany(args);
1512
return deleteResult["deletedCount"];
16-
1713
} catch(err) {
1814
console.log("Failed to delete item: ", err.message);
1915
return { error: err.message };

snippets/functions/mongodb-crud/deleteOne.js renamed to snippets/functions/mongodb-crud/crud_DeleteOne.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
exports = async function(objectIdToDelete){
2-
// Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab)
1+
exports = async function(deleteFilter){
32
var serviceName = "mongodb-atlas";
4-
5-
// Update these to reflect your db/collection
63
var dbName = "sample_supplies";
74
var collName = "sales";
85

9-
// Get a collection from the context
6+
// example deleteFilter:
7+
// { _id: ObjectId("123")}
8+
109
var collection = context.services.get(serviceName).db(dbName).collection(collName);
1110

1211
try {
13-
const query = { "_id": objectIdToDelete }
14-
deleteResult = await collection.deleteOne(query);
12+
deleteResult = await collection.deleteOne(deleteFilter);
1513
return deleteResult["deletedCount"];
1614

1715
} catch(err) {

snippets/functions/mongodb-crud/Find.js renamed to snippets/functions/mongodb-crud/crud_Find.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,10 @@ exports = async function(args){
99
// Get a collection from the context
1010
var collection = context.services.get(serviceName).db(dbName).collection(collName);
1111

12-
const query = { "price": { "$gt": 29.99 } };
13-
const projection = {
14-
"title": 1,
15-
"quantity": 1,
16-
}
17-
1812
try {
19-
doc = await collection.find(query, projection);
20-
return doc;
13+
docs = await collection.find(args);
14+
console.log(JSON.stringify(docs));
15+
return docs;
2116

2217
} catch(err) {
2318
console.log("Failed to find item: ", err.message);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
exports = async function(saleId){
2+
var serviceName = "mongodb-atlas";
3+
var dbName = "sample_supplies";
4+
var collName = "sales";
5+
6+
var collection = context.services.get(serviceName).db(dbName).collection(collName);
7+
const query = { "_id": new BSON.ObjectId(saleId) };
8+
try {
9+
doc = await collection.findOne(query);
10+
console.log('found:', JSON.stringify(doc));
11+
return doc;
12+
13+
} catch(err) {
14+
console.log("Failed to find item: ", err.message);
15+
return { error: err.message };
16+
}
17+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
exports = async function(args){
2+
// Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab)
3+
var serviceName = "mongodb-atlas";
4+
5+
// Update these to reflect your db/collection
6+
var dbName = "sample_supplies";
7+
var collName = "sales";
8+
9+
// Get a collection from the context
10+
var collection = context.services.get(serviceName).db(dbName).collection(collName);
11+
12+
try {
13+
insertResult = await collection.insertMany(args);
14+
console.log('insertResult ids:', insertResult.insertedIds);
15+
console.log('insertResult length:', insertResult.insertedIds.length);
16+
console.log('returning', JSON.stringify(insertResult));
17+
return insertResult;
18+
19+
} catch(err) {
20+
console.log("Failed to insert item(s): ", err.message);
21+
return { error: err.message };
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exports = async function(objectIdToDelete){
1+
exports = async function(args){
22
// Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab)
33
var serviceName = "mongodb-atlas";
44

@@ -10,12 +10,12 @@ exports = async function(objectIdToDelete){
1010
var collection = context.services.get(serviceName).db(dbName).collection(collName);
1111

1212
try {
13-
const query = { "_id": objectIdToDelete }
14-
deleteResult = await collection.deleteOne(query);
15-
return deleteResult["deletedCount"];
13+
// Execute an InsertOne in MongoDB
14+
insertResult = await collection.insertOne(args);
15+
return insertResult;
1616

1717
} catch(err) {
18-
console.log("Failed to delete item: ", err.message);
18+
console.log("Failed to insert item: ", err.message);
1919
return { error: err.message };
2020
}
2121
};

snippets/functions/mongodb-crud/FindOne.js renamed to snippets/functions/mongodb-crud/crud_Project.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
exports = async function(productId){
2-
// Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab)
1+
exports = async function(saleId, projection={}){
32
var serviceName = "mongodb-atlas";
4-
5-
// Update these to reflect your db/collection
63
var dbName = "sample_supplies";
74
var collName = "sales";
85

9-
// Get a collection from the context
106
var collection = context.services.get(serviceName).db(dbName).collection(collName);
11-
12-
const query = { "productId": productId };
13-
const projection = {
14-
"title": 1,
15-
"quantity": 1,
16-
}
17-
7+
const query = { "_id": new BSON.ObjectId(saleId) };
8+
// An example of the projection object that might be passed in:
9+
/*const projection = {
10+
_id:0,
11+
storeLocation:1,
12+
items: 1
13+
}*/
1814
try {
1915
doc = await collection.findOne(query, projection);
16+
console.log('found:', JSON.stringify(doc));
2017
return doc;
2118

2219
} catch(err) {

snippets/functions/mongodb-crud/Replace.js renamed to snippets/functions/mongodb-crud/crud_Replace.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
exports = async function(productId){
1+
exports = async function(productId, replacement){
2+
console.log(productId);
3+
console.log(replacement);
24
// Find the name of the MongoDB service you want to use (see "Linked Data Sources" tab)
35
var serviceName = "mongodb-atlas";
46

@@ -9,15 +11,14 @@ exports = async function(productId){
911
// Get a collection from the context
1012
var collection = context.services.get(serviceName).db(dbName).collection(collName);
1113

12-
const query = { "productId": productId };
13-
14-
const replacement = {
15-
"name": "blocks",
16-
"price": 20.99,
17-
"category": "toys"
18-
};
14+
const query = { "_id": BSON.ObjectId(productId) };
15+
// an example replacement ojbect:
16+
/*const replacement = {
17+
"storeLocation": "East Appleton",
18+
"couponUsed": true,
19+
};*/
1920

20-
const options = { "returnNewDocument": false };
21+
const options = { "returnNewDocument": true };
2122

2223
try {
2324
return await collection.findOneAndReplace(query, replacement, options);

0 commit comments

Comments
 (0)