Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7862055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:58:50+00:00 2026-06-02T22:58:50+00:00

Given the following schema: var UserSchema = new Schema({ , email : { type:

  • 0

Given the following schema:

var UserSchema = new Schema({
   , email   :  { type: String }
   , passwordHash   :  { type: String }
   , roles  :  { type: [String] }
});

I’d like email to be the key.
How can I define this?

I could do:

var UserSchema = new Schema({
       , _id:  { type: String }
       , passwordHash   :  { type: String }
       , roles  :  { type: [String] }
    });

so MongoDB would recognize it as the id-field, and adapt my code to refer to _id instead of email but that doesn’t feel clean to me.

Anyone?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:58:52+00:00Added an answer on June 2, 2026 at 10:58 pm

    Since you’re using Mongoose, one option is to use the email string as the _id field and then add a virtual field named email that returns the _id to clean up the code that uses the email.

    var userSchema = new Schema({
        _id: {type: String},
        passwordHash: {type: String},
        roles: {type: [String]}
    });
    
    userSchema.virtual('email').get(function() {
        return this._id;
    });
    
    var User = mongoose.model('User', userSchema);
    
    User.findOne(function(err, doc) {
        console.log(doc.email);
    });
    

    Note that a virtual field is not included by default when converting a Mongoose doc to a plain JS object or JSON string. To include it you have to set the virtuals: true option in the toObject() or toJSON() call:

    var obj = doc.toObject({ virtuals: true });
    var json = doc.toJSON({ virtuals: true });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given following array: var arr = [undefined, undefined, 2, 5, undefined, undefined]; I'd like
Given the following schema: CREATE TABLE players ( id BIGINT PRIMARY KEY, name TEXT
Appreciate some insight given the following code: <script type=text/javascript> $(document).ready(function() { var soapEnv =
Given the following schema / data / output how would I format a SQL
Given the following data/schema: DECLARE @t1 TABLE ( Id int NOT NULL ) DECLARE
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
Given the following code, is there a way I can call class A's version
I have a table with the following schema: +------------------+ |Field | Type | +------------------+
Given the following (heavily simplified) tables: create table Tags ( TagId int Primary Key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.