Automattic / Automattic/mongoose

Automatic denormalization / embedding

Open
#8,530 0 comments 5 reactions 0 assignees View on GitHub
new feature
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 5h
Merged PRs (30d)
32

Description

`populate()` makes it easy to pull in data from another collection, but in Mongoose 5.x you're on your own for embedding data.

If you're pulling a blog post's author from a 'User' collection, the below works fine.

```javascript
const blogPostSchema = Schema({
title: String,
content: String,
authorId: String,
tags: [String]
});

blogPostSchema.virtual('author', {
ref: 'User',
localField: 'authorId',
foreignField: '_id',
justOne: true
});
```

But if you want to embed `author`, it's up to you to set up middleware. Would be great if Mongoose could handle this:

```javascript
const blogPostSchema = Schema({
title: String,
content: String,
authorId: String,
author: {
type: UserSchema.pick(['name', 'email']),
embed: 'User',
localField: 'authorId',
foreignField: '_id'
},
tags: [String]
});
```

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the populate() and middleware behavior referenced in the issue, then trace how schema virtuals and embedded fields are defined. Compare the proposed blogPostSchema example with existing Mongoose schema and population tests. Done should mean a supported automatic denormalization or embedding workflow matching the requested configuration, with documented behavior and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.