i have two models in seperate sevices(micro service). How can i use the model in other sevice to populate its data in my aggregation.
I added a clone of user model coming from user service. In other to load the appropriate model like so.
import mongoose, { Schema } from 'mongoose';
import configuration from '../config/config';
const db = mongoose.createConnection(configuration.mongo.auth_db);
export const User = db.model(
'Users',
new Schema({
userType: String,
status: Boolean,
isVerified: Boolean,
})
);
export const Merchant = db.model(
'Merchant',
new Schema({
isApproved: Boolean,
storeStatus: String,
storeUrl: String,
})
);
This is the actual model in the wishlist service where i also added the model references
import { Product } from './product';
import { User } from './user';
const WishlistSchema = new Schema<IWishlist>(
{
user: {
type: Schema.Types.ObjectId,
ref: User,
required: true,
},
products: [
{
product: {
type: Schema.Types.ObjectId,
ref: Product,
},
addedAt: {
type: Date,
default: new Date(),
},
},
],
},
{
timestamps: true,
}
);
This is what i have in my get wishlist code. The issue is $lookup from accepts only string collection name. And when i try to change it the actual collection name. i.e from: "users", it doesn't work
import { User } from '../models/user';
const wishlist = await Wishlist.aggregate([
{
$match: {
user: new Types.ObjectId(user.id),
},
},
{
$unwind: '$products',
},
{
$sort: {
'products.addedAt': -1,
},
},
{
$lookup: {
from: User,
localField: 'user',
foreignField: '_id',
as: 'user',
},
},
]);