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

Setting global strictQuery doesn't work after schemas are created #12703

Closed
2 tasks done
gavbaa opened this issue Nov 18, 2022 · 1 comment · Fixed by #12717
Closed
2 tasks done

Setting global strictQuery doesn't work after schemas are created #12703

gavbaa opened this issue Nov 18, 2022 · 1 comment · Fixed by #12717
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@gavbaa
Copy link

gavbaa commented Nov 18, 2022

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.7.2

Node.js version

16.x

MongoDB server version

6.x

Typescript version (if applicable)

4.8.0

Description

mongoose.set('strictQuery', false); only correctly disables strict query behavior if you run it before the schemas are created. After the schemas are created, changes to this property is ignored. Similarly, if you change it in the middle of schema creation, it affects some schemas but not others. The schemas should respect the current state of this global if it's set, not what it was at schema creation time.

Steps to Reproduce

'use strict';

const assert = require('assert');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/gh7008', { useNewUrlParser: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;

const schema = new Schema({
  title: String,
});

const Test = mongoose.model('test', schema);

mongoose.set('strictQuery', false);

const test0 = new Test({
  title: 'chimichanga'
});
const test1 = new Test({
  title: 'burrito bowl'
});
const test2 = new Test({
  title: 'taco supreme'
});

async function run() {
  assert.strictEqual(mongoose.version, '6.7.2');
  await conn.dropDatabase();
  await test0.save();
  await test1.save();
  await test2.save();

  let cond = {
    $or: [
      {
        title: {
          $regex: 'urri',
          $options: 'i'
        }
      },
      {
        name: {
          $regex: 'urri',
          $options: 'i'
        }
      }
    ]
  }
  let found = await Test.find(cond);
  assert.strictEqual(found.length, 1);
  assert.strictEqual(found[0].title, 'burrito bowl');

  console.log('All Assertions Pass.');
  return conn.close();
}

run();

Expected Behavior

The current state of mongoose.set('strictQuery', false); should be used if no per-query/schema overrides are specified.

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

'use strict';

const assert = require('assert');
const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const schema = new Schema({
  title: String,
});

const Test = mongoose.model('test', schema);

mongoose.set('strictQuery', false);

const test0 = new Test({
  title: 'chimichanga'
});
const test1 = new Test({
  title: 'burrito bowl'
});
const test2 = new Test({
  title: 'taco supreme'
});

async function run() {
  await mongoose.connect('mongodb://localhost:27017/gh7008', { useNewUrlParser: true });
  await mongoose.connection.dropDatabase()
  const conn = mongoose.connection;
  assert.strictEqual(mongoose.version, '6.7.2');
  await conn.dropDatabase();
  await test0.save();
  await test1.save();
  await test2.save();

  let cond = {
    $or: [
      {
        title: {
          $regex: 'urri',
          $options: 'i'
        }
      },
      {
        name: {
          $regex: 'urri',
          $options: 'i'
        }
      }
    ]
  }
  let found = await Test.find(cond);
  assert.strictEqual(found.length, 1);
  assert.strictEqual(found[0].title, 'burrito bowl');

  console.log('All Assertions Pass.');
  return conn.close();
}

run();

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.

3 participants