thatmarvin
thatmarvin

Reputation: 2880

Are embedded document ids unique in the collection?

I have the following Mongoose schema that embeds another:

var EmbedSchema = new Schema({
  foo: String
});

var ParentSchema = new Schema({
  foo: String
  embeds: [EmbedSchema]
});

After instantiating and embedding a couple objects, I'll end up with something like this:

{
  "_id": "4f505a866e65f3896b00002c",
  "foo": "some value",
  "embeds: [
    {
      "_id": "4f505aa36e65f3896b000034",
      "foo": "some value 1"
    }, {
      "_id": "4f2eeb8f559757bf4f000001",
      "foo": "some value 2"
    }
  ]
}

Are the _ids within the embeds unique across the collection? I was thinking of querying just using embeds._id to get the Parent if it is.

Upvotes: 2

Views: 984

Answers (2)

Gregor
Gregor

Reputation: 1468

If you allow mongo to create your _id field then they will all be unique in that collection.

Upvotes: 1

mu is too short
mu is too short

Reputation: 434965

Embedded documents are full blown documents so their _ids are not only unique within the collection, they are globally unique.

Upvotes: 5

Related Questions