-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Examples for using aliases in mongoose find #6427
Comments
Querying by aliases is an open issue at the moment, #5321. Here is an example of how to use the translateAliases method to do this now: ###6427.js #!/usr/bin/env node
'use strict';
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
const conn = mongoose.connection;
const Schema = mongoose.Schema;
const schema = new Schema({
n: {
type: String,
alias: 'name'
}
});
const Test = mongoose.model('test', schema);
const test = new Test({ name: 'Billy' });
async function run() {
await conn.dropDatabase();
await test.save();
let doesntwork = await Test.findOne({ name: 'Billy' });
console.log(doesntwork);
let works = await Test.findOne(Test.translateAliases({ name: 'Billy' }));
console.log(works);
return conn.close();
}
run(); Output:
|
@vkarpov15 can we re-open this? 'use strict';
const mongoose = require('mongoose');
const { Schema } = mongoose;
const assert = require('assert');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true });
const userSchema = new Schema({
name: { type: String, alias: 'nameAlias' }
});
const User = mongoose.model('User', userSchema);
async function run() {
await User.deleteMany();
await User.create({ nameAlias: 'Hafez' });
const user = await User.findOne({ nameAlias: 'Hafez' });
assert.ok(user); // fails
}
run().catch(console.error); |
@AbdelrahmanHafez this is closed because of #5338 and |
Sweet, let's follow up on #8678 then. |
@vkarpov15 this is an open question, could |
@loris it could be used for migrations. You might have the legacy field as an alias for the new field, for example. |
We are using nodejs 9.8 , mongo 3.6 , mongoose 5.0.7
I am looking for some examples of aliasing fields in find query.
Any help appreciated. I cant seem to find any examples. But also read that mongoose 4.0.0 onward supports this feature
The text was updated successfully, but these errors were encountered: