Automattic / Automattic/mongoose

Automatic field renaming

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

Description

### Prerequisites

- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the feature has not already been requested

### 🚀 Feature Proposal

While expending the scope of the codebase, we had to populate a lot of thing.
Lot of regression happened because we might need to display something of the populated field but elsewhere just need to check an id. It's realy hard to maintain populated or not field accross the codebase.

I saw [Populate Virtuals](https://mongoosejs.com/docs/populate.html#populate-virtuals) that can resolve this issue but we need to duplicate every field with relation.

Maybe we could have someting like :

### Motivation

less regression and boilerplate.

### Example

```js
await project.findOne({ _firmId: req.user._firmId, _id: projectId })
.populate({
path: 'staffingSetup.list.resourceList.resourceId',
select: 'firstname lastname picture',
as: 'staffingSetup.list.resourceList.resource' // or populated: 'staffingSetup.list.resourceList.resource'
}).lean()
```

and
```js
const ProjectSchema = new Schema({

display: { Type: String },
staffingSetup: [new Schema({
positionId: { type: Schema.ObjectId, ref: 'Job' },
list: [
new Schema({

staffingPeriod: [new Schema({
days: Number,
startDate: { type: Date },
endDate: { type: Date }
})],
resourceList: [new Schema({
resourceId: { type: Schema.ObjectId, ref: 'Resource', index: true, as: 'resource' }, // or populated: 'resource' that will be the default name instead of resourceId
status: Number
})],
})]
})],
})
```

Contributor guide

Open the contributing guide

Research direction

The proposal names Mongoose Populate Virtuals, Schema definitions, and populate() options but no repository files or tests. Start by reading the referenced Populate Virtuals documentation and tracing the existing schema and populate APIs; done would require an agreed automatic field-renaming design and validation of the shown usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, node.js
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.