Q:

How to set MongoDB automatically assign ID to each sub-document?

public class SubClass {
    public bool isValid {
    get;
    set;
    }
}

public class MainClass {
    public List < SubClass > SubClasses {
    get;
    set;
    }
}

MainClass m = new MainClass() {
    Name = "Mohsen", SubClasses = new List < SubClass > (new SubClass[] {
    new SubClass() {
        IsValid = true
    }
    })
};
MongoMainClassCollection.Save < MainClass > (m);

The problem is that all SubClass ID's have a default value of zero.
I want MongoDB assign a unique ID for any SubClass whenever I save a MainClass.  
Is there a way to configure MongoDB to do this ?  

A:

MongoDB won't do this automatically for you. MongoDB doesn't add ID's to subdocuments. Best bet would be to add something in your Subclass constructor to automatically populate the ID for you. 
public class SubDocument {
    //if using objectId as ID
    public ObjectId Id = { get; set;}

    public SubDocument(){
        Id = ObjectId.GenerateNewId()
    }

}

