Automattic / Automattic/mongoose

SUGGESTION for v5: Populate should avoid mutating properties

Open
#5,934 13 comments 5 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

IMHO, populate mutating an _id into a document is wrong. Mutations make building CRUD services more difficult than it should be because the object populated cannot be sent back with modifications without further processing to restore the id.

If possible, I would like to see an `alias` in v5 so that instead of populating the id, it would populate a new property named as defined in the `alias` as shown herebelow:

```
var storySchema = Schema({
authorId: { type: Schema.Types.ObjectId, ref: 'Person', alias: 'author' },
title: String,
});
```

This would be the equivalent of AS in SQL Queries.

Without designating an `alias`, the behavior would remain the same as in v4 to stay compatible with previous versions.

Contributor guide

Open the contributing guide

Research direction

Start by tracing the existing populate behavior and schema path handling in the Mongoose codebase; the issue does not identify specific files or tests. Compare the proposed alias behavior with current v4-compatible behavior, and consider the feature done when populated values use the configured alias without mutating the original _id semantics and existing behavior remains unchanged without an alias.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.