Git Product home page Git Product logo

Comments (3)

emiljanitzek avatar emiljanitzek commented on May 24, 2024

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.

simonnilsson avatar simonnilsson commented on May 24, 2024

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.

simonnilsson avatar simonnilsson commented on May 24, 2024

Downgrading from 8.2.2 to 8.2.1 seems to solve this issue.

from mongoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.