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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:38:45+00:00 2026-05-31T19:38:45+00:00

The schema for objects I am putting in a collection goes something like this:

  • 0

The “schema” for objects I am putting in a collection goes something like this:

{
    name : "Niranjan", //<--key
    children : ["Suraj", "kalidas", "Suraj"]
}

But I wanted to ensure that children would be treated as a set – in the example above I wanted Mongo to not accept the record because “Suraj” appear twice in the array.

I tried creating a unique index over name and children

db.folks.ensureIndex({name:1,children:1},{unique:true})

What this does, however, is that it makes a globally unique index over any child name + folk name. That is if I DO NOT also have a unique index on name I will be able todo the following:

db.folks.insert({name:'p',children:['c1','c2']})
db.folks.insert({name:'p',children:['c3']})

because p+c1, p+c2 and p+c3 are distinct. If I tried to insert the following:

db.folks.insert({name:'p',children:['c4','c1']})

then I will get

E11000 duplicate key error index: test.folks.$name_1_children_1  dup key: { : "p", : "c1" }

Is there a way to achieve this without doing a
check during insertion as in https://stackoverflow.com/questions/9640233/unique-array-values-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-05-31T19:38:47+00:00Added an answer on May 31, 2026 at 7:38 pm

    Unique indexes will act the way you expect in a future release of MongoDB:

    https://jira.mongodb.org/browse/SERVER-1068

    For now, you’ll need to check for uniqueness in your application before inserting.

    Incidentally, when you’re updating documents, you can use $addToSet instead of $push, to enforce uniqueness:

    http://www.mongodb.org/display/DOCS/Updating#Updating-%24addToSetand%24each

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

Sidebar

Related Questions

I have a collection of Database objects, each containing collections of Schema objects and
I've got this schema: Silverlight4 project 1a. silverlight class library project with some objects
Hy guys I've got the following schema for my objects: Product: columns: name: {
I would like to migrate me previously serialized objects in database to new schema.
I have a situation like this. I have schema which has high level user
I'm generating business objects from this schema using Enterprise Architect. The schema has the
I'm trying to verify if a schema matches the objects I'm initializing. Is there
I need to create an XML schema definition (XSD) that describes Java objects. I
I have a database where I store objects. I have the following (simplified) schema
By default, objects (tables, stored procedures, etc) are set up with the dbo owner/schema

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.