Skip to content

different behavior for key starting with dot ('.') #14178

Closed
@rohanricky

Description

@rohanricky

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.12.3

Node.js version

18.12.1

MongoDB server version

4.4

Typescript version (if applicable)

No response

Description

var mongoose = require('mongoose')
console.log(mongoose.version)
let Schema = mongoose.Schema
var eventSchema = new Schema({
  name: { type: String },
  field3: { type: Schema.Types.Mixed },
  __stateBeforeSuspension: {
    field1: { type: String },
    field2: { type: String },
    field3: { type: Schema.Types.Mixed },
    jsonField: {
        name: { type: String },
        ex: { type: String }
    }
  }
})

eventSchema.add({
  test: { 
    val1: { type: String },
    val2: { type: String }
   }
})

eventSchema.add({
  subSchema4: [{ field1: { type: String } }, { field2: { type: String } }]
})

var Event = mongoose.model('Event', eventSchema)
async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  let eventObj = new Event({ name: 'event object', __stateBeforeSuspension: { field1: 'test', jsonField: { name: null, ex: 'test' }}, test: { val1: 'hello', val2: 'there' }, subSchema4: [{ field1: 'test' }, { field2: 'test2' }] })
  eventObj.save(function (err, eventObj) {
    const newO = eventObj.toObject()
    newO.__stateBeforeSuspension.field3 = {'.ippo': 5}
    eventObj.set(newO)
    eventObj.save(function (err, eventObj) {
      console.log(eventObj)
    })
  })
}
run();

Output with 6.12.3

{
  name: 'event object',
  __stateBeforeSuspension: {
    field1: 'test',
    field3: { '': [Object] },
    jsonField: { name: null, ex: 'test' }
  },
  _id: new ObjectId("657ae93de1d6bf5012d5e889"),
  test: { val1: 'hello', val2: 'there' },
  subSchema4: [
    { field1: 'test', _id: new ObjectId("657ae93de1d6bf5012d5e88a") },
    { _id: new ObjectId("657ae93de1d6bf5012d5e88b") }
  ],
  __v: 0
}

Output with 5.12.5

{
  __stateBeforeSuspension: {
    jsonField: { name: null, ex: 'test' },
    field1: 'test',
    field3: { '.ippo': 5 }
  },
  _id: 657ae987986e7d5f9d5943e5,
  name: 'event object',
  test: { val1: 'hello', val2: 'there' },
  subSchema4: [
    { _id: 657ae987986e7d5f9d5943e6, field1: 'test' },
    { _id: 657ae987986e7d5f9d5943e7 }
  ],
  __v: 0
}

Steps to Reproduce

__stateBeforeSuspension.field3 in 5.12.5 : { '.ippo': 5 }
__stateBeforeSuspension.field3 in 6.12.3 : { '': [Object] } ( the [Object] is {'ippo': 5}, key is empty )

Expected Behavior

Expecting 5.12.5 behavior will be supported

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions