Skip to content
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

Not working nested defaults - findByIdAndUpdate #11668

Closed
relloccate opened this issue Apr 15, 2022 · 6 comments
Closed

Not working nested defaults - findByIdAndUpdate #11668

relloccate opened this issue Apr 15, 2022 · 6 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@relloccate
Copy link

relloccate commented Apr 15, 2022

Hi there.
When you trying to set a document props by the findByIdAndUpdate method, the default value will not be set into DB if nested (but JSON will be returned).
The latest mongoose ^6.3.0
Needs a patch? or dupe issue? Thanks.

const setToDb = async ({ id }) => {
    return await Collection.findByIdAndUpdate(
        id,
        {
            $set: {
                'time.hit': Date.now()
                // UPD: IF LEAVE $set EMPTY, BOTH SETS
            }
        },
        { new: true, setDefaultsOnInsert: true, upsert: true }
    );
};

const result = await setToDb({ id: 'someid' }); // the doc returns with "resolved" field, but no "resolved" field in database
console.log(result); // { time: { hit: 1650007109236, resolved: 1650007109000 } }

Schemes

It Not Sets

const scheme = mongoose.Schema({
    time: {
        hit: { type: Number, default: 0 },
        resolved: { type: Number, default: Date.now }
    }
});

It Sets (for example)

const scheme = mongoose.Schema({
    resolved: { type: Number, default: Date.now }
});
@Uzlopak
Copy link
Collaborator

Uzlopak commented Apr 15, 2022

Solved the issue?

@relloccate
Copy link
Author

nah

@relloccate relloccate reopened this Apr 15, 2022
@relloccate
Copy link
Author

When you set the first document (is not exist) the defaults not inserts if you putting a data to the same object where is field with default located.

@vkarpov15 vkarpov15 added this to the 6.3.4 milestone Apr 15, 2022
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Apr 15, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.3.4, 6.3.2 Apr 15, 2022
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Apr 18, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const {Schema} = mongoose;

const badSchema = new Schema({
    time: {
        hit: {type: Number, default: 0},
        resolved: { type: Number, default: Date.now}
    }
});

const goodSchema = new Schema({
    resolved: {type: Number, default: Date.now}
});

const Bad = mongoose.model('Bad', badSchema);

const Good = mongoose.model('Good', goodSchema);

async function run() {
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();
    const id = mongoose.Types.ObjectId().toString();
    console.log(id);
    const bad = await Bad.findByIdAndUpdate({_id: id}, {$set: {'time.hit': Date.now()}}, { new: true, setDefaultsOnInsert: true, upsert: true });
    const good = await Good.findByIdAndUpdate({_id: id}, {$set: {'time.hit': Date.now()}}, { new: true, setDefaultsOnInsert: true, upsert: true });

    console.log('bad', bad);
    console.log('good', good);

    console.log('check bad DB', await Bad.findOne());
    console.log('check good DB', await Good.findOne());
}

run();

Data in database using compass:

Bad:

{  "_id": {    "$oid": "625d808a58b117762e33357d"  },  "__v": 0,  "time": {    "hit": 1650294922665  }}

Good:

{  "_id": {    "$oid": "625d808a58b117762e33357d"  },  "__v": 0,  "resolved": 1650294922732}

@donaldshen
Copy link

donaldshen commented Mar 1, 2024

When you set the first document (is not exist) the defaults not inserts if you putting a data to the same object where is field with default located.

I faced the same issue here. It's quite useless for a nested default value to work if you can't set other fields at the same nested level of it during the first upserting.

@vkarpov15
Copy link
Collaborator

@donaldshen this issue is fixed, were you on an older version of Mongoose that didn't have this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

5 participants