Automattic / Automattic/mongoose
Support square bracket notation for `Document.prototype.set()`: `set('arr[0].path', val)`
- 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?**
Cannot set a specific path
**If the current behavior is a bug, please provide the steps to reproduce.**
```javascript
const mongoose = require('mongoose')
mongoose.set('debug', true)
const { Schema, model } = mongoose
const { Mixed } = Schema.Types
const VariableSchema = new Schema({
type: String,
valueType: String,
value: Mixed,
displayName: String,
modifier: String,
})
const StepSchema = new Schema({ url: String }, { discriminatorKey: `type` })
const Step = model(`Step`, StepSchema)
const LoopConfigurationSchema = new Schema({
loopItemsQueryPaths: [[VariableSchema]],
})
const LoopSchema = new Schema({
configurations: {
type: [LoopConfigurationSchema],
},
})
Step.discriminator(`loop`, LoopSchema)
mongoose
.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(async () => {
let doc = await Step.create({
type: `loop`,
configurations: [
{
loopItemsQueryPaths: [],
},
],
})
doc.set({
'configurations[0].loopItemsQueryPaths': [
[
{
type: 'object',
valueType: 'qbManualEntry',
value: '//div',
},
],
],
})
await doc.save()
console.dir(doc.configurations[0].loopItemsQueryPaths) // print []
process.exit()
})
.catch((err) => {
console.error(err)
process.exit(1)
})
```
**What is the expected behavior?**
`doc.configurations[0].loopItemsQueryPaths` should be set, right now it is `[]`
The gist is pretty big but I tried to make it as small as I could. I have re-read everything to make sure there was no typo too.
It looks similar to another [issue](https://github.com/Automattic/mongoose/issues/11428) I opened recently
**What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.**
Node 6.13.2
Mongoose 6.2.7
Mongo 5.0.5
Contributor guide
Research direction
Start at the Document.prototype.set() entry point and reproduce the supplied nested-array example using the stated Mongoose, Node.js, and MongoDB versions. Compare the square-bracket path with the existing dot-path behavior and review issue 11428; done means configurations[0].loopItemsQueryPaths contains the value after set() and save().
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb, nodejs
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100