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

mongoose.set('translateAliases', true) Not working in mongoose@8.3.1 #14521

Closed
2 tasks done
wanxger opened this issue Apr 14, 2024 · 1 comment · Fixed by #14562
Closed
2 tasks done

mongoose.set('translateAliases', true) Not working in mongoose@8.3.1 #14521

wanxger opened this issue Apr 14, 2024 · 1 comment · Fixed by #14562
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@wanxger
Copy link

wanxger commented Apr 14, 2024

Prerequisites

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

Mongoose version

8.3.1

Node.js version

v20.11.0

MongoDB server version

6.0.2

Typescript version (if applicable)

5.3.3

Description

/workspaces/example/node_modules/.pnpm/mongoose@8.3.1/node_modules/mongoose/lib/cast.js:290
          throw new StrictModeError(path, 'Path "' + path + '" is not in ' +
                ^
StrictModeError: Path "boo" is not in schema, strict mode is `true`, and upsert is `true`.
    at cast (/workspaces/example/node_modules/.pnpm/mongoose@8.3.1/node_modules/mongoose/lib/cast.js:290:17)
    at model.Query.Query.cast (/workspaces/example/node_modules/.pnpm/mongoose@8.3.1/node_modules/mongoose/lib/query.js:4786:12)
    at model.Query.Query._castConditions (/workspaces/example/node_modules/.pnpm/mongoose@8.3.1/node_modules/mongoose/lib/query.js:2199:10)
    at model.Query._updateThunk (/workspaces/example/node_modules/.pnpm/mongoose@8.3.1/node_modules/mongoose/lib/query.js:3752:8)
    at model.Query._updateOne (/workspaces/example/node_modules/.pnpm/mongoose@8.3.1/node_modules/mongoose/lib/query.js:3857:23)
    at model.Query.exec (/workspaces/example/node_modules/.pnpm/mongoose@8.3.1/node_modules/mongoose/lib/query.js:4330:80)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async main (/workspaces/example/main.ts:45:27) {
  isImmutableError: false,
  path: 'boo'
}

Steps to Reproduce

import mongoose from 'mongoose';

mongoose.set('translateAliases', true);

const main = async () => {
  const uri = 'mongodb://root:example@mongodb';

  const connection = mongoose.createConnection(uri, {
    dbName: 'testdb',
  });

  connection.set('translateAliases', true);

  console.debug('connection', connection);

  const boo = 'boo';

  const foo = 'foo';

  const mongooseSchema = new mongoose.Schema({
    B: {
      alias: 'boo',
      required: true,
      unique: false,
      type: String,
    },
    F: {
      alias: 'foo',
      required: true,
      unique: true,
      type: String,
    },
  });

  const ExampleModel = connection.model('Example', mongooseSchema, 'example', {
    overwriteModels: true,
  });

  const findResult = await ExampleModel.find({
    boo,
  });

  console.debug('findResult', findResult);

  const updateOneResult = await ExampleModel.updateOne(
    { boo },
    { foo },
    {
      upsert: true,
    },
  );

  console.debug('updateOneResult', updateOneResult);
};

main();

Expected Behavior

No response

@IslandRhythms IslandRhythms added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Apr 15, 2024
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

mongoose.set('translateAliases', true);

const main = async () => {
  const uri = 'mongodb://localhost:27017';

  const connection = mongoose.createConnection(uri, {
    dbName: 'testdb',
  });

  connection.set('translateAliases', true);

  console.debug('connection', connection);

  const boo = 'boo';

  const foo = 'foo';

  const mongooseSchema = new mongoose.Schema({
    B: {
      alias: 'boo',
      required: true,
      unique: false,
      type: String,
    },
    F: {
      alias: 'foo',
      required: true,
      unique: true,
      type: String,
    },
  });

  const ExampleModel = connection.model('Example', mongooseSchema, 'example', {
    overwriteModels: true,
  });

  const findResult = await ExampleModel.find({
    boo,
  });

  console.debug('findResult', findResult);

  const updateOneResult = await ExampleModel.updateOne(
    { boo },
    { foo },
    {
      upsert: true,
    },
  );

  console.debug('updateOneResult', updateOneResult);
};

main();

@vkarpov15 vkarpov15 added this to the 8.3.4 milestone Apr 26, 2024
vkarpov15 added a commit that referenced this issue May 4, 2024
vkarpov15 added a commit that referenced this issue May 6, 2024
fix(query): apply translateAliases before casting to avoid strictMode error when using aliases
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
3 participants