Reputation: 313
The mongo db docs answer this question, but the answer seems wrong to me. From the mongodb documentation on Model Referenced One-to-Many Relationships Between Documents, they give the following answer:
{
_id: "oreilly",
name: "O'Reilly Media",
founded: 1980,
location: "CA"
}
{
_id: 123456789,
title: "MongoDB: The Definitive Guide",
author: [ "Kristina Chodorow", "Mike Dirolf" ],
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English",
publisher_id: "oreilly"
}
{
_id: 234567890,
title: "50 Tips and Tricks for MongoDB Developer",
author: "Kristina Chodorow",
published_date: ISODate("2011-05-06"),
pages: 68,
language: "English",
publisher_id: "oreilly"
}
Is this a documentation error? The example's use of _id seems to conflict with the documented description of Mongodb's ObjectId.
Would the correct solution be for publisher_id to have the 12 byte BSON object id?
Upvotes: 0
Views: 498
Reputation: 6921
I think that the only requirement for _id is to be unique. If you don't explicitly give one then the one generated is the object id as described in docs.
See quote from the same place you are linking:
Because ObjectIds are small, most likely unique, and fast to generate, MongoDB uses ObjectIds as the default value for the _id field if the _id field is not specified
Upvotes: 3