0

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',
    },
  },
]);
Erin Deji
  • 92
  • 6
  • What do you mean by "it doesn't work"? Please show the debugging details – Lin Du Jul 25 '23 at 06:59
  • @LinDu It doesn't work because the user collection is from another database entirely. So it returns an empty array as it is unable to populate the data. The collection 'users' is from another database named auth. And the wishlist has its own database and collection – Erin Deji Jul 25 '23 at 07:05
  • I also tried from: 'auth.users' same result. – Erin Deji Jul 25 '23 at 07:15
  • make sure _id of foreignField and user in localField are same, then join will happen. – Gaurav Sisodia Jul 25 '23 at 07:33
  • Is this your question? https://jira.mongodb.org/browse/SERVER-34935. Also, see https://stackoverflow.com/questions/74555873/how-can-i-join-two-collections-from-different-databases-in-mongodb – Lin Du Jul 25 '23 at 07:35
  • The first one yes. 2nd one no. Because I am already able to join the two collections. The only bottleneck is that $lookup.from only accepts model names as string values, not the actual DB Model – Erin Deji Jul 25 '23 at 07:47
  • when joining two collections, must look out for the local field and foreign field, if both fields are similar then it will join the collection, otherwise, it will show the blank array. – Gaurav Sisodia Aug 09 '23 at 09:34

0 Answers0