luin
luin

Reputation: 1995

One to One Relationship in MongoDB

Collection blog:

{
    _id: ObjectId( "47cc67093475061e3d95369d" )
    title:
    body:
    author:
}

I wanna know the following two examples which is better as the blog.replies collection.

{
    _id: ObjectId( "47cc67093475061e3d95369d" )
    replies: [
    ]
}


{
    _id: ObjectId( "56a463497875061e5d443607" )
    blog_id: ObjectId( "47cc67093475061e3d95369d" )
    replies: [
    ]
}

thank you!

Upvotes: 1

Views: 1296

Answers (1)

TROODON
TROODON

Reputation: 1175

use example 2

{
    _id: ObjectId( "56a463497875061e5d443607" )
    blog_id: ObjectId( "47cc67093475061e3d95369d" )
    replies: [
    ]
}

For show page:

db.blog.find({"_id" : ObjectId("47cc67093475061e3d95369d")}); // get blog page
db.replies.find({"blog_id" : ObjectId("47cc67093475061e3d95369d")}); // get blog replies

Upvotes: 1

Related Questions