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

Document.prototype.$set doesn't work per the documenation when passing path as an object of key/value pairs #12157

Closed
2 tasks done
randy5235 opened this issue Jul 27, 2022 · 4 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.

Comments

@randy5235
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.3.4

Node.js version

16.x.x

MongoDB server version

4.2

Description

The document.set documentation allows for passing either (path: string, value: string, type: string, options: object)
or
(path: object, type: string, option: object)
or
(path: object, options: object)

with type always optional.

It currently ignores passing path as an object.

Steps to Reproduce

const mongoose = require('mongoose');
const connection = mongoose.connect('connectionString');

const testSchema = new mongoose.Schema({
  email: String,
  name: String,
});

const Test = mongoose.model('Test', testSchema);

// insert a couple of records doesn't really matter what

const myUser = Test.find({_id: <id from generated record>});
// This doesn't work as expect
myUser.set( { path1 : 'Value', path2: 'Value'}, {strict: false});
// setting them like this does
myUser.set('path1', 'Value', {strict: false});
myUser.set('path2', 'Value', {strict: false});



### Expected Behavior

Expected behavior:

Expectation is that the documented behavior is honored.

randy5235 added a commit to randy5235/mongoose that referenced this issue Jul 27, 2022
@randy5235
Copy link
Author

I opened this PR to resolve this behavior: #12158

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Aug 8, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');
mongoose.set('debug', true);
const testSchema = new mongoose.Schema({
  email: String,
  name: String,
});

const Test = mongoose.model('Test', testSchema);


async function run() {
    // insert a couple of records doesn't really matter what
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();

    const user = await Test.create({ name: 'Test Testerson', email: 'test@localhost.com'})
    const myUser = Test.find({ _id: user._id });
    // This doesn't work as expect
    myUser.set( { path1 : 'Value', path2: 'Value'}, {strict: false});
    // setting them like this does
    myUser.set('path1', 'Value', {strict: false});
    await myUser.set('path2', 'Value', {strict: false});
    console.log(await Test.findOne());
}

run();

@IslandRhythms IslandRhythms linked a pull request Aug 8, 2022 that will close this issue
@IslandRhythms
Copy link
Collaborator

Could also be a duplicate of #12155

@randy5235
Copy link
Author

I'm closing this. After rereading the code that is in place. I suspect that my interpretation of the signature:
.set({key: value}, {options.values}) is incorrect. I see internally this gets called with .set({key:value}, undefined, {options.values} I may submit an update for the documentation to make this more clear, assuming that my current understanding is correct.

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

Successfully merging a pull request may close this issue.

2 participants