MongoDB.Bson.Serialization.Serializers.BsonDocumentSerializer.SetDocumentId C# (CSharp) Метод

SetDocumentId() публичный метод

Sets the document Id.
public SetDocumentId ( object document, object id ) : void
document object The document.
id object The Id.
Результат void
        public void SetDocumentId(object document, object id)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }

            var bsonDocument = (BsonDocument)document;
            var idBsonValue = id as BsonValue;
            if (idBsonValue == null)
            {
                idBsonValue = BsonValue.Create(id); // be helpful and provide automatic conversion to BsonValue if necessary
            }

            BsonElement idElement;
            if (bsonDocument.TryGetElement("_id", out idElement))
            {
                idElement.Value = idBsonValue;
            }
            else
            {
                bsonDocument.InsertAt(0, new BsonElement("_id", idBsonValue));
            }
        }
    }