Skip to content

Commit 69b2849

Browse files
committed
mongoose populae
1 parent d2a5f15 commit 69b2849

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

MongoDB/populate-method-mongoose.md

+21
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ Arrays of ObjectId refs works like this. Just call the populate method on the qu
103103
And like magic, we have created a unified object using 2 schemas, 2 models, and 2 collections. All of the steps are important of course, but the thing that no other site made explicitly clear was that after setting up the ground work, you have to make sure you are pushing _ids into the field you will need populated later.
104104

105105

106+
### Another implementation
107+
108+
Let’s pretend we’re building a social app, and we have two models: a User and a Post:
109+
110+
```js
111+
var UserSchema = {
112+
_id: String,
113+
username: String
114+
};
115+
116+
var PostSchema = {
117+
_id: String,
118+
user: {
119+
ref: 'User',
120+
type: String
121+
}
122+
123+
```
124+
125+
If you run this query: ``Post.find({}).populate('user').exec(callback)``, Mongoose will look at the field user in the post, see that it has a ref to the User model, and find that user by its _id
126+
106127
107128
### Sources to read
108129

0 commit comments

Comments
 (0)