any reason it's included? i don't want to delete obj.__v
every time i send it down the client.
No particularly good reason that I can think of. It is a little strange that there's no good way to turn this off. Thanks for the suggestion.
can we make it not sent as default?
Don't want to add backwards breaking changes unnecessarily since we're in rc's. I can add the ability to turn it off at the schema level - would that work?
yup that would work!
I can add the ability to turn it off at the schema level
:+1:
Done. Specify { versionKey: false }
in toObject()
options to disable. Can also write a plugin to do that in a one-liner.
@vkarpov15 setting { versionKey: false }
will disable versioning all together. What I want to do is just not expose __v
in my API while keeping the versioning functionality. Also on another -very minor- note, this could be used by an attacker to sniff out if a system is using mongoose. Not that I really care but there is even a security argument for being able to exclude __v
.
@poisa it's meant to be set in the toObject
options, not the schema options so versioning is still intact.
#!/usr/bin/env node
'use strict';
const assert = require('assert');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;
const schema = new Schema({
name: String
}, { toObject: { versionKey: false } });
const Test = mongoose.model('test', schema);
const test = new Test({ name: 'TEST' });
async function run() {
await conn.dropDatabase();
let doc = await test.save();
assert.strictEqual(doc.__v, 0);
assert.strictEqual(doc.toObject().__v, undefined);
console.log(`version: ${mongoose.version}: All tests pass!`);
return conn.close();
}
run();
issues: ./2675.js
version: 5.2.3: All tests pass!
issues:
@lineus That's great thanks! I didn't catch that from the docs the first time.
(By the way, as far as I'm concerned this can be closed)
:+1: I missed this option too, check out versionKey
option in toObject()
options
Most helpful comment
@poisa it's meant to be set in the
toObject
options, not the schema options so versioning is still intact.2675.js
Output: