【发布时间】:2020-08-02 04:06:15
【问题描述】:
我是 NoSQL/MongoDB 的新手,我需要在我的数据库中的一个集合上编写插入事件触发器,如下所示: 数据库名称:GiftShop 集合名称:Gifts Gifts Schema如下:-
const mongoose = require("mongoose");
const userModel = require('./userModel');
const imageModel = require('./imageModel');
const giftSchema = mongoose.Schema({
name: String,
availableQuantity: Number,
price: Number,
Seller: {type: mongoose.Schema.Types.ObjectId,ref: 'UserModel'},
imageName: {type: mongoose.Schema.Types.ObjectId,ref: 'ImageModel'},
deliveryInDays: Number,category: [{type: String, ref: 'CategoryModel'}]
}, {collection: "gifts"});
module.exports = giftSchema;
我有另一个名为通知的集合。我想创建一个这样的触发器,一旦将新礼物插入礼物集合中,通知集合必须填充以下信息: 礼物编号, 卖家编号, 买家 ID 数组。
通知的架构如下:
const mongoose = require("mongoose");
const notificationSchema = mongoose.Schema({
seller: { type: mongoose.Schema.Types.ObjectId,ref: 'UserModel'},
buyer: [{type: mongoose.Schema.Types.ObjectId,ref: 'UserModel'}],
newGift: {type: mongoose.Schema.Types.ObjectId,ref: 'GiftModel'}
}, {collection: "notifications"});
module.exports = notificationSchema;
以下是我在 MongoDB atlas 中编写的函数,但它有很多问题:
exports = function (changeEvent) {
const {fullDocument} = changeEvent;
const {buyer, seller, newGift} = fullDocument;
const collection = context.services.get("Cluster0").db("test").collection("notification");
return collection.insertOne({buyer, seller, newGift})};
我在 MongoDB atlas 上为创建触发器所做的设置:
我正在通过连接到我的 MongoDB 地图集的 MongoDB 指南针插入一个新礼物,但我的通知集合没有填充通知架构中提到的数据。我哪里错了?根据我的要求编写触发器的正确方法是什么?谢谢。
【问题讨论】:
标签: mongodb mongoose mongodb-query database-trigger mongodb-atlas