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

Examples for using aliases in mongoose find #6427

Closed
vjoshihumancare opened this issue May 6, 2018 · 6 comments
Closed

Examples for using aliases in mongoose find #6427

vjoshihumancare opened this issue May 6, 2018 · 6 comments

Comments

@vjoshihumancare
Copy link

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

@lineus
Copy link
Collaborator

lineus commented May 8, 2018

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:

issues: ./6427.js
null
{ _id: 5af1a034c11683827ef1fa45, n: 'Billy', __v: 0 }
issues:

@lineus lineus closed this as completed May 8, 2018
@AbdelrahmanHafez
Copy link
Collaborator

@vkarpov15 can we re-open this?
We still can not find/update with an alias. The following test fails:

'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);

@vkarpov15
Copy link
Collaborator

@AbdelrahmanHafez this is closed because of #5338 and translateAliases(). We can open up a separate issue for running translateAliases() automatically on queries and updates.

@AbdelrahmanHafez
Copy link
Collaborator

Sweet, let's follow up on #8678 then.

@loris
Copy link
Contributor

loris commented Mar 25, 2020

@vkarpov15 this is an open question, could aliases be used for migration purpose? Let's take a simple use-case: it happens quite often that we need to rename a schema field.
Right now, this is a non trivial task: we need to add a new field with the new name to the schema, update all the setters query to ensure they write the value both to the old and new fields, deploy code, backfill the new fields in database by copying the value from the old fields, update all the getters queries to read the new field instead of the old field, deploy code, remove the old field from the setters queries, deploy, remove old fields from database.
By setting a field alias, mongoose could allow the code to read both from old or new fields and write to the old or the new fields, and keep both fields synchronised

@vkarpov15
Copy link
Collaborator

@loris it could be used for migrations. You might have the legacy field as an alias for the new field, for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants