Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
rohanricky opened this issue Dec 14, 2023 · 3 comments
Closed
2 tasks done

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

rohanricky opened this issue Dec 14, 2023 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@rohanricky
Copy link
Contributor

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

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Dec 14, 2023
@IslandRhythms
Copy link
Collaborator

var mongoose = require('mongoose')
console.log(mongoose.version)
const Schema = mongoose.Schema
const 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 } }]
})

const Event = mongoose.model('Event', eventSchema)
async function run() {
  await mongoose.connect('mongodb://localhost:27017/gh14178');
  await mongoose.connection.dropDatabase();
  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();

@vkarpov15 vkarpov15 added this to the 6.12.5 milestone Dec 23, 2023
@sakethyerra
Copy link

@vkarpov15 @IslandRhythms, Can you please let us know if there is any tentative time line to release this one.

vkarpov15 added a commit that referenced this issue Dec 28, 2023
fix(document): avoid flattening dotted paths in mixed path underneath nested path
@vkarpov15
Copy link
Collaborator

@sakethyerra we'll ship 6.12.5 with the fix next week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

4 participants