Beispiel #1
0
        /// <summary>
        /// Inserts an object into the database.
        /// </summary>
        /// <param name="Object">Object to insert.</param>
        public async Task Insert(object Object)
        {
            ObjectSerializer Serializer     = this.GetObjectSerializer(Object);
            string           CollectionName = Serializer.CollectionName;
            IMongoCollection <BsonDocument> Collection;

            if (string.IsNullOrEmpty(CollectionName))
            {
                Collection = this.defaultCollection;
            }
            else
            {
                Collection = this.GetCollection(CollectionName);
            }

            if (Serializer.HasObjectIdField)
            {
                if (Serializer.HasObjectId(Object))
                {
                    throw new Exception("Object already has an Object ID. If updating an object, use the Update method.");
                }
                else
                {
                    await Serializer.GetObjectId(Object, true);
                }
            }
            else
            {
                BsonDocument Doc = Object.ToBsonDocument(Object.GetType(), Serializer);
                await Collection.InsertOneAsync(Doc);
            }
        }