Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: automatically generated IDs are zeroes

I'm using MongoDB and official C# driver 0.9

I'm just checking how embedding simple documents works.

There are 2 easy classes:

public class User
{
    public ObjectId _id { get; set; }
    public string Name { get; set; }
    public IEnumerable<Address> Addresses { get;set; }
}

public class Address
{
    public ObjectId _id { get; set; }
    public string Street { get; set; }
    public string House { get; set; }
}

I create a new user:

var user = new User
{
    Name = "Sam",
    Addresses = (new Address[] { new Address { House = "BIGHOUSE", Street = "BIGSTREET" } })
};

collection.Insert(user.ToBsonDocument());

The user is successfully saved, so is his address.

After typing

db.users.find()

in MongoDB shell, I got the following result:

{ "_id" : ObjectId("4e572f2a3a6c471d3868b81d"), "Name" : "Sam",  "Addresses" : [
        {
                "_id" : ObjectId("000000000000000000000000"),
                "Street" : "BIGSTREET",
                "House" : "BIGHOUSE"
        }
] }

Why is address' object id 0?

Doing queries with the address works though:

collection.FindOne(Query.EQ("Addresses.Street", streetName));

It returns the user "Sam".

like image 397
Alex Avatar asked Jan 04 '11 13:01

Alex


People also ask

How are MongoDB IDs generated?

By default, MongoDB generates a unique ObjectID identifier that is assigned to the _id field in a new document before writing that document to the database. In many cases the default unique identifiers assigned by MongoDB will meet application requirements.

Are MongoDB IDs predictable?

Mongo ObjectIds are generated in a predictable manner, the 12-byte ObjectId value consists of: a 4-byte value representing the seconds since the Unix epoch, a 3-byte machine identifier, a 2-byte process id, and.

Are MongoDB IDs sequential?

Although MongoDB does not support auto-increment sequence as a default feature like some relational databases, we can still achieve this functionality using a counter collection. The counter collection will have a single document that tracks the current unique identifier value.

Are MongoDB object IDs unique?

MongoDB is a NoSQL database that operates with collections and documents. Each document created on MongoDB has a unique object ID property. So when creating a document without entering an ID, the document will be created with an auto-generated ID.


2 Answers

It's not so much a bug as a case of unmet expectations. Only the top level _id is automatically assigned a value. Any embedded _ids should be assigned values by the client code (use ObjectId.GenerateNewId). It's also possible that you don't even need an ObjectId in the Address class (what is the purpose of it?).

like image 124
Robert Stam Avatar answered Oct 06 '22 00:10

Robert Stam


Use BsonId attribute:

public class Address
{
    [BsonId]
    public string _id { get; set; }
    public string Street { get; set; }
    public string House { get; set; }
}

Identifying the Id field or property

To identify which field or property of a class is the Id you can write:

public class MyClass {
    [BsonId]
    public string SomeProperty { get; set; }
}

Driver Tutorial

Edit

It's actually not working. I will check later why. If you need get it work use following:

    [Test]
   public void Test()
    {
        var collection = Read.Database.GetCollection("test");

        var user = new User
        {
            Name = "Sam",
            Addresses = (new Address[] { new Address { House = "BIGHOUSE", Street = "BIGSTREET", _id = ObjectId.GenerateNewId().ToString() } })
        };

        collection.Insert(user.ToBsonDocument());
    }
like image 20
Andrew Orsich Avatar answered Oct 06 '22 00:10

Andrew Orsich