I have an issue, I'm not able to get the relationship between 2 schemas with Mongoose, I'm a new user using this, I'm using NestJS, that's a NodeJS Framework using TypeScript.
I have these files:
user.schema.ts
import * as mongoose from 'mongoose';
export const UserSchema = new mongoose.Schema({
name: String,
email: String,
role: Number,
tokens: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Token'}]
});
token.schema.ts
import * as mongoose from 'mongoose';
export const TokenSchema = new mongoose.Schema({
token: String,
refreshToken: String,
createdAt: String,
expiresAt: String,
isValid: Boolean,
userId: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User'}]
});
So, the relation is created, I can see it, but I don't know how to get the children of the User schema. In my ts I have this:
await this.userModel.find().populate('user', 'tokens').exec()
As I know that's to get the relation between the 2 schemas. BTW: The collections are users and tokens.
I'll appreciate any feedback or if I have to make something clear.