diff --git a/README.md b/README.md index 7e4a3df..d5d3e8d 100755 --- a/README.md +++ b/README.md @@ -142,6 +142,12 @@ Append a key and a bson-library-generated oid to the bson object. Enumerate bson object as a list. +* $bson to_array arrayName ?typeArrayName? + +Enumerate bson object as an array of key-value pairs. Embedded bson arrays and objects are set to contain subordinate bson in list format. + +if typeArrayName is specified, for each key of the key-value pairs, an element is inserted into typeArray for the same key with the value being the name of the bson datatype such as int, double, string, oid, etc. + * $bson print Print is for debugging only, it sort of shows you what's in the bson object. @@ -275,6 +281,10 @@ Any error condition (CURSOR_INVALID, CURSOR_PENDING, CURSOR_QUERY_FAIL, CURSOR_B Return the bson object of the current row as a list. +* $cursor to_array arrayName ?typeArrayName? + +Set an array and possibly typeArray similarly to bson to_array. + * $cursor set_query $bson Set a cursor's query with a configured bson object. @@ -320,6 +330,25 @@ Allow reads even if a shard is down. Set what fields are returned. fieldList is a list of field names with 1 or 0. 1 says to include the field, 0 says to exclude it. The fieldList is sticky for future queries. This may change. See http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/ for how the 1/0 thing works. +Search +--- + +* $mongo search ?-namespace namespace? ?-fields fieldList? ?-array arrayName? ?-typearray typeArrayName? ?-list listVar? ?-offset offset? ?-limit limit? + +Create a cursor against the specified namespace. + +* If -fields is present, fieldList is a list of fieldNames. Fields returned are restricted to the named fields. If a field name starts with a dash it indicates that that field is to be explicitly suppressed. + +* If -array is present, arrayName is the name of an array set in the caller's context containing elements for the fields of each row returned. + +* If -typearray is present it's the name of an array set in the caller's context containing elements for the field names of each row returned with the values being the bson data type. + +* If -list is present, the name of a variable that will receive the bson list. + +* If -offset is present, the first offset rows of the result are skipped. + +* If -limit is present it specifies the maximum number of rows that can be returned. + Example --- diff --git a/mongo.tcl b/mongo.tcl index b1fe045..3455f8d 100644 --- a/mongo.tcl +++ b/mongo.tcl @@ -8,6 +8,115 @@ package require mongo namespace eval ::mongo { +# +# _search - search routine called by search method of tcl mongodb objects +# +# m search -namespace tutorial.persons -array row -code {parray row; puts ""} +# +proc _search {args} { + set obj [lindex $args 0] + set args [lrange $args 1 end] + + if {[llength $args] & 1} { + error "list of key-value pairs must have an even number of arguments" + } + + foreach "key value" $args { + if {[string index $key 0] != "-"} { + error "key '$key' of key-value pairs doesn't start with a -" + } + + switch -exact -- $key { + "-fields" { + set fields $value + } + + "-code" { + set code $value + } + + "-array" { + set arrayName $value + upvar $arrayName array + } + + "-typearray" { + set typeArrayName $value + upvar $typeArrayName typeArray + } + + "-list" { + set listName $value + upvar $listName listVar + } + + "-offset" { + set offset $value + } + + "-limit" { + set limit $value + } + + + "-namespace" { + set namespace $value + } + + default { + error "unknown key $key: must be one of -namespace, -fields, -code, -array, -typearray, -list, -offset, -limit" + } + } + } + + if {![info exists namespace]} { + error "required field '-namespace' is missing" + } + + set cursor [$obj cursor #auto $namespace] + + if {[info exists fields]} { + set fieldList [list] + foreach field $fields { + if {[string index $field 0] == "-"} { + lappend fieldList [string range $field 1 end] 0 + } else { + lappend fieldList $field 1 + } + } + $cursor set_fields $fieldList + } + + if {[info exists limit]} { + $cursor set_limit $limit + } + + if {[info exists offset]} { + $cursor set_skip $offset + } + + while {[$cursor next]} { + if {[info exists arrayName]} { + if {![info exists typeArrayName]} { + unset -nocomplain array + $cursor to_array array + } else { + unset -nocomplain array typeArray + $cursor to_array array typeArray + } + } + + if {[info exists listName]} { + set listVar [$cursor to_list] + } + + if {[info exists code]} { + uplevel $code + } + } + + $cursor destroy +} } ;# namespace ::mongo