Kalanamith
Kalanamith

Reputation: 20648

How to save an Entity without the object Id or an id MongoDB C# Driver

Um looking for a way to save a collection without an Id My Entity Declaration as follows

                public class TestA : IEntity
                    {
                        public string AppleSet { get; set; }
                        public string MapSted { get; set; }

                        [BsonIgnore]
                        public string Id { get; set; }


                    }

I need to declare ID field in the entity, is there a way to tell mongodb to ignore Id for a specific entity and save it to the database?

Upvotes: 1

Views: 2011

Answers (1)

mnemosyn
mnemosyn

Reputation: 46301

A document must have an id. That is a requirement of the database itself. Even if you convince the driver to attempt to insert a document without an id, mongod itself will add an _id field automatically. As you can see from this link, the inserted _id will be returned and is hence part of the lower-level API. As such, _id is not just any other field, but has special semantics, one rule being that it has to be unique.

Upvotes: 2

Related Questions