Skip to content

Commit e580350

Browse files
authored
Changing a Sync Schema
* revamp modify schema pages
1 parent fa6899e commit e580350

25 files changed

+460
-241
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"enabled": false
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"api-key": {
3+
"name": "api-key",
4+
"type": "api-key",
5+
"disabled": true
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "mongodb-atlas",
3+
"type": "mongodb-atlas",
4+
"config": {
5+
"clusterName": "Cluster0",
6+
"readPreference": "primary",
7+
"wireProtocolEnabled": false
8+
},
9+
"version": 1
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"values": {}
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"values": {}
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"values": {}
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"values": {}
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"values": {}
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"name": "copyTaskObjectToTaskV2",
4+
"private": false,
5+
"run_as_system": true
6+
},
7+
{
8+
"name": "copyTaskV2ObjectToTask",
9+
"private": false,
10+
"run_as_system": true
11+
}
12+
]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// :code-block-start: copyTaskObject
2+
exports = function (changeEvent) {
3+
const db = context.services.get("mongodb-atlas").db("ExampleDB");
4+
const collection = db.collection("Task");
5+
// The changed document's _id as an integer:
6+
const changedDocId = changeEvent.documentKey._id;
7+
// If a document in the Task collection has been deleted,
8+
// delete the equivalent object in the TaskV2 collection:
9+
if (changeEvent.operationType === "delete") {
10+
const tasksV2Collection = db.collection("TaskV2");
11+
// Convert the deleted document's _id to a string value
12+
// to match TaskV2's schema:
13+
const deletedDocumentID = changedDocId.toString();
14+
return tasksV2Collection.deleteOne({ _id: deletedDocumentID })
15+
}
16+
// A document in the Task collection has been created,
17+
// modified, or replaced, so create a pipeline to handle the change:
18+
const pipeline = [
19+
// Find the changed document data in the Task collection:
20+
{ $match: { _id: changeEvent.documentKey._id } },
21+
{
22+
// Transform the document by changing the _id field to a string:
23+
$addFields: {
24+
_id: { $toString: "$_id" },
25+
},
26+
},
27+
// Insert the document into TaskV2, using the $merge operator
28+
// to avoid overwriting the existing data in TaskV2:
29+
{ $merge: "TaskV2" }]
30+
return collection.aggregate(pipeline);
31+
};
32+
// :code-block-end:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// :code-block-start: copyTaskV2Object
2+
exports = function (changeEvent) {
3+
const db = context.services.get("mongodb-atlas").db("ExampleDB");
4+
const collection = db.collection("TaskV2");
5+
// The changed document's _id as a string:
6+
const changedDocId = changeEvent.documentKey._id;
7+
// If a document in the TaskV2 collection has been deleted,
8+
// delete the equivalent object in the Task collection
9+
if (changeEvent.operationType === "delete") {
10+
const taskCollection = db.collection("Task");
11+
// Convert the deleted document's _id to an integer value
12+
// to match Task's schema:
13+
const deletedDocumentID = parseInt(changedDocId);
14+
return taskCollection.deleteOne({ _id: deletedDocumentID })
15+
}
16+
// A document in the Task collection has been created,
17+
// modified, or replaced, so create a pipeline to handle the change:
18+
const pipeline = [
19+
// Find the changed document data in the Task collection
20+
{ $match: { _id: changedDocId } },
21+
{
22+
// Transform the document by changing the _id field
23+
$addFields: {
24+
_id: { $toInt: "$_id" },
25+
},
26+
},
27+
{ $merge: "Task" }
28+
]
29+
return collection.aggregate(pipeline);
30+
};
31+
// :code-block-end:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"use_natural_pluralization": true
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"app_id": "functions_tester-qodrp",
3+
"config_version": 20210101,
4+
"name": "functions_tester",
5+
"location": "US-VA",
6+
"deployment_model": "LOCAL"
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"development_mode_enabled": false
3+
}
Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
exports = function(changeEvent) {
2-
const db = context.services.get("mongodb-atlas").db("ExampleDB");
3-
const collection = db.collection("Task");
4-
const changedDocId = changeEvent.documentKey._id; // the changed document's _id as an integer
5-
6-
// if a document in the Task collection has been deleted, delete the adjacent object in the TaskV2 collection
7-
if(changeEvent.operationType === "delete") {
8-
const tasksV2Collection = db.collection("TaskV2");
9-
const deletedDocumentID = changedDocId.toString(); // get the deleted document's _id as a string value since TaskV2's _id are queried as a string
10-
return tasksV2Collection.deleteOne({ _id: deletedDocumentID })
11-
}
12-
13-
// if a document in the Task collection has been created, modified, or replaced, do the same to the adjacent object in the TaskV2 collection
14-
const pipeline = [
15-
// extract the changed document data from the Task collection
16-
{ $match: { _id: changeEvent.documentKey._id } },
17-
{
18-
// transform the document, by altering the _id field
19-
$addFields: {
20-
_id: { $toString: "$_id" }, // change the _id field to a string type, since TaskV2 stores _id as a string
21-
},
1+
exports = function (changeEvent) {
2+
const db = context.services.get("mongodb-atlas").db("ExampleDB");
3+
const collection = db.collection("Task");
4+
// The changed document's _id as an integer:
5+
const changedDocId = changeEvent.documentKey._id;
6+
7+
// If a document in the Task collection has been deleted,
8+
// delete the equivalent object in the TaskV2 collection:
9+
if (changeEvent.operationType === "delete") {
10+
const tasksV2Collection = db.collection("TaskV2");
11+
12+
// Convert the deleted document's _id to a string value
13+
// to match TaskV2's schema:
14+
const deletedDocumentID = changedDocId.toString();
15+
return tasksV2Collection.deleteOne({ _id: deletedDocumentID })
16+
}
17+
18+
// A document in the Task collection has been created,
19+
// modified, or replaced, so create a pipeline to handle the change:
20+
const pipeline = [
21+
// Find the changed document data in the Task collection:
22+
{ $match: { _id: changeEvent.documentKey._id } },
23+
{
24+
// Transform the document by changing the _id field to a string:
25+
$addFields: {
26+
_id: { $toString: "$_id" },
2227
},
23-
{ $merge: "TaskV2" } // insert the document into TaskV2, using the $merge operator to avoid overwriting the existing data in TaskV2
24-
]
25-
return collection.aggregate(pipeline);
26-
};
27-
28+
},
29+
// Insert the document into TaskV2, using the $merge operator
30+
// to avoid overwriting the existing data in TaskV2:
31+
{ $merge: "TaskV2" }]
32+
return collection.aggregate(pipeline);
33+
};
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
exports = function(changeEvent) {
1+
exports = function (changeEvent) {
22
const db = context.services.get("mongodb-atlas").db("ExampleDB");
33
const collection = db.collection("TaskV2");
4-
const changedDocId = changeEvent.documentKey._id; // the changed document's _id as a string
4+
// The changed document's _id as a string:
5+
const changedDocId = changeEvent.documentKey._id;
56

6-
// if a document in the TaskV2 collection has been deleted, delete the adjacent object in the Task collection
7-
if(changeEvent.operationType === "delete") {
7+
// If a document in the TaskV2 collection has been deleted,
8+
// delete the equivalent object in the Task collection
9+
if (changeEvent.operationType === "delete") {
810
const taskCollection = db.collection("Task");
9-
const deletedDocumentID = parseInt(changedDocId); // get the deleted document's _id as an integer value since Task's _id are queried as an integer
10-
return taskCollection.deleteOne({ _id: deletedDocumentID})
11+
// Convert the deleted document's _id to an integer value
12+
// to match Task's schema:
13+
const deletedDocumentID = parseInt(changedDocId);
14+
return taskCollection.deleteOne({ _id: deletedDocumentID })
1115
}
12-
13-
// if a document in the TaskV2 collection has been created, modified, or replaced, do the same to the adjacent object in the Task collection
16+
17+
// A document in the Task collection has been created,
18+
// modified, or replaced, so create a pipeline to handle the change:
1419
const pipeline = [
15-
// extract the changed document data from the TaskV2 collection
16-
{ $match: { _id: changedDocId } },
20+
// Find the changed document data in the Task collection
21+
{ $match: { _id: changedDocId } },
1722
{
18-
// transform the document, by altering the _id field
23+
// Transform the document by changing the _id field
1924
$addFields: {
20-
_id: { $toInt: "$_id" }, // change the _id field to an integer type, since Task stores _id as an integer
25+
_id: { $toInt: "$_id" },
2126
},
2227
},
23-
{ $merge: "Task" } // insert the document into Task, using the $merge operator to avoid overwriting the existing data in Task
28+
{ $merge: "Task" }
2429
]
2530
return collection.aggregate(pipeline);
2631
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
exports = function (changeEvent) {
2+
const db = context.services.get("mongodb-atlas").db("ExampleDB");
3+
const collection = db.collection("Task");
4+
// The changed document's _id as an integer:
5+
const changedDocId = changeEvent.documentKey._id;
6+
// If a document in the Task collection has been deleted,
7+
// delete the equivalent object in the TaskV2 collection:
8+
if (changeEvent.operationType === "delete") {
9+
const tasksV2Collection = db.collection("TaskV2");
10+
// Convert the deleted document's _id to a string value
11+
// to match TaskV2's schema:
12+
const deletedDocumentID = changedDocId.toString();
13+
return tasksV2Collection.deleteOne({ _id: deletedDocumentID })
14+
}
15+
// A document in the Task collection has been created,
16+
// modified, or replaced, so create a pipeline to handle the change:
17+
const pipeline = [
18+
// Find the changed document data in the Task collection:
19+
{ $match: { _id: changeEvent.documentKey._id } },
20+
{
21+
// Transform the document by changing the _id field to a string:
22+
$addFields: {
23+
_id: { $toString: "$_id" },
24+
},
25+
},
26+
// Insert the document into TaskV2, using the $merge operator
27+
// to avoid overwriting the existing data in TaskV2:
28+
{ $merge: "TaskV2" }]
29+
return collection.aggregate(pipeline);
30+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
exports = function (changeEvent) {
2+
const db = context.services.get("mongodb-atlas").db("ExampleDB");
3+
const collection = db.collection("TaskV2");
4+
// The changed document's _id as a string:
5+
const changedDocId = changeEvent.documentKey._id;
6+
// If a document in the TaskV2 collection has been deleted,
7+
// delete the equivalent object in the Task collection
8+
if (changeEvent.operationType === "delete") {
9+
const taskCollection = db.collection("Task");
10+
// Convert the deleted document's _id to an integer value
11+
// to match Task's schema:
12+
const deletedDocumentID = parseInt(changedDocId);
13+
return taskCollection.deleteOne({ _id: deletedDocumentID })
14+
}
15+
// A document in the Task collection has been created,
16+
// modified, or replaced, so create a pipeline to handle the change:
17+
const pipeline = [
18+
// Find the changed document data in the Task collection
19+
{ $match: { _id: changedDocId } },
20+
{
21+
// Transform the document by changing the _id field
22+
$addFields: {
23+
_id: { $toInt: "$_id" },
24+
},
25+
},
26+
{ $merge: "Task" }
27+
]
28+
return collection.aggregate(pipeline);
29+
};

source/images/agg-in-atlas.png

653 KB
Loading

source/includes/note-modify-schema-properties-of-synced-realms.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
The following page demonstrates how to modify schema properties of a local
44
{+realm+}. Learn how to :doc:`modify schema properties of a synced realm
5-
</sync/modify-synced-object-schema>`.
5+
</sync/synced-schema-overview>`.

0 commit comments

Comments
 (0)