# Relationships in MongoDB not working (with Mongo Atlas)

**URL:** <https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424>\
**Category:** Help me!\
**Created:** [June 7, 2020, 11:25am UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424 "2020-06-07T11:25:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Solias99](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/solias99/32/115_2.png) [@Solias99](https://community.forestadmin.com/u/Solias99)\
**Post date:** [June 7, 2020, 11:25am UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/1 "2020-06-07T11:25:22Z")

</div>

We’re using MongoDB Atlas as a cloud service for our database. However as you all know, MapReduce isn’t supported for anything below M10 clusters.

We followed the “Enrich your models” guide in the documentation, and generated schemas. Our data is not showing successfully on Forest Admin. However, references/relationships are not working, no matter what we do.

A simple example:  
customer hasMany subscriptions

But under a given customer, the list of subscriptions refuse to show.

Further information:  
It feels like the reference is somewhat successfully being built, as there is an option to “Create subscription” where no subscriptions are shown for a customer. When we fill out the form, the subscription is successfully created too. But the extended views (list of subscriptions for a customer, and the customer details for a given subscription) just refuses to show up.

Any help will be duly appreciated! 🙂

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

---

<div class="post-metadata">

**Author:** ![anon37102731](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@anon37102731](https://community.forestadmin.com/u/anon37102731)\
**Post date:** [June 8, 2020, 10:13am UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/2 "2020-06-08T10:13:28Z")

</div>

Hi @Solias99,

Thank you for reaching out and sharing your issue.

I’m not quite sure to understand, appart from your related data, is the rest of your collection accessible and displaying the correct data?

Also to assist you I might need to take a look at your model definition. Could you share it?

---

<div class="post-metadata">

**Author:** ![Solias99](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/solias99/32/115_2.png) [@Solias99](https://community.forestadmin.com/u/Solias99)\
**Post date:** [June 8, 2020, 10:59am UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/3 "2020-06-08T10:59:50Z")

</div>

Sure thing.

The collections individually, (Customers and Subscriptions) are accessible and display everything right. Only the references don’t work, in the sense that when I navigate to a customer’s subscriptions, it’s an empty list, even though in the below screenshot you can see that the customer has a bunch of subscriptions.

Customer Schema

```javascript
const schema = mongoose.Schema({ 
     subscriptions: [{ type: mongoose.Schema.Types.ObjectId, ref: 'subscriptions'}] 
})

```

There is no `belongsTo` in the relationship.

![Subscriptions](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/5026ecbdfc66f20179863c570eae79cd4577d271.png)

---

<div class="post-metadata">

**Author:** ![anon37102731](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@anon37102731](https://community.forestadmin.com/u/anon37102731)\
**Post date:** [June 8, 2020, 3:06pm UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/4 "2020-06-08T15:06:27Z")

</div>

@Solias99, I haven’t been able to reproduce the issue.

As I can understand from your screenshot, your `Customers` model does have an array of subscriptions as one of its field right? (as stated in our [documentation](https://docs.forestadmin.com/documentation/reference-guide/relationships#adding-a-hasmany-relationship), it’s mandatory to have your relationship displayed in the related data section)

---

<div class="post-metadata">

**Author:** ![Solias99](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/solias99/32/115_2.png) [@Solias99](https://community.forestadmin.com/u/Solias99)\
**Post date:** [June 9, 2020, 6:57am UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/5 "2020-06-09T06:57:07Z")

</div>

Yes, the screenshot I posted above is one document in my Customers’ collection. You can see that an array of subscription IDs is present.

Have you tried reproducing the issue using Atlas? Specifically, without mapReduce, so you’ll have to write all the models/schemas yourself.  
Is there some way we can get in contact off this platform? I am confident it’s a 5-minute fix, but it could also potentially be a breaking bug on your end.

Edit: I’ve changed the way the a primary key is assigned to the Customers table (Instead of Mongo’s default id, it’s a 10 digit number coming from somewhere else. Could that be a potential reason for this whole setup breaking?

---

<div class="post-metadata">

**Author:** ![Sliman\_Medini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sliman_medini/32/19_2.png) [@Sliman\_Medini](https://community.forestadmin.com/u/Sliman_Medini)\
**Post date:** [June 10, 2020, 8:35am UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/6 "2020-06-10T08:35:45Z")

</div>

Hello @Solias99,

Your \_id should be ObjectID, it’s very recommended. It offers features and is very well managed by Mongo (and Forest). [https://docs.mongodb.com/manual/reference/method/ObjectId/](https://docs.mongodb.com/manual/reference/method/ObjectId/)

Further of that, let’s consider your “customers” and “subscriptions” collections.  
-The customer schema is good (but where are the other fields, they should be together here)  
-The data extract you showed in your last screenshot indicates in a correct way a customer document having many subscriptions.

Please try to generate your schema locally (with a local Mongo) before deploying it, if it doesn’t work manually.

Regards

---

<div class="post-metadata">

**Author:** ![Solias99](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/solias99/32/115_2.png) [@Solias99](https://community.forestadmin.com/u/Solias99)\
**Post date:** [June 10, 2020, 1:46pm UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/7 "2020-06-10T13:46:39Z")

</div>

Hey,

Guess we’ll change it to the default ObjectID then, and see if it fixes stuff.

Regarding your additional points,  
-I omitted the rest of the fields for brevity,  
-Yup. That’s why I’m confused as to why the Subscriptions woulddn’t be showing up for that customer. Not using ObjectID for customer’s `_id` shouldn’t necessarily break that, right?

---

<div class="post-metadata">

**Author:** ![Sliman\_Medini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sliman_medini/32/19_2.png) [@Sliman\_Medini](https://community.forestadmin.com/u/Sliman_Medini)\
**Post date:** [June 10, 2020, 1:57pm UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/8 "2020-06-10T13:57:57Z")

</div>

yes, since it’s customers’id that was changed, it should not mess with customers.subscriptions -\> subscriptions.

If your problem persists, can you have a try with a local mongo?  
Like that relationship will be generated (and if it’s not generated with a local mongo, it means something weird is in the data, and we will look at it together).

---

<div class="post-metadata">

**Author:** ![Solias99](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/solias99/32/115_2.png) [@Solias99](https://community.forestadmin.com/u/Solias99)\
**Post date:** [June 10, 2020, 2:12pm UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/9 "2020-06-10T14:12:31Z")

</div>

May I know what you mean by local mongo? Right now we have a full-fledged back-end with an endpoint to fetch all subscriptions for a customer and it works fine. (Still connected to Atlas)

You want me to connect to `localhost:27017` and test right?

---

<div class="post-metadata">

**Author:** ![Sliman\_Medini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sliman_medini/32/19_2.png) [@Sliman\_Medini](https://community.forestadmin.com/u/Sliman_Medini)\
**Post date:** [June 10, 2020, 2:20pm UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/10 "2020-06-10T14:20:26Z")

</div>

Yes, if you can do that, just for debugging purpose.

Like that you will have your schema generated with relationship using the mapReduce provided by a local database.  
Mongo can be shipped locally quickly in a docker container.

---

<div class="post-metadata">

**Author:** ![Solias99](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/solias99/32/115_2.png) [@Solias99](https://community.forestadmin.com/u/Solias99)\
**Post date:** [June 10, 2020, 2:42pm UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/11 "2020-06-10T14:42:15Z")

</div>

Alright I’ll look into it.

Meanwhile is it possible that you can try reproducing this error on your side as well? (without mapReduce)

Problem statement:

- Don’t use ObjectIds for primary key, use a number instead.

EDIT: By the way this above problem statement raises another problem I wanted to bring up: I can’t create a customer through ForestAdmin at all! It says “\_id” is required even though \_id is a locked/disabled field when creating a document.

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

EDIT2: This is somewhat intentional however. We don’t ever want to be create customers through here. It happens in some other microservice where we get a uniqueID. Just wondering if this problem is correlated with the `customers.subscriptions->subscriptions` problem

---

<div class="post-metadata">

**Author:** ![Sliman\_Medini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sliman_medini/32/19_2.png) [@Sliman\_Medini](https://community.forestadmin.com/u/Sliman_Medini)\
**Post date:** [June 10, 2020, 2:49pm UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/12 "2020-06-10T14:49:42Z")

</div>

Oh yes, hacking default \_id in Mongo is really _really_ not recommended.

---

<div class="post-metadata">

**Author:** ![Solias99](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/solias99/32/115_2.png) [@Solias99](https://community.forestadmin.com/u/Solias99)\
**Post date:** [June 10, 2020, 2:52pm UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/13 "2020-06-10T14:52:25Z")

</div>

Looks like we’ll have to change our strategy and stick to default `_id` then. Will try that later in the evening, see if the references are working and then get back to you. (Will also try local mongo once)

---

<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:** [June 12, 2020, 9:33am UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/14 "2020-06-12T09:33:40Z")

</div>

Hey @Solias99 👋  
Did you get more success trying to stick to the default Mongo `_id`?

---

<div class="post-metadata">

**Author:** ![Solias99](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/solias99/32/115_2.png) [@Solias99](https://community.forestadmin.com/u/Solias99)\
**Post date:** [June 12, 2020, 10:05am UTC](https://community.forestadmin.com/t/relationships-in-mongodb-not-working-with-mongo-atlas/424/15 "2020-06-12T10:05:08Z")

</div>

Hey @anon34731316,

Yes, we got everything to work with default `_id`.

Maybe I can raise some sort of PR for the documentation regarding working without them? It’s not all that uncommon for people to replace `_id`, I’ve done it before for various purposes, including (but not limited to) replacing them in favour of IDs being generated in another application.
