Comments (3)
Found and even bigger problem with this behaviour where the sub document gets updated in the database by just updating any other property on the document. Look at the example below. By just changing the name, the sub document gets changed from the default empty object to null
.
const mongoose = require('mongoose');
const SubSchema = new mongoose.Schema({
name: { type: String }
}, { _id: false });
const MainSchema = new mongoose.Schema({
name: String,
sub: {
type: SubSchema,
default: {}
}
});
const MainModel = mongoose.model('Main', MainSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017/mongoose_test');
console.log(mongoose.version);
const doc = new MainModel({ name: 'foo' });
console.log('init', doc.sub); // {} OK!
await doc.save();
const savedDocFirst = await MainModel.findById(doc.id).orFail();
console.log('findById', savedDocFirst.sub); // {} OK!
savedDocFirst.name = 'bar';
await savedDocFirst.save();
const savedDocSecond = await MainModel.findById(doc.id).orFail();
console.log('findById', savedDocSecond.sub); // null WRONG!
}
run().then(() => mongoose.connection.close()).catch((error) => console.error(error));
from mongoose.
I'm seeing the same issue, a field of type Mixed
and default {}
gets set to null
on save() when updating other fields. This feels like a regression introduced in recent update.
from mongoose.
Downgrading from 8.2.2 to 8.2.1 seems to solve this issue.
from mongoose.
Related Issues (20)
- Add a nullable field, its not same as "required" HOT 1
- Turn off "don't cast update pipelines by default" in favor of a more secure approach HOT 2
- Add TypeScript interface for the new PipelineStage - Vector Search HOT 1
- Add `pre` and `post` function types on Query class
- Nested populate with nested paths and subdocuments is not working fine HOT 6
- Best method to set Query and Lean defaults HOT 4
- Issue with 2dsphere indexes in nested schemas vs subdocuments HOT 2
- (Typescript) toObject on populated documents loses the structure
- Recompiling schema does not reapply discriminators HOT 1
- Recursion check in toObject breaks mongoose-intl plugin
- Cannot define discriminator options on a Schema HOT 1
- Model.createSearchIndex() create SIGTERM on DuplicateIndex
- Populate doesn't work - nested path + wildcard + 2 references on a same model
- How to catch MongoNetworkError: connect ECONNREFUSED in Node when using mongoose.createConnection() HOT 2
- Pre and Post (middleware) hooks issue
- getters array in virtualtype for "id" virtual is not deduped HOT 1
- FilterQuery Typing fails for generic type HOT 4
- Change tracking is getting lost on transaction retry HOT 1
- Types break in semver patch change HOT 4
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.