I am starting in Mongodb
, and as you know this does not have relaciones
, there is no joins
tablas
etc.
I'm trying to pass a relational diagram to NOSQL
. I have the following tables.
users
id
name
messages
idUserFrom
idUserTo
messages
As you can see, the table messages
has as foreign the id of users
, how can I create it although there is not in mongo pks
and fks
how can I carry out this step.
So far I have created the database and one collection
with the insertion of the table users
.
> use mongodb
switched to db mongodb
> db
mongodb
> db.users.insert({id:"1",name:"Jorge"})
WriteResult({ "nInserted" : 1 })
> show collections
users
> db.users.find().pretty();
{
"_id" : ObjectId("5c9bbc275a56aa679bb06155"),
"id" : "1",
"name" : "Jorge"
}
collections
How can I make it look like this messages
: This table contains the id of some user, which can be the idFrom
oridTo
db.messages.find().pretty();
{
"_id" : ObjectId("5c9bbc275a56aa679bb06155"),
"idFrom" : "xxxxxxxxxx",
"idTo" : "xxxxxxxxxx",
"message" : "Hola"
}
In MongoDB there are relationships between collections. What happens is that Mongo is a noSQL database . This means that it does not use Structured Query Language (or SQL in English). Therefore, there is no sentence like
SELECT * FROM table
, since it is typical of that language.On the other hand, the term " non-relational database" tends to be confused with the absence of relationships. The truth is that Mongo does allow you to implement relationships, but through something known as references . There is even calm talk of cardinality of relations, with the same characteristics that we know in a "relational" database. The particularity of Mongo (as well as many other noSQL DBs ) is that it does not fit or adhere faithfully to the ER (Entity-Relationship) model, hence its description as a non-relational DB .
ISSUE
You want to create a relationship between two Mongo collections. One collection, called users , will have a relationship with another collection called messages . Each type document is required to
message
have a reference in two of its fields to a type documentuser
.SOLUTION
To achieve what we propose, we must create a reference to
user
in each field of the type documentmessage
where we want to create the relationship. Mongo offers us two types of ReferencesManual References
They are the ones used by placing the value of
_id
a document in the field of another document. Then, if you want to obtain the associated document_id
, you must make a second query to the database using the information of said value.Suppose we create the collection
users
as follows:If we want to see the objects of the collection
users
that we just created, we will use the methodfind()
:We can see that Mongo has assigned the values
_id
as typeObjectId
. It does this automatically unless we_id
explicitly pass it a value for the field.Now we will create the collection
messages
and enter the reference manually. We achieve this in the following way:(we have used the corresponding values
_id
)If we want to see the documents in the collection,
messages
we use the method againfind()
:Now we have manually related documents in the collection
messages
to documents in the collectionusers
.Many times these types of manual relationships will suffice for our application. However, to know the name or email of the user who sent the message or the user who received the message, we must make a second query to the database:
In the previous query I have used a
query
({"_id" : ObjectId("5c9cce340aee604c4ab6cd08")}
) in the methodfind()
, and I have also passed aprojection
({"_id":0, "idFrom": 1}
) as a parameter.If we wanted to do a similar query in SQL, it could be written like this:
You can find more information about the method
find()
in the documentationDBRef References
DBRefs is a convention used to represent a document, rather than a specific (manual) reference type like the one we just saw. References of this type include the name of the collection to which the document belongs, the value of
_id
the document field, and may include the name of the database to which the collection belongs.A document
DBRef
has the following format: (it is important to respect the order of the fields)To use a type reference
DBRef
in our case we can do it as follows:If we make a query to our collection
messages2
we obtain the following:In this way we have created the relationship between the collection
messages2
and the collectionusers
usingDBRefs
.Again, to know the name or email of the user that sends or receives, a second query must be made to the database
JOIN
Now, since the question asks if JOINs can be done in Mongo, the answer is: YES, using a method called
aggregate()
, which as its translation supposes, adds something to our query. In this case we can add the documents associated with the in the e_id
fields of the collection . This type of is known as . This way we have a way to fetch the data in a single query.idFrom
idTo
messages
aggregation
$lookup aggregation
A simple example of this can be seen in the following query:
Basically we are telling Mongo to lookup the documents in the collection
users
whose_id
matches the fieldidFrom
eidTo
and add them to the returned document in a fieldsender
and a fieldreceiver
.This translated to SQL is a simple JOIN, or maybe a query of the type:
As you can see, almost everything that can be done with SQL can be done in Mongo.
Hope this is the answer you were looking for.