# Need to create smart relationship between two collections that exist in different database with MongoDB

**URL:** <https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557>\
**Category:** Help me!\
**Tags:** howtos\
**Created:** [November 21, 2022, 9:08am UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557 "2022-11-21T09:08:22Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giri\_S](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/giri_s/32/3930_2.png) [@Giri\_S](https://community.forestadmin.com/u/Giri_S)\
**Post date:** [November 21, 2022, 9:08am UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/1 "2022-11-21T09:08:22Z")

</div>

Hi Team, I need to now how to create a smart relationship between two collection that exist in different database.

My Config.  
MongoDB Version: MongoDB 4.4.17 Enterprise. (doesn’t support lookup operation for joining the collection in the different database)  
forest admin agent installed in NodeJs.  
npm mongoose --version 8.1.0  
npm express --version 8.1.0

Schema Sample:  
Database Name: credit, Collection name: customers.  
Database Name: loans, Collection\_name: transactions

customers sample schema:  
{  
\_id: Object\_id,  
company\_name: String,  
entity\_id: ObjectId  
}

transactions sample schema

{  
\_id: ObjectId,  
customer\_id: ObjectId  
}

Relationship.

transactions collections customer\_id belongs\_to Customer collection entity\_id.

---

<div class="post-metadata">

**Author:** ![anon34731316](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@anon34731316](https://community.forestadmin.com/u/anon34731316)\
**Post date:** [November 21, 2022, 11:18am UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/2 "2022-11-21T11:18:51Z")

</div>

Hello @Giri_S,

Thank you for your message 🙌

Have you checked [this part of the documentation](https://docs.forestadmin.com/documentation/reference-guide/models/relationships/create-a-smart-relationship)?  
If it doesn’t suit your needs, could you please tell me why?

Thanks!

---

<div class="post-metadata">

**Author:** ![Giri\_S](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/giri_s/32/3930_2.png) [@Giri\_S](https://community.forestadmin.com/u/Giri_S)\
**Post date:** [November 21, 2022, 1:09pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/3 "2022-11-21T13:09:20Z")

</div>

Yes @anon34731316 I checked but i receiving the below error when I tried to create smart relationship.

**Cannot retrieve the orginal\_customer\_id value because of an internal error in the getter implementation: Customer.find is not a function**

customers is a model.  
orginal\_customer\_id is a smart relationship field.

```javascript
const { collection } = require("forest-express-mongoose");
const logger = require("../config/logger")(module);
const { loanTransactions } = require("../models");
const AWS = require('aws-sdk');
const Customer = require('../models/onboarding/customers');
console.log("Printing Customer", Customer)

// This file allows you to add to your Forest UI:
// - Smart actions: https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions
// - Smart fields: https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields
// - Smart relationships: https://docs.forestadmin.com/documentation/reference-guide/relationships/create-a-smart-relationship
// - Smart segments: https://docs.forestadmin.com/documentation/reference-guide/segments/smart-segments

collection("loanTransactions", {
  fields: [
    {
      field: 'orginal_customer_id',
      type: 'String',
      reference: 'customers._id',
      get: function (transaction) {
        return Customer.find({entity_id: transaction.customer_id});
      }
    }
  ],
  actions: [
   
    {
      name: "Release Deal",
      type: "single",
      fields: [

        {
          field: "deal_name",
          type: "String",
          isRequired: true,
          isReadOnly: true
        },
        {
          field: "investor_ids",
          isRequired: true,
          reference: "entities._id",
          type: ['String']
        }
      ], hooks: {
        load: async ({ fields, request }) => {
          const deal_name = fields.find(
            (field) => field.field === 'deal_name'
          );
          const id = request.body.data.attributes.ids[0];
          const loanTransaction = await loanTransactions.findById(id);
          deal_name.value = loanTransaction.deal_name;
          return fields;
        },
      },
    },
  ],

});

```

Customer schema

```javascript
// This model was generated by Forest CLI. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models

module.exports = (mongoose, Mongoose) => {
  // This section contains the properties of your model, mapped to your collection's properties.
  // Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const schema = Mongoose.Schema({
    'company_name': String,
    'state': String,
    'entity_id': { type: Mongoose.Schema.Types.ObjectId, ref: 'craEntities' },
    'last_updated_by': { type: Mongoose.Schema.Types.ObjectId, ref: 'craUsers' },
    'updated_at': Date,
    'created_at': Date,
    'created_by': String,
    'esop': {
      'esop_status': String,
      'vesting_period': [Number],
    },
  }, {
    timestamps: false,
  });

  return mongoose.model('customers', schema, 'customers');
};

```

Index.js

```javascript
const fs = require('fs');
const path = require('path');
const Mongoose = require('mongoose');

const databasesConfiguration = require('../config/databases');

const connections = {};
const db = {};

databasesConfiguration.forEach((databaseInfo) => {
  const connection = Mongoose.createConnection(databaseInfo.connection.url, databaseInfo.connection.options);
  connections[databaseInfo.name] = connection;

  const modelsDir = databaseInfo.modelsDir || path.join(__dirname, databaseInfo.name);
  fs
    .readdirSync(modelsDir)
    .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js')
    .forEach((file) => {
      try {
        const model = require(path.join(modelsDir, file))(connection, Mongoose);
        db[model.modelName] = model;
      } catch (error) {
        console.error(`Model creation error: ${error}`);
      }
    });
});

db.objectMapping = Mongoose;
db.connections = connections;

module.exports = db;

```

---

<div class="post-metadata">

**Author:** ![anon34731316](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@anon34731316](https://community.forestadmin.com/u/anon34731316)\
**Post date:** [November 21, 2022, 1:39pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/4 "2022-11-21T13:39:39Z")

</div>

@Giri_S,

Thanks for the prompt reply.

Just to be sure: apart from this smart relationship, is your configuration on Forest Admin with multiple DB working fine? Can you browse all your collections?  
(Otherwise you might want to check this [topic](https://community.forestadmin.com/t/issue-plug-multiple-nosql-databases/3248/3))

About the error you’re facing, I’m not an expert of MongoDB 4 Enterprise.  
Should the `find` be accessible?  
Can you log the Customer value inside the getter to see if it’s well defined?

Thanks.

---

<div class="post-metadata">

**Author:** ![Giri\_S](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/giri_s/32/3930_2.png) [@Giri\_S](https://community.forestadmin.com/u/Giri_S)\
**Post date:** [November 21, 2022, 1:49pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/6 "2022-11-21T13:49:49Z")

</div>

@anon34731316 Yes everything is working fine except this one smart-relationship field, even in the detailed view, I can see all fields, except this smart relationship field.

i logged the value: **Printing Customer [Function (anonymous)]**

MongoDB connections and methods are working fine.

my guess is since **Customer** schema is anonymous function, it expects two input to be passed, even the vscode text editor linter showing this as error(attaching a screenshot).

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/1/181154ba5cf857540a71de24d8ed6f3de1f37b21.png)

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [November 21, 2022, 2:18pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/7 "2022-11-21T14:18:40Z")

</div>

Hey @Giri_S 👋

Could you try to import Customer as `const { Customer } = require('../models');`?

According to your `models/index.js` (`db[model.modelName] = model;`) all your models should be exposed as “flat”. If it still does not work, I would suggest to try:

- Using the connection name to directly import the model
- `console.log(db)` inside `models/index.js` to check if the model name is available.

Let me know if that helps 🙏

---

<div class="post-metadata">

**Author:** ![Giri\_S](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/giri_s/32/3930_2.png) [@Giri\_S](https://community.forestadmin.com/u/Giri_S)\
**Post date:** [November 21, 2022, 3:09pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/10 "2022-11-21T15:09:13Z")

</div>

Hey @anon39848301 solution you provided worked but now I am struggling in getting the \_id field of any model, i can’t able to get the \_id of any document. (attached the debug pointer screenshot for reference)

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/f/f91a35789c3e9afca39f73f5c109bc1468989fe5.png)

---

<div class="post-metadata">

**Author:** ![Giri\_S](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/giri_s/32/3930_2.png) [@Giri\_S](https://community.forestadmin.com/u/Giri_S)\
**Post date:** [November 21, 2022, 3:21pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/11 "2022-11-21T15:21:28Z")

</div>

The get function should return a customer document were customers.entity\_id == transactions.customer\_id, can you please guide on how to write this.  
@anon39848301 @anon34731316

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [November 21, 2022, 3:42pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/12 "2022-11-21T15:42:35Z")

</div>

Shouldn’t it be `transaction.customer_id` instead of `transaction.customer_id.id` in your `get` function ?

---

<div class="post-metadata">

**Author:** ![Giri\_S](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/giri_s/32/3930_2.png) [@Giri\_S](https://community.forestadmin.com/u/Giri_S)\
**Post date:** [November 21, 2022, 4:31pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/13 "2022-11-21T16:31:27Z")

</div>

thanks @anon39848301 i can able to define a relationship successfully now. but now when I try to include the related data in summary I can’t able to do that, since relation\_data is undetectable.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/9/9cc4e5b9a90d731c62d2a6fdcc0731899b09d368.png)  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/4/4bf7a1ed6ced21dd4578a943e81ae51cf9192a45.png)

@anon34731316

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [November 21, 2022, 4:35pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/14 "2022-11-21T16:35:41Z")

</div>

So the initial issue is resolved, right?

For the second one, it will be easier to debug if you could provide the associated HTTP response of the summary view (Here or as DM if you consider it private)

---

<div class="post-metadata">

**Author:** ![Giri\_S](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/giri_s/32/3930_2.png) [@Giri\_S](https://community.forestadmin.com/u/Giri_S)\
**Post date:** [November 21, 2022, 4:48pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/15 "2022-11-21T16:48:08Z")

</div>

@anon39848301 initial issue was resolved thanks a lot. for the second one below is the response

```javascript
{"included":[{"type":"customers","id":"632022ed2cad2c001ecb0732","attributes":{"_id":"632022ed2cad2c001ecb0732"}},{"type":"entities","id":"632022eb93e12d006c4552d8","attributes":{"PAN":"AABCU9001R","registrar":null,"can_avail_moratorium":false,"subgroups":["anchor"],"company_name":"EF Builders","investor":null,"updated_at":"2022-09-13T17:16:29.356Z","created_at":"2022-09-13T06:27:56.385Z","trustee":null,"retail_user":null,"rating_agency":null,"customer":true,"CIN":null,"vendor":null,"contact_person":null,"law_firm":null,"auditor":null,"anchor":null,"collateral_manager":null,"guarantor":null,"_id":"632022eb93e12d006c4552d8"}}],"data":{"type":"loanTransactions","id":"637b7e423d671d001a0d191b","attributes":{"deal_name":"EF LAP 11 2022 T53","transaction_state":"draft","_id":"637b7e423d671d001a0d191b"},"relationships":{"borrower_id":{"data":{"type":"customers","id":"632022ed2cad2c001ecb0732"},"links":{"related":{"href":"/forest/loanTransactions/637b7e423d671d001a0d191b/relationships/borrower_id"}}},"customer_id":{"data":{"type":"entities","id":"632022eb93e12d006c4552d8"},"links":{"related":{"href":"/forest/loanTransactions/637b7e423d671d001a0d191b/relationships/customer_id"}}}}}}

```

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [November 22, 2022, 8:22am UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/16 "2022-11-22T08:22:16Z")

</div>

From the screenshot you shared and the type of relation you declared (A `belongsTo`), nothing will be displayed in the “Related data” of the “Loan transactions” collection for the summary via (As a Loan Transactions only have one customer). Customer infos should be available in the “Fields” part of the Visual Builder.

Let me know if that helps.

---

<div class="post-metadata">

**Author:** ![Giri\_S](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/giri_s/32/3930_2.png) [@Giri\_S](https://community.forestadmin.com/u/Giri_S)\
**Post date:** [November 22, 2022, 11:05am UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/17 "2022-11-22T11:05:25Z")

</div>

@anon39848301 @anon34731316 Got it, I have one more use case, where I need to create a has\_many smart relationship, while going through the doc they used the lookup operator but in my case, I can’t use the lookup operator because collections exist in different database and my version of mongo doesn’t support lookup between collections in a different database. kindly guide me in fixing this.

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [November 22, 2022, 1:19pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/18 "2022-11-22T13:19:41Z")

</div>

At first read and in my opinion, you should still be able to achieve this without lookup, as long as you have access to both table on both databases.

The following hasMany declaration in our forest-express-mongoose documentation available [here](https://docs.forestadmin.com/documentation/reference-guide/models/relationships/smart-relationship-examples/smart-hasmany-relationship-in-mongodb) doesn’t mention the `lookup` function.

Let me know if that helps.

---

<div class="post-metadata">

**Author:** ![Giri\_S](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/giri_s/32/3930_2.png) [@Giri\_S](https://community.forestadmin.com/u/Giri_S)\
**Post date:** [November 24, 2022, 1:56pm UTC](https://community.forestadmin.com/t/need-to-create-smart-relationship-between-two-collections-that-exist-in-different-database-with-mongodb/5557/19 "2022-11-24T13:56:05Z")

</div>

@anon39848301 Thanks a lot, i managed to do it without a lookup.
