Comments (4)
https://mongoosejs.com/docs/guide.html#versionKey
According to the documentation, you shouldn't be putting versionKey inside toObject. You should put it in the Schema definition like
const schema = new mongoose.Schema(
{ name: String },
// Could be `optimisticConcurrency` as well
{ versionKey: false } //defaults to __v
);
from mongoose.
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema(
{ name: String },
// Could be `optimisticConcurrency` as well
{ versionKey: "__v", toObject: { versionKey: false } }
);
const Test = mongoose.model("Test", testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.insertMany([{ name: 'x' }]);
const doc = await Test.findOne();
console.log(doc.__v);
console.log('done');
}
run();
from mongoose.
@juona Does this solve your problem for now?
#!/usr/bin/env node
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema(
{ name: String },
// Could be `optimisticConcurrency` as well
{ versionKey: '__v' }
);
// testSchema.set('__v', false) //sets __v to 0 commented out or not
const Test = mongoose.model("Test", testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.insertMany([{ name: 'x' }]);
const doc = await Test.findOne();
console.log(doc.__v); // 0
console.log('done');
process.exit(0)
}
run();
from mongoose.
@donoftime2018 Thanks for the help. This was not a burning issue, I just used Model.prototype.create()
instead. I will give your method a try in case there is a real need to use insertMany
(still on v7, you see, so this fix probably does not apply).
@vkarpov15 Thanks, swift as always!
from mongoose.
Related Issues (20)
- Mismatch between the type derived from the ```lean``` query and the declared interface on virtual fields HOT 3
- Documentation site down? HOT 1
- Type 'import(".../node_modules/mongoose/node_modules/mongodb/mongodb").MongoClient' is not assignable to type 'import(".../node_modules/mongodb/mongodb").MongoClient'. HOT 7
- BSON Timestamp in a mongoose schema HOT 1
- The return type of connection.tranaction method should not be Promise<void>.
- [Feature] Add option to disable filters on each method HOT 4
- InferRawDocType returns `unknown` for every property instead of the right type HOT 3
- TS: _id not required on DocumentArray properties of documents returned from query HOT 6
- Embedded documents are missing the `toJSON()` method HOT 1
- Can not creating a Schema with a field named 'parent' HOT 8
- Query recursive model with discriminator not working with string ref id filter HOT 1
- await mongoose.connect() not fully connected HOT 5
- how to search objectid with regex? HOT 3
- địt con bà chúng mày
- Example for findOneAndUpdate is confusing HOT 3
- Unable to use `$pull` on nested array within discriminated union
- Document.prototype.validate() does not validate subdocument required fields with validateModifiedOnly option HOT 1
- BulkWrite updateOne with filter $elemMatch { $elemMatch: { $in: [] } } throwing TypeError HOT 1
- Model.hydrate() should work with subdocument array projection
- Private PKG: MongooseError: Operation `users.findOne()` buffering timed out after 10000ms HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose.