Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The correct way of storing document reference in one-to-one relationship in MongoDB

I have two MongoDB collections user and customer which are in one-to-one relationship. I'm new to MongoDB and I'm trying to insert documents manually although I have Mongoose installed. I'm not sure which is the correct way of storing document reference in MongoDB.

I'm using normalized data model and here is my Mongoose schema snapshot for customer:

/** Parent user object */
user: {
    type: Schema.Types.ObjectId,
    ref: "User",
    required: true
}

user

{ 
    "_id" : ObjectId("547d5c1b1e42bd0423a75781"), 
    "name" : "john", 
    "email" : "[email protected]", 
    "phone" : "01022223333", 
}

I want to make a reference to this user document from the customer document. Which of the following is correct - (A) or (B)?

customer (A)

{ 
    "_id" : ObjectId("547d916a660729dd531f145d"), 
    "birthday" : "1983-06-28", 
    "zipcode" : "12345", 
    "address" : "1, Main Street", 
    "user" : ObjectId("547d5c1b1e42bd0423a75781")
}

customer (B)

{ 
    "_id" : ObjectId("547d916a660729dd531f145d"), 
    "birthday" : "1983-06-28", 
    "zipcode" : "12345", 
    "address" : "1, Main Street", 
    "user" : {
        "_id" : ObjectId("547d5c1b1e42bd0423a75781")
    }
}
like image 327
Sithu Avatar asked Dec 02 '14 10:12

Sithu


People also ask

How can we represent a one-to-one relationship in MongoDB?

In MongoDB, a relationship represents how different types of documents are logically related to each other. Relationships like one-to-one, one-to-many, etc., can be represented by using two different models: Embedded document model. Reference model.

How do I reference a document in MongoDB?

MongoDB applications use one of two methods to relate documents: Manual references save the _id field of one document in another document as a reference. Your application runs a second query to return the related data. These references are simple and sufficient for most use cases.

Which format is used by MongoDB for storing documents?

MongoDB stores data in BSON format both internally, and over the network, but that doesn't mean you can't think of MongoDB as a JSON database.

How can documents be stored in MongoDB?

Documents are stored on disk using block compression to reduce storage usage. Documents are automatically uncompressed in memory when retrieved by the MongoDB server. Each collection & index is stored in a separate file within the storage.


2 Answers

Use variant A. As long as you don't want to denormalize any other data (like the user's name), there's no need to create a child object.

This also avoids unexpected complexities with the index, because indexing an object might not behave like you expect.

Even if you were to embed an object, _id would be a weird name - _id is only a reserved name for a first-class database document.

like image 65
mnemosyn Avatar answered Sep 26 '22 07:09

mnemosyn


Remember these things

Embedding is better for...

  • Small subdocuments
  • Data that does not change regularly
  • When eventual consistency is acceptable
  • Documents that grow by a small amount
  • Data that you’ll often need to perform a second query to fetch Fast reads

References are better for...

  • Large subdocuments
  • Volatile data
  • When immediate consistency is necessary
  • Documents that grow a large amount
  • Data that you’ll often exclude from the results
  • Fast writes

Variant A is Better. you can use also populate with Mongoose

like image 21
Barno Avatar answered Sep 25 '22 07:09

Barno