Skip to content

Global 'strictQuery' is ignored when schema/models are defined before mongoose setup #11362

Answered by Maximusya
Maximusya asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks, @vkarpov15!

The following code structure seem to put things in the correct order and all the desired global mongoose options are honored by the model:

$ node index.js
Mongoose: users.findOne({ wrongField: 'bar' }, { projection: {} })

index.js:

import { db } from './db.js';
import User from './User.js';

run().catch(console.error);

async function run() {
  try {
    await db.connect();
    const user = await User.findOne({ wrongField: 'bar' })
  } finally {
    await db.disconnect();
  }
}

db.js:

import mongoose from 'mongoose';

mongoose.set('debug', true);
mongoose.set('strictQuery', false);
mongoose.set('autoCreate', false);
mongoose.set('autoIndex', false);

export const db = {

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@Maximusya
Comment options

@vkarpov15
Comment options

@Maximusya
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by Maximusya
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants