-
Hi, first of all awesome library! How can I create a relation between different collections? Example: In the example folder we have two collections: What if I go into the posts and do something like this:
And another example with the same data.
Is this achievable and I missed it somehow in the docs? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
Hello @SelfDevTV! Actually, in the first example from
In the second example you provided, in the I would recommend reading the official MongoDB docs about document relations - they are very solid:
Does it answer your question? Best regards, |
Beta Was this translation helpful? Give feedback.
-
Hi @pkosiec Thank you for the fast reply. Where in the first example can I find this relation? Maybe you can link me to this specific example you mean where this is set up correctly. The second example: You are right, since a post can have many categories a Category can have many posts. But still how can I relate one to another? How do I access the id's of the posts? Can't find it. Thank you very much, |
Beta Was this translation helpful? Give feedback.
-
Oh, right - sorry, my bad. In the So, it should look like this: Posts: const posts = [
{
id: getObjectId('post1'),
title: 'Lorem ipsum',
description: 'Sample Post 1 description',
tags: ['sample', 'tags'],
author: {
name: 'Author',
email: '[email protected]',
},
categories: [getObjectId("sample"), getObjectId("random")],
comments: [],
creationDate: new Date(),
}
]
module.exports = posts; Categories: module.exports = [
{
id: getObjectId("sample"),
name: "Sample category",
},
{
id: getObjectId("random"),
name: "Random category",
},
{
id: getObjectId("another-one"),
name: "Another category",
},
]; All IDs are predictable, that's why you can specify that your post is assigned to "random" and "sample" categories. I hope now it's clear how it is done. The whole idea is to have predictable IDs, and that's it. And here's what's the const getObjectId = name => {
const hash = createHash('sha1')
.update(name, 'utf8')
.digest('hex');
return new ObjectId(hash.substring(0, 24));
}; So, it creates a hash, and makes an ObjectId instance of part of the hash. We know that the result will be always the same, if the input name is equal. |
Beta Was this translation helpful? Give feedback.
-
Thank you very much. That clears it up for me :) I just thought that this helper always returns a random hash. But when the name is the same the id is the same, now I get it :) Thank you and have a nice day. |
Beta Was this translation helpful? Give feedback.
-
You're welcome! Thanks for pointing it out - I agree that this example should contain one relation for clarity. I will update it for 3.2.1. Greetings from Poland 🙂 |
Beta Was this translation helpful? Give feedback.
-
Tried this out and it works like a charm. Now this lib has everything I need for my projects purposes. Thank you very much again. |
Beta Was this translation helpful? Give feedback.
-
I'm glad you like it 🙂 Enjoy using it! |
Beta Was this translation helpful? Give feedback.
Oh, right - sorry, my bad. In the
examples
directory there are no relations indeed. I thought you've just copied it from the repo, but you actually modified it. And you did it right - this is how the relation should look like, if you want to do it by document references.So, it should look like this:
Posts:
Categories: