0

I'm trying to create a document in my Schema that essentially works like a dictionary with an array of values (dates):

I want to use this to keep track of email correspondence I am sending users:

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

I would then update this document field with something like:

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()

})

I want to do this so that I make sure that I don't send the same user the same email within a certain period of time.

However I can't work out how to set this up in my schema. Is this sort of structure possible?

I've tried many different structures for correspondenceHistory, but none of them have worked and I can't work out what I'm doing wrong. Here is my schema as it stands:

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 });
fugu
  • 6,417
  • 5
  • 40
  • 75
  • Will the keys of the dictionary remain same all the time? –  May 11 '20 at 06:20
  • No. I will create a new one for each new correspondence type – fugu May 11 '20 at 06:22
  • Maybe this is helpful https://stackoverflow.com/questions/25030577/creating-a-dictionary-schema-with-mongoose –  May 11 '20 at 06:25
  • Kinda - thanks. So I guess it's not possible? `correspondenceHistory: { key: String, value: Date }` doesn't achieve what I want, as it just overwrite the document each time – fugu May 11 '20 at 06:29
  • 1
    Yeah, you can push whatever field you want to the object then, but it is at the cost of no validation by mongoose. –  May 11 '20 at 06:30
  • 1
    With Mongoose schemas you can do `correspondenceHistory: [ { key: String, value: Date } ]` . Or even define another schema `Corresponedence = mongoose.schema({ key: String, value: Date })` then `correspondenceHistory: [ Correspondence ]` – Jeremy Thille May 11 '20 at 07:06
  • @JeremyThille OK thanks. I've tried this, but it's less convenient than what I was hoping for. – fugu May 11 '20 at 07:24
  • @JeremyThille - although, just trying it again now I remember why i didn't like it. `correspondenceHistory: [ { subject: String, date: Date } ]` when I do `user.correspondenceHistory.push({'subject': data.subject, 'date': Date.now() });` I get an `_id` created for each entry. What's going on here? e.g. `{ _id: 5eb8fe2bef0a4e11b104e71c, subject: 'Welcome to my website', date: 2020-05-11T07:26:00.000Z } ` – fugu May 11 '20 at 07:29
  • 2
    https://stackoverflow.com/questions/17254008/stop-mongoose-from-creating-id-property-for-sub-document-array-items – Jeremy Thille May 11 '20 at 07:35
  • @fugu i think it creates a subdocument every time you push. subdocuments have their own _id automatically – Karl L May 12 '20 at 02:44

0 Answers0