i want to reference mongoDB fields in queries and aggregates instead of using fields as hardcoded strings. For example: This what i'm doing now:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
const CatSchema = new mongoose.Schema({
name: String
});
const Cat = mongoose.model('Cat', CatSchema);
Cat.findOne({name: 'beauty'}).exec() //using name field hardcoded in code
//OR
Cat.aggregate([
{
$match: {
name: 'beauty' //using name field hardcoded in code
}
}
]).exec()
What i'm trying to do is something like this:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
const CatSchema = new mongoose.Schema({
name: String
});
const Cat = mongoose.model('Cat', CatSchema);
Cat.findOne({CatSchema.name: 'beauty'}).exec() //use a reference as field name
//OR
Cat.aggregate([
{
$match: {
CatSchema.name: 'beauty' //use a reference as field name
}
}
]).exec()
Why this? to prevent issues on code on field name changes. And to only do a (right-click + refactor) on the schema field name.