带有嵌套对象的猫鼬模式

时间:2020-05-11 06:04:55

标签: javascript node.js mongodb mongoose

我正在尝试在Schema中创建一个文档,该文档本质上就像具有值数组(日期)的字典一样工作:

我想用它来跟踪我发送给用户的电子邮件通信:

let d = {
    'generic-contact' = ['11/02/2019', '11/05/2020'],
    'update-profile' = ['1/01/2018']
}

然后我将使用以下内容更新此文档字段:

let emailAddresses = ['joebloggs@yahoo.com', 'foobar@googlemail.com']

let recipients = await Profile.find({ email: { "$in" : emailAddresses } })
let emailTopic = 'newsletter03'

recipients.forEach(user => {

    user.correspondenceHistory[emailTopic].push(Date.now())
    user.save()

})

我想这样做是为了确保在一定的时间内不要向同一用户发送相同的电子邮件。

但是,我不知道如何在我的模式中进行设置。这种结构可能吗?

我为correspondenceHistory尝试了许多不同的结构,但是它们都没有起作用,我无法弄清我做错了什么。这是我的现状:

const mongoose = require("mongoose");
const passportLocalMongoose = require("passport-local-mongoose");

var profileSchema = new mongoose.Schema({
    email: String,
    firstname: String,
    lastname: String,
    correspondenceHistory: { type: Array } ### Here ###
}, { discriminatorKey: 'accountType', retainKeyOrder: true, timestamps: true });

0 个答案:

没有答案