Automattic / Automattic/mongoose

Support `prop: [schema1, schema2]` as a way of defining an array of length 2 with subdoc 0 having `schema = schema1` and subdoc 1 having `schema = schema2`

Open
#9,972 1 comment 0 reactions 0 assignees View on GitHub
new feature
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

**Do you want to request a *feature* or report a *bug*?**
Bug
**What is the current behavior?**
The schema will create 2 objects as scripted, but it will iterate twice with the values defined for object 1 in a children array.
**If the current behavior is a bug, please provide the steps to reproduce.**

```
const mongoose = require('mongoose');

const subAddressAV = new mongoose.Schema(
{
nameAV: {
type: String,
default: "Available"
},
colorAV: {
type: String,
default: "rgb(40%, 100%, 40%)"
},
available: {
type: Number,
// required: [true, 'Please Pick a Number'],
default : 0,
min: 0,
max: 100
}
}
);
const subAddressNV = new mongoose.Schema(
{
nameNV: {
type: String,
default: "NonAvailable"
},
colorNV: {
type: String,
default: "rgb(100%, 83.5%, 0%)"
},
nonavailable: {
type: Number,
// required: [true, 'Please Pick a Number'],
default : 0,
min: 0,
max: 100
}
}
);

const subAddress = new mongoose.Schema({
name: {
type: String,
// required: [true, 'Please add a Name'],
unique: true,
maxLength: [20, 'Name cannot be more than 20 characters']
},
description: {
type: String,
// required: true,
maxLength: [400, 'Description cannot be more than 400 characters']
},
color: {
type: String,
// required: true,
},
createdAt: {
type: Date,
default: Date.now
},
children: [subAddressAV, subAddressNV]
});

const NoteSchema = new mongoose.Schema({
team_title: {
type: String,
default: "TEAM"
},
team_description: {
type: String,
default: "We!!"
},
team_color: {
type: String,
default: "rgb(50.2%, 0%, 50.2%)"
},
children: [subAddress]
})

module.exports = mongoose.models.Note || mongoose.model('Note', NoteSchema);

```

N/A
**What is the expected behaviour?**
The behaviour: it should return and assign the values for both objects when doing a POST request
the Post request:
```
{
"children": [
{
"name": "swwss",
"description": "sswws s",
"children": [
{
"nameAV": "sswwws",
"colorAV": "rbg(ww)",
"available":10
},
{
"nameNV": "sss",
"colorNV": "rbg(sswwss)",
"nonavailable":11
}
]
}
]
}
```
and te return is totally wrong:

```
{
"success": true,
"data": {
"team_title": "TEAM",
"team_description": "We!!",
"team_color": "rgb(50.2%, 0%, 50.2%)",
"_id": "603a78cbea92dc2cd76cf198",
"children": [
{
"_id": "603a78cbea92dc2cd76cf199",
"name": "swwss",
"description": "sswws s",
"children": [
{
"nameAV": "sswwws",
"colorAV": "rbg(ww)",
"available": 10,
"_id": "603a78cbea92dc2cd76cf19a"
},
{
"nameAV": "Available",
"colorAV": "rgb(40%, 100%, 40%)",
"available": 0,
"_id": "603a78cbea92dc2cd76cf19b"
}
],
"createdAt": "2021-02-27T16:52:27.747Z"
}
],
"__v": 0
}
}
```
more information: https://stackoverflow.com/questions/66306598/next-js-mongoose-schema-wrong-children-values-at-a-post-request
**What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.**
I am using studio3t (localhost - mongo)
```
"dependencies": {
"d3": "5.12.0",
"isomorphic-unfetch": "^3.1.0",
"mongoose": "^5.11.17",
"next": "10.0.6",
"react": "17.0.1",
"react-dom": "17.0.1",
"semantic-ui-css": "^2.4.1",
"semantic-ui-react": "^2.0.3",
"sunburst-chart": "1.11.2"
}
```
and
```
node --version
v15.8.0

```

is this possible at all, to have 2 objects in a child schema array?

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the behavior with the standalone Mongoose schema and POST request shown in the issue, then compare the requested heterogeneous children array with the current returned document. The StackOverflow example provides additional context; done means both child objects retain their respective schema fields and values.

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.