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 8995195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:35:15+00:00 2026-06-15T23:35:15+00:00

I have the following schema in mongoose: UserSchema = new Schema username: {type: String,

  • 0

I have the following schema in mongoose:

UserSchema = new Schema
    username: {type: String, required: true}

GameSchema = new Schema
    identifier: String
    users: [UserSchema]

I want to ensure that every user in a game has a unique username. However, if I add

unique: true

to the username definition, then it seems to enforce uniqueness across all games, not just within the game that the user resides. Also, if I have more than 1 game with no users, then I get the following error:

games.$users.username_1  dup key: { : null }

I tried adding a custom validator to the username field to manually check if that username is already taken within the scope of the parent game, but in mongoose the validator function only receives the actual string of the username, so I don’t have any way of checking that the username is unique within the game because I can’t get a reference to the parent game document in the validator function.

Is there any way to accomplish this sort of validation in mongoose?

  • 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-15T23:35:16+00:00Added an answer on June 15, 2026 at 11:35 pm

    As you found out, adding a unique index to a field of an array doesn’t enforce uniqueness within the array, it ensures that no two documents in the collection contain the same field value in the array.

    Instead, look at the $addToSet array operator as a way of atomically adding a value to an array only if it’s not already in the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to following code: var UserSchema = mongoose.Schema({ email: { type: String, unique:
I have the following Schemas in Mongoose/Node.js: var ItemsSchema = new Schema({ itemname: String,
In mongoose I've got this model: var userschema = new mongoose.Schema({ user: String, following:
I have the following schema User: columns: id: type: integer primary: true name: string
I have the following code for mongoose schemas var EstacionSchema = new Schema({ nombre
I have the following schema definition: Usuario: columns: empresa_id: { type: BIGINT, notnull: true
Take a look at the following schema: var userSchema = new mongoose.Schema({ fullName: {
I have the following Mongoose schema that embeds another: var EmbedSchema = new Schema({
Let's say I have the following schema var userSchema = new Schema({ name :
I have the following: Schema create_table mouldings, :force => true do |t| t.string suppliers_code

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.