MongoDB Trigger Function “error performing mongodb write: ‘ObjectId’ is not defined”

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.

  1. When I do InsertOne – and just await collection.InsertOne(doc) – I get needs to be an object error with mongowrite
  2. When I do an InsertMany – await collection.InsertOne(doc.toArray()) I get “needs to be an array error with mongowrite
  3. 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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật