So, I've two schemas where one has a reference to other, the problem is that for the other one I haven't any reference.
var articleSchema = new mongoose.Schema({
title: {type: String, text: true},
description: {type: String, text: true},
url: String,
....
var userSchema = new mongoose.Schema({
...
articles: [{
type: ObjectId,
ref: 'Article'}],
...
As seen above, in the article scheme there isn't any reference to the User schema while User schema has the array of article ids as a reference.
My question: Is there any way to get those references and by mapping or using any other method, I want to store for each article the User ID ?
Vishwa Bhat
Technology Enthusiast
Firstly, you don't necessarily need to follow RDB relation concepts on Mongo DB. Mongo provides flexibility for keeping redudant data with very negligible overhead to make retrieval operations fast. So you can keep User ID inside Article model although you have array of Article ID references inside User model.
Secondly, I'd not recommend you to keep reference of Article doc inside User as all articles will load with every deafult find query you make on User model. Instead, store Article IDs as raw ObjectIds inside array and whenever you want to operate on articles inside User query then you can use $lookup operator inside aggregation or simply do a find a query on Article with matching User ID because anyway Document IDs are indexed by deafult so performance wont hamper much.
I hope I've answered your question. Cheers!