user3836476
user3836476

Reputation: 1145

Mongodb Nested model

My model looks like following

User Model
{
    UserName:'string',
    Mobile: string
}

Post Model
{
    Post:string,
    Date: { type: Date, default: Date.now },
    User: 'ObjectId'
}

While getting data How can I get result like.

posts=[
{Post:'abc',Date:'10/10/2016',User:{UserName:'testuser1',Mobile:'9090123456'}},
{Post:'abc',Date:'10/10/2016',User:{UserName:'testuser2',Mobile:'9090123457'}}
]

Upvotes: 1

Views: 128

Answers (1)

Asif Saeed
Asif Saeed

Reputation: 2045

You have to add

User : {type : Schema.Types.ObjectId,ref:'User'}

in your Post Model

and when you query you have to do

db.Post.find({}).populate('User')

Upvotes: 3

Related Questions