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

Using mongoose just for data validation, set(_id, false) and toObject(), incorrect result #9390

Closed
skiv71 opened this issue Sep 5, 2020 · 2 comments
Milestone

Comments

@skiv71
Copy link

skiv71 commented Sep 5, 2020

Hi, just testing some code, which... to my mind should be straight forward, yet is isn't working as expected...

function Sch(o, insert) {
    if (insert)
        return new Schema(schema)
    var s = new Schema()
    s.set(`_id`, false)
    Object.keys(o)
        .forEach(key => s.add({ [key]: schema[key] }))
    return s
}

async function main(insert) {
    var u = { name: `Neil`, firmId: `5f4ebb71f5332b0011bb0bd5` }
    var Model = mongoose.model(`User`, Sch(u, insert))
    var m = new Model(u)
    m.validate()
    console.log(m.toObject())
}

main()

which, produces...

options: {
    typePojoToMixed: true,
    typeKey: 'type',
    id: true,
    noVirtualId: false,
    _id: false,
    noId: false,
    validateBeforeSave: true,
    read: null,
    shardKey: null,
    autoIndex: null,
    minimize: true,
    discriminatorKey: '__t',
    optimisticConcurrency: false,
    versionKey: '__v',
    capped: false,
    bufferCommands: true,
    strictQuery: false,
    strict: true
  }
}
{
  _id: 5f52e93eeb1953237678ce46,
  name: 'Neil',
  firmId: 5f4ebb71f5332b0011bb0bd5
}

I've set _id false, but I see it also has other flags, but regardless, my output object contains the _id - which I don't want?

@skiv71
Copy link
Author

skiv71 commented Sep 5, 2020

I've tried forcing them other flags, to no avail. Is this a bug in the *.toObject() method?

I see it has options, but none to remove the _id field?

@skiv71
Copy link
Author

skiv71 commented Sep 5, 2020

I can confirm, when creating new Schema(), is i use the args null, { _id: false }, i get the desired result... so the schema.set(o, v) doesn't work (for me)

@vkarpov15 vkarpov15 added this to the 5.10.4 milestone Sep 7, 2020
vkarpov15 added a commit that referenced this issue Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants