Skip to content

Commit ce352c1

Browse files
Merge pull request #1475 from teleginzhenya/feature/mongoose-prop-ref-example
Mongoose @prop decorator ref example
2 parents 7ef0983 + f36f337 commit ce352c1

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ Footer should contain a [closing reference to an issue](https://help.github.com/
169169
Samples: (even more [samples](https://github.com/nestjs/nest/commits/master))
170170
171171
```
172-
docs(changelog) update change log to beta.5
172+
docs(changelog): update change log to beta.5
173173
```
174174
```
175-
bugfix(@nestjs/core) need to depend on latest rxjs and zone.js
175+
bugfix(@nestjs/core): need to depend on latest rxjs and zone.js
176176
177177
The version in our package.json gets copied to the one we publish, and users need the latest of these.
178178
```

content/techniques/mongo.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ Alternatively, the `@Prop()` decorator accepts an options object argument ([read
7272
name: string;
7373
```
7474

75+
In case you want to specify relation to another model, later for populating, you can use `@Prop()` decorator as well. For example, if `Cat` has `Owner` which is stored in a different collection called `owners`, the property should have type and ref. For example:
76+
77+
```typescript
78+
import { Types } from 'mongoose';
79+
import { Owner } from '../owners/schemas/owner.schema';
80+
81+
@Prop({ type: Types.ObjectId, ref: Owner.name })
82+
owner: Owner;
83+
```
84+
85+
In case there are multiple owners, your property configuration should look as follows:
86+
87+
```typescript
88+
@Prop({ type: [Types.ObjectId], ref: Owner.name })
89+
owner: Owner[];
7590
Finally, the **raw** schema definition can also be passed to the decorator. This is useful when, for example, a property represents a nested object which is not defined as a class. For this, use the `raw()` function from the `@nestjs/mongoose` package, as follows:
7691

7792
```typescript

0 commit comments

Comments
 (0)