Exemplo n.º 1
0
        /// <summary> Get an Enumerable from the given database, where ID field matches the given ID, or null. </summary>
        /// <typeparam name="T"> Generic type of items to get </typeparam>
        /// <param name="databaseName"> Name of database to sample  </param>
        /// <param name="idField"> Field of ID to match </param>
        /// <param name="id"> ID to match in field </param>
        /// <returns> All items with matching id, or an empty list </returns>
        public List <T> GetAll <T>(string databaseName, string idField, string id) where T : DBEntry
        {
            var      filter = BsonHelpers.Query($"{{ \"{idField}\": \"{id}\" }}");
            List <T> result = Collection <T>(databaseName).Find(filter).ToList();

            return(result);
        }
Exemplo n.º 2
0
        /// <summary> Gets raw data from the DB as a JsonObject </summary>
        /// <param name="database"> Database to get out of </param>
        /// <param name="collection"> Collection to get out of </param>
        /// <param name="idField"> ID field to match </param>
        /// <param name="id"> ID to get from field </param>
        /// <returns> Data matching query </returns>
        public JsonObject GetData(string database, string collection, string idField, string id)
        {
            var  filter = BsonHelpers.Query($"{{ \"{idField}\": \"{id}\" }}");
            BDoc result = dbClient.GetDatabase(database).GetCollection <BDoc>(collection).Find(filter).FirstOrDefault();

            return(FromBson(result));
        }
Exemplo n.º 3
0
        /// <summary> Get all items from the default database, where the given ID field matches the given ID. </summary>
        /// <typeparam name="T"> Generic type of items to get </typeparam>
        /// <param name="idField"> ID Field to look for 'guid' within </param>
        /// <param name="id"> ID to look for 'guid' </param>
        /// <returns> All elements matching the given ID </returns>
        /// <remarks> For example, if `Item` has a field `owner:Guid`, this can be used to find all `Item`s owned by a given entity. </remarks>

        public List <T> GetAll <T>(string idField, string id) where T : DBEntry
        {
            var filter = BsonHelpers.Query($"{{ \"{idField}\": \"{id}\" }}");
            //var filter = Builders<T>.Filter.Eq(idField, id);

            List <T> result = Collection <T>().Find(filter).ToList();

            return(result);
        }
Exemplo n.º 4
0
        /// <summary> Get an item from the default database, where ID field matches the given ID, or null. </summary>
        /// <typeparam name="T"> Generic type of item to get </typeparam>
        /// <param name="databaseName"> Name of database to sample </param>
        /// <param name="idField"> Field of ID to match </param>
        /// <param name="id"> ID to match in field </param>
        /// <returns> First item matching id, or null. </returns>
        public T Get <T>(string databaseName, string idField, string id) where T : DBEntry
        {
            // Todo: Benchmark and figure out which of these is faster
            var filter = BsonHelpers.Query($"{{ \"{idField}\": \"{id}\" }}");
            //var filter = Builders<T>.Filter.Eq(idField, id);

            T result = Collection <T>(databaseName).Find(filter).FirstOrDefault();

            return(result);
        }