Comments (4)
For setting translateAliases
application-wide, you should be able to just do mongoose.set('translateAliases', true)
. The following script shows that's enough to turn on translateAliases
for all queries:
'use strict';
const mongoose = require('mongoose');
mongoose.set('translateAliases', true);
run().catch(error => {
console.error(error);
process.exit(-1);
});
async function run() {
await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
const schema = new mongoose.Schema({
name: { type: String, alias: 'fullName' }
});
const Test = mongoose.model('Test', schema);
await Test.deleteMany({});
await Test.create({ name: 'foo' });
console.log(await Test.findOne({ fullName: 'foo' })); // Finds the doc
process.exit(0);
}
For setting defaults on lean queries, try the mongoose-lean-defaults plugin, that's our officially supported plugin for applying defaults on lean documents. Another approach you can try is Model.applyDefaults()
in a post hook, Model.applyDefaults()
is a one-liner for applying a model's defaults to a given POJO.
from mongoose.
Thank you so much for the response. I will use mongoose.set('translateAliases', true);
as suggested to apply that globally.
However, I still have some questions about how to apply the appropriate lean options to each and every lean query. I am already using the defaults plugin to set default values for properties when a lean query is involved. I think I am asking something different.
In this case, I am asking if there is a good way to set options for each and every lean query; for example:
MY OPTIONS
const LEAN_OPTS = Object.freeze({
autopopulate: true,
defaults: true,
getters: true,
virtuals: true,
versionKey: false,
});
IN MY pre
hook
this._mongooseOptions = {
...this.mongooseOptions(),
...(this.mongooseOptions()?.lean && {
lean: {
...LEAN_OPTS,
...this.mongooseOptions().lean,
},
}),
};
Hope that makes more sense. I probably didn't describe it well. My apologies.
from mongoose.
I'd recommend doing something more like the following:
if (this.mongooseOptions()?.lean) {
this.setOptions({
lean: { ...LEAN_OPTS, this.mongooseOptions().lean }
});
}
But otherwise you're right, currently the only good way to set default lean
options is via plugin, and your approach works
from mongoose.
Thank you so much. I will give that a shot and see how that goes. Hopefully, that will cascade to the populate
queries too. 👍
from mongoose.
Related Issues (20)
- Nested populate with nested paths and subdocuments is not working fine HOT 6
- Issue with 2dsphere indexes in nested schemas vs subdocuments HOT 2
- (Typescript) toObject on populated documents loses the structure
- Recompiling schema does not reapply discriminators HOT 1
- Using default value on sub documents does not work as expected HOT 3
- Recursion check in toObject breaks mongoose-intl plugin
- Cannot define discriminator options on a Schema HOT 1
- Model.createSearchIndex() create SIGTERM on DuplicateIndex
- Populate doesn't work - nested path + wildcard + 2 references on a same model
- How to catch MongoNetworkError: connect ECONNREFUSED in Node when using mongoose.createConnection() HOT 2
- Pre and Post (middleware) hooks issue
- getters array in virtualtype for "id" virtual is not deduped HOT 1
- FilterQuery Typing fails for generic type HOT 5
- Change tracking is getting lost on transaction retry HOT 1
- Types break in semver patch change HOT 4
- "$inc" operator not working HOT 1
- Cannot find module 'mongoose' or its corresponding type declarations. HOT 4
- Type of subdocument not inferred in toJSON() HOT 2
- Differentiating Type when using `lean()` vs no `lean()` HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose.