Skip to content

Commit

Permalink
polydb:method find_one in the database
Browse files Browse the repository at this point in the history
  • Loading branch information
apaffenholz committed Jul 15, 2024
1 parent e22a1f9 commit 9062d1f
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/polydb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import NetworkOptions

import Mongoc

import Mongoc: find
import Mongoc: find, find_one

#Polymake.Polydb's types store information via
# a corresponding Mongoc type variable
Expand Down Expand Up @@ -123,6 +123,7 @@ julia> length(collection, "DIM"=>3, "N_FACETS"=>5)
function Base.length(c::Collection{T}, d::Pair...) where T
return Base.length(c.mcol, Mongoc.BSON(d...))
end

"""
find(c::Collection{T}, d::Dict=Dict(); opts::Union{Nothing, Dict})
Expand Down Expand Up @@ -166,6 +167,53 @@ function Mongoc.find(c::Collection{T}, d::Pair...) where T
return Cursor{T}(Mongoc.find(c.mcol, Mongoc.BSON(d...)))
end

"""
find_one(c::Collection{T}, d::Dict=Dict(); opts::Union{Nothing, Dict})
Return one document from a collection `c` matching the criteria given by `d`.
Apply search options `opts`.
# Examples
```julia-repl
julia> db = Polymake.Polydb.get_db();
julia> collection = db["Polytopes.Lattice.SmoothReflexive"];
julia> query = Dict("DIM"=>5, "N_FACETS"=>8);
julia> opts = Dict("skip"=>13);
julia> pm_object = Polymake.Polydb.find_one(collection, query, opts=opts);
julia> typeof(pm_object)
Polymake.LibPolymake.BigObjectAllocated
```
"""
function Mongoc.find_one(c::Collection{T}, d::Dict=Dict(); opts::Union{Nothing, Dict}=nothing) where T
p = Mongoc.find_one(c.mcol, Mongoc.BSON(d); options=(isnothing(opts) ? nothing : Mongoc.BSON(opts)))
return isnothing(p) ? nothing : parse_document(p)

Check warning on line 193 in src/polydb.jl

View check run for this annotation

Codecov / codecov/patch

src/polydb.jl#L191-L193

Added lines #L191 - L193 were not covered by tests
end

"""
find_one(c::Collection{T}, d::Pair...)
Search a collection `c` for documents matching the criteria given by `d`.
# Examples
```julia-repl
julia> db = Polymake.Polydb.get_db();
julia> collection = db["Polytopes.Lattice.SmoothReflexive"];
julia> pm_object = Polymake.Polydb.find_one(collection, "DIM"=>3, "N_FACETS"=>5);
julia> typeof(pm_object)
Polymake.LibPolymake.BigObjectAllocated
```
"""
function Mongoc.find_one(c::Collection{T}, d::Pair...) where T
p = Mongoc.find_one(c.mcol, Mongoc.BSON(d...))
return isnothing(p) ? nothing : parse_document(p)

Check warning on line 214 in src/polydb.jl

View check run for this annotation

Codecov / codecov/patch

src/polydb.jl#L212-L214

Added lines #L212 - L214 were not covered by tests
end

"""
Collection{T}(c::Collection)
Expand Down

0 comments on commit 9062d1f

Please sign in to comment.