How is it being link or reference?

I have a question how is it being reference the order collection and the user collection?, because the user has an _id given by mongo and the order is going to be for that user, but all that extra information where is it coming? like the shipping_address? is it being by the reference? and how? thank you so much

the shipping address could be in a separate “address” collection by a reference, but that would be a poor choice when it comes to query orders as the “lookup” would require extra round trip to the “address” collection.

when you embed it into its own order document, your query just becomes extremely fast to fetch. embedding is done on the client side when you create the document. you could also add basic information about the user, for example, such as name and phone number. the important thing is not to embed too many things into a document, especially an uncontrollably growing array content.

1 Like

Thank you so much @Yilmaz_Durmaz now it’s more clear, I have one more question that is blowing my mind, how is it happening that the order collection is getting the customer_id, from the customer collection, and how is being created that link between them, because until know what i have being reading about the $lookUp it needs an already filled collection right?

what you described is the basis of an app on the client side:

  • you need items for your shop to sell, so you create an items collection.
    • you buy items from suppliers and add details to the items collection.
  • you need users to buy things from your site, so you create a users collection
    • a user signs up, so you add the user’s details to the user collection.
  • user signs in to the site for shopping
    • you get user details from the user collection
    • you get items from the items collection and show them to the user, preferably with paging
    • user adds items to the cart
  • you need to process user orders so you create an orders collection, preferably before users start coming :slight_smile:
    • you create an order document with at least the user ID, cart items’ IDs, and shipping address which are required fields so you can process the order.

you can see the order of creation of documents. you cannot have an order without a user and without items.

and, by the way, in relational databases, you have to define primary-foreign key relations to make joins and have to have a document for one leg of many-or-one relation before inserting the other leg. this is not true in mongodb. lookup does not need a “filled” collection. you can have an empty collection and your query will just return an empty result.


OK now it is more visible, but i am still not getting it :frowning: , lets say that the user is creating the order document, so how am I going to create that link between this two collections, is it with the $lookUp, like I must pass the _id of the user to the order document and how?

Like in realation database that we have PK and FK for creating those relations, how is it in Mongo when we are talking about link between two collections?

Thank you for your time and sorry

yes, you embed the user ID into the order. that’s it. when the user logs in, you have already queried the user details so you already have the ID for more operations. that includes embedding that into order document. The following is perfectly fine for this purpose.

order_id: o123,
user: {id: u123, name:"someone", phone:42},
     {id: i123, name:"some item", price:10, quantity: 4},
     {id: i321, name:"some other item", price:20, quantity: 2}
shipping_address:{ address_id:a123, city: "somewhere", state: "on", country: "earth"}

In its current shape, all the basic details to process the order are embedded. user? already in database. items? already in database.

address? embed it in user details or in a separate address collection, or just keep it fully embedded into the order. no matter when you do that, it is perfectly fine.

there is no FK or PK. for example just create an empty document for your needs. every inserted document gets an automatic ID by the server and then use that where ever you need it. update documents as you get more details later.

the final design is your choice. just keep the pros and cons in mind regarding the size of embedding versus the timing of lookups.

1 Like

But how should i embed the user_id into the order collection?, like with the $lookUp or duplicating it? :face_with_monocle:
let say that the user document is:

  _id: "ObjectID",
 username: "string",

and the order document is:

 _id: "ObjectID",
 items: "Array"

so in the document order in its schema i should integrate a new field? like


to create that link? if it is like that, there will be no errors having the user_id in blank?

how familiar are you with Javascript and JSON? MongoDB has the same freedom of object structure, with added types like ObjectId and Date. Else I would suggest you play around with Javascript for a while.

open a “mongo” or “mongosh” connection to your database and apply these command. they are the exact steps I described so far, starting with empty documents. if you have time try to understand them as they are pretty much the basics.

use testdb

var user_id=db.test.insertOne( {} ).insertedId
var user=db.test.findOne( {_id: user_id} )

var item_id=db.test.insertOne( {} ).insertedId
var item=db.test.findOne( {_id:i tem_id} )

var order_id=db.test.insertOne({}).insertedId
var order=db.test.findOne({_id:order_id})

order.address={ city: "somewhere", state: "on", country: "earth"}

db.test.updateOne( {_id:order_id}, { $set: { ...order } } )
db.test.find( {_id:order_id} ).pretty()

the result is pretty much similar to this except IDs.

        "_id" : ObjectId("630e4ece2272ff80311d510c"),
        "address" : {
                "city" : "somewhere",
                "state" : "on",
                "country" : "earth"
        "items" : [
                        "_id" : ObjectId("630e4ec62272ff80311d510b")
        "user_id" : ObjectId("630e4ebb2272ff80311d510a")

for language drivers like mongoose, check their documentation on how to use null/empty/blank entries.


Thank you so much @Yilmaz_Durmaz you just helped me a lot, i am really thankful for your time. Sorry for bothering som. so in resume in my orden collection i must pass the user_id by saving the _id of the user maybe at the localStorage

you may have it in the “state” in memory if it is a React app, Or store in server-side session info, Or keep it in JWT token along with other details and store it in one of browser’s storage systems, might be localStorage as you said.

It all depends on how your frontend works to identify the user.

Good Luck.

PS: Be carefull storing and retrieving all ObjectId values. You might be tempted to query as _id="630e4ece2272ff80311d510c" but it must be _id=ObjectId("630e4ece2272ff80311d510c") that uses ObjectId fucntion.

1 Like

thank you so much for the tips, so one i save the id i should pass it as an ObjectID thankyou a lot

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.