Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

mongodb - Unique parent-child relationship data in Mongoose

In a language learning app, each time a user learns a new sentence(which is a document in itself), I would like to add that as a child of the user, so that for each user you can see the sentences that they have already learned.

This is all fine to do with

sentences: [{
        type: mongoose.Schema.ObjectId,
        ref: 'Sentence'
      }]

However, I would like to add data about that user's relationship with the child, e.g. firstLearnedOnDate: , totalCorrectAnswers: etc. Is this possible in mongoose? Is there a better solution that I'm missing?

I have tried messing around with putting the mongoose.Schema.ObjectId into another object with the other properties which I want, but that didn't work.

question from:https://stackoverflow.com/questions/65848310/unique-parent-child-relationship-data-in-mongoose

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you should read about ref and populate, check the documentation and read this article, with use populate you can do what you want


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...