4

Let's suppose I have a schema like this

var Language = new Schema({
    name: { type: String, unique: true, required: true },
    pos: [{
        name: String,
        attributes: [{
            name: String
        }]
    }]
});

Will each item in pos, and in attributes, have an _id? If I add a unique index to the name field in the pos array, will uniqueness be enforced to just that array, or will it be unique for all entries?

Peter Olson
  • 139,199
  • 49
  • 202
  • 242

2 Answers2

3

No, embedded documents like pos and attributes that don't have their own schema do not have an _id property.

If you add a unique index to the name field in the pos array, uniqueness will be enforced across the collection, but not within the array of a single document. See this post.

Community
  • 1
  • 1
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
1

In Mongoose 4, pos and attributes will get their own schema. You can prevent them from getting _id attributes like so:

// ...
    attributes: [{
        name: String,
        _id: false
    }]
// ...
Adam Lockhart
  • 1,165
  • 1
  • 10
  • 13