diff --git a/README.md b/README.md index 718db10..078c30c 100755 --- a/README.md +++ b/README.md @@ -358,7 +358,7 @@ Set what fields are returned. fieldList is a list of field names with 1 or 0. Search --- -* $mongo search ?-namespace namespace? ?-fields fieldList? ?-array arrayName? ?-typearray typeArrayName? ?-list listVar? ?-offset offset? ?-limit limit? +* $mongo search ?-namespace namespace? ?-fields fieldList? ?-array arrayName? ?-typearray typeArrayName? ?-list listVar? ?-offset offset? ?-limit limit? ?-comparebson bson? ?-sort fieldList? ?-code code? Create a cursor against the specified namespace. @@ -374,6 +374,12 @@ Create a cursor against the specified namespace. * If -limit is present it specifies the maximum number of rows that can be returned. +* If -comparebson is present it specifies a bson object that contains an encoded query to return only rows matching the query specification. + +* If -sort is present it contains a list of fields to sort by, from most significant to least significant. if the first character of the field name is a dash that indicates sorting in reverse order. + +* If -code is present, it specifies a code body that is executed for each row returned + Example --- @@ -433,3 +439,10 @@ This is a little gross and is going to be simplified, but... $cursor init $namespace $cursor set_query $query ``` + +Bugs +--- + +The code is currently early beta quality so there could be quite a few bugs including ones that trigger a coredump. + +There are almost for sure some memory leaks so until those are all tracked down expect long-running jobs' memory footprint to grow and plan accordingly. diff --git a/mongo.tcl b/mongo.tcl index 3455f8d..f7e3a45 100644 --- a/mongo.tcl +++ b/mongo.tcl @@ -58,6 +58,13 @@ proc _search {args} { set limit $value } + "-comparebson" { + set compareBson $value + } + + "-sort" { + set sort $value + } "-namespace" { set namespace $value @@ -95,7 +102,36 @@ proc _search {args} { $cursor set_skip $offset } + # generate the query + set queryBson [::mongo::bson create #auto] + + if {[info exists compareBson]} { + $queryBson start_object {$query} bson $compareBson finish_object + + } + + # if there's a sort option, append it to the query + if {[info exists sort]} { + $queryBson start_object {$orderby} + foreach field $sort { + if {[string index $field 0] == "-"} { + $queryBson int [string range $field 1 end] 0 + } else { + $queryBson int $field 1 + } + } + $queryBson finish_object + } + + $cursor set_query $queryBson + + # + # iterate over the matching rows. + # you have to invoke "next" to get the first row, by the way + # while {[$cursor next]} { + # pull the data out of the row, get types too if a type array + # is specified if {[info exists arrayName]} { if {![info exists typeArrayName]} { unset -nocomplain array @@ -106,10 +142,12 @@ proc _search {args} { } } + # if they specified -list, give them their list of type triplets if {[info exists listName]} { set listVar [$cursor to_list] } + # if they specified a code block, execute it if {[info exists code]} { uplevel $code }