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.
I have solved my issue with ONE-TO-MANY relationship in this way:
package.json:
"@nestjs/mongoose": "7.0.2"
"mongoose": "^5.10.14"
For doing an example, I'll use the schemas from this issue(User
and Token
):
Token
Schemaimport { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
@Schema({_id: false})
export class Token {
@Prop({type: Types.ObjectId})
_id: Types.ObjectId;
@Prop()
token: string;
@Prop()
refreshToken: string;
@Prop()
createdAt: Date; // You can use Date here.
@Prop()
expiresAt: Date;
@Prop()
isValid: boolean;
}
export const TokenSchema = SchemaFactory.createForClass(Token);
Token
Schema into TokenModule
import { MongooseModule } from '@nestjs/mongoose';
@Module({
imports: [
MongooseModule.forFeature([
{ name: Token.name, schema: TokenSchema },
]),
],
// ...
})
export class TokenModule {}
User
Schemaimport { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type UserDocument = User & Document;
@Schema({_id: false})
export class User {
@Prop({type: Types.ObjectId})
_id: Types.ObjectId;
@Prop()
name: string;
@Prop()
email: string;
@Prop()
role: number;
@Prop(type: [Types.ObjectId], ref: Token.name)
tokens: Token[];
}
export const UserSchema = SchemaFactory.createForClass(User);
User
Schema into UserModule
import { MongooseModule } from '@nestjs/mongoose';
@Module({
imports: [
MongooseModule.forFeature([
{ name: User.name, schema: UserSchema },
]),
],
// ...
})
export class UserModule {}
populate
function in the UserService
@Injectable()
export class UserService {
constructor(
@InjectModel(User.name) private userModel: Model<UserDocument>,
) {}
async getUserWithPopulate(id: string) {
// Here is the ace in the hole, you need to specify the path (tokens) and the model (Token.name).
await this.userModel
.find()
.populate('tokens', null, Token.name)
.exec()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With