private int GetIdDocument(Type t) { lock (SectionGetId) { var bsonDoc = CollectionsContainer.GetCollectionsByName("docsid").FirstOrDefault(); var coll = CollectionsContainer.GetMongoCollection(bsonDoc); var filter = Builders <BsonDocument> .Filter.Eq("DocName", t.Name); var incrDocIdOptions = Builders <BsonDocument> .Update.Inc("DocId", 1); var rec = coll.Find(filter).CountAsync(); rec.Wait(); if (rec.Result == 0) { var res = coll.InsertOneAsync(new DocsId { DocId = 2, DocName = t.Name }.ToBsonDocument()); res.Wait(); if (res.Exception != null) { return (BsonSerializer.Deserialize <DocsId>( coll.FindOneAndUpdateAsync <BsonDocument>(filter, incrDocIdOptions) .Result).DocId); } return(1); } return (BsonSerializer.Deserialize <DocsId>( coll.FindOneAndUpdateAsync <BsonDocument>(filter, incrDocIdOptions).Result).DocId); } }