In my MongoDB trigger function I have a simple export of
exports = async function (changeEvent) {
// A Database Trigger will always call a function with a changeEvent.
// Documentation on ChangeEvents: https://docs.mongodb.com/manual/reference/change-events/
// This sample function will listen for events and replicate them to a collection in a different Database
// Access the _id of the changed document:
const docId = changeEvent.documentKey._id.toString();
// Get the MongoDB service you want to use (see "Linked Data Sources" tab)
// Note: In Atlas Triggers, the service name is defaulted to the cluster name.
const serviceName = "AtlasCluster";
const database = "testDev";
const collection = context.services.get(serviceName).db(database).collection(changeEvent.ns.coll);
const collectionsink = context.services.get(serviceName).db(database).collection("test");
const doc = collection.aggregate(
[
{
$match: {
_id: new BSON.ObjectId(docId)
}
},
{
$unwind: "$test"
},
{
$unwind: "$test.array"
},
{
$project: {
parameter: "$test.array.samples",
hierarchy: {
"equipmentComponentId": "Lorem",
"lastUpdatedDate": "2024-05-12T13:23:12.456Z",
position: "$test.array.samples.position",
},
}
}
]);
console.log(doc);
// Get the "FullDocument" present in the Insert/Replace/Update ChangeEvents
try {
// If this is a "delete" event, delete the document in the other collection
if (changeEvent.operationType === "delete") {
await collection.deleteOne({ "_id": docId });
}
// If this is an "insert" event, insert the document into the other collection
else if (changeEvent.operationType === "insert") {
console.log("Insert");
console.log(docId);
await collection.aggregate(
[
{
$match: {
_id: new BSON.ObjectId(ObjectId(docId))
}
},
{
$unwind: "$equipment"
},
{
$unwind: "$equipment.components"
},
{
$project: {
_id:0,
parameter: "$equipment.components.samples.parameters",
ierarchy: {
"equipmentComponentId": "Lorem",
"lastUpdatedDate": "2024-05-12T13:23:12.456Z",
position: "$test.array.samples.position",
},
}
},
{
$merge:{
into: "test"
}
}
]);
}
// If this is an "update" or "replace" event, then replace the document in the other collection
else if (changeEvent.operationType === "update" || changeEvent.operationType === "replace") {
await collection.replaceOne({ "_id": docId }, changeEvent.fullDocument);
}
} catch (err) {
console.log("error performing mongodb write: ", err.message);
}
};
I am unsure what I am doing wrong but I have a few issues that I am unable to solve for. Inside my Insert block I try to do a variety of InsertOne, InsertMany, and also a merge.
- When I do InsertOne – and just await collection.InsertOne(doc) – I get needs to be an object error with mongowrite
- When I do an InsertMany – await collection.InsertOne(doc.toArray()) I get “needs to be an array error with mongowrite
- I tried to do a merge – and now I get “error performing mongodb write: ‘ObjectId’ is not defined”. I even tried to use new BSON method for ObjectID but it doesnt find the document and nothing is getting merged
Am I missing something that is here that these codes should work find if it is working fine in monogoshell?