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
removing keys not defined in schema via bulkWrite #8778
Comments
Welcome @Monokai You need to set // 8778
const mongoose = require('mongoose');
const { Schema } = mongoose;
const assert = require('assert');
run().catch(console.error);
async function run () {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true
});
await mongoose.connection.dropDatabase();
const userSchema = new Schema({
name: { type: String }
}, { strict: false });
const User = mongoose.model('User', userSchema);
await User.create({ name: 'Hafez' });
await User.bulkWrite([{
updateOne: {
filter: { name: 'Hafez' },
update: { $set: { notInSchema: true } }
}
}]);
// using collection `findOne` to get the document directly from the database
// without any work from mongoose side
const user = await User.collection.findOne({ name: 'Hafez' });
assert.equal(user.notInSchema, true);
console.log('Done.');
} |
@AbdelrahmanHafez Thank you for the suggestion. But what if I want to keep my schema strict, but not in a particular update? I understand you can override the strict setting in the update calls right? |
Yes, you can do that on Using the native bulkWrite would bypass any mongoose trimming to fields for that specific operation, and you get to keep your schema strict: // 8778
const mongoose = require('mongoose');
const { Schema } = mongoose;
const assert = require('assert');
run().catch(console.error);
async function run () {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true
});
await mongoose.connection.dropDatabase();
const userSchema = new Schema({
name: { type: String }
});
const User = mongoose.model('User', userSchema);
await User.create({ name: 'Hafez' });
// using native bulkWrite to bypass any work done by mongoose
await User.collection.bulkWrite([{
updateOne: {
filter: { name: 'Hafez' },
update: { $set: { notInSchema: true } }
}
}]);
// using collection `findOne` to get the document directly from the database
// without any work from mongoose side
const user = await User.collection.findOne({ name: 'Hafez' });
assert.equal(user.notInSchema, true);
console.log('Done.');
} Does that help? |
@AbdelrahmanHafez Thank you. Yes, using the native query it worked flawlessly! |
In |
@AbdelrahmanHafez I have mongoose 5.13.7, but the type definition for
So I am unable to pass |
I'm using Node 12, MongoDB 3.5.5 and Mongoose 5.9.7.
I'm looking for a way to delete old keys from my documents. As I understand, I need to set
strict
tofalse
to bypass schema validation, because these keys don't exist in the schema definition anymore. I'm usingbulkWrite(updates)
, and myupdates
array looks like this:The bulk write result looks something like this:
The old keys are not deleted though, doesn't matter if I use
strict
or not. I've also tried settingbypassDocumentValidation
totrue
on thebulkWrite()
call.How can I delete old keys that don't exist in the schema definition anymore via
bulkWrite
?The text was updated successfully, but these errors were encountered: