Answer the question
In order to leave comments, you need to log in
How to make a Many to Many relationship in Sails.js so that a separate collection is not created in MongoDB?
Good afternoon. The problem is that when creating many-to-many relationships in the standard way, Waterline creates a new collection in MongoDB, instead of creating arrays in documents with identifiers pointing to specific documents from the related collections, which in my opinion is a bit contrary to the concept of mongo. I read the Waterline documentation, and did not find a way to create links without an extra collection. Maybe someone faced a similar problem? I don't want to make Mongi look like a relational database.
An example as you wish
//Документ в коллекции Pet
{
"_id": "5507168dc2bf59812137d80c",
"name": "Hugin",
"owners": ["550715ccc2bf59812137d806", "550715f8c2bf59812137d807"]
}
//Документы в коллекции Human
[{
"_id": "550715ccc2bf59812137d806",
"name": "Karry",
"pets": ["5507168dc2bf59812137d80c"]
},
{
"_id": "550715f8c2bf59812137d807",
"name": "Stephen",
"pets": ["5507168dc2bf59812137d80c", "5507168dc2bf59812137d80d"]
}]
{
"_id": "5507168dc2bf59812137d80e",
"humans_pets": "550715f8c2bf59812137d807",
"pets_humans": "5507168dc2bf59812137d80c"
}
Answer the question
In order to leave comments, you need to log in
@particlebanana: Right now waterline-schema builds a relational schema for your models. Adapters that can handle schemaless data can be updated to transform queries and criteria to use this sort of data structure.
Currently the mongo adapter doesn't do this and will store data in a "join table" just like mysql or postgresql. I'd happily accept a PR to fix this for mongo though. I started on it a while back but didn't have the time to get very far with it, should be in the commit history though.
Waterline is currently building relational relationships for your models. Adapters can modify schema information to transform queries and criteria to work with their own structure.
Currently, the mongo adapter does not do this and stores data in an "interim table" like MySQL or PostgreSQL. I would gladly accept a Pull Request to fix this for monga. Some time ago, I started working on it, but didn't have time to make enough headway in that direction. Should be somewhere in the commit history.
The relational model was not initially designed for many-to-many relationships, so, alas, one cannot do without a separate table.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question