Index: library/mongodb/nx-mongo.tcl =================================================================== diff -u -N -r0f881e4bc45e927c8d84c1b1b468ef7537cb9b03 -rab21c8502870e715c567c657146283699e31b01b --- library/mongodb/nx-mongo.tcl (.../nx-mongo.tcl) (revision 0f881e4bc45e927c8d84c1b1b468ef7537cb9b03) +++ library/mongodb/nx-mongo.tcl (.../nx-mongo.tcl) (revision ab21c8502870e715c567c657146283699e31b01b) @@ -434,10 +434,10 @@ # # For interaction with bson structures, we provide on the class # level "bson cond" (a small dsl for a more convenient syntax in - # bson queries), "bson query" (combining conditions with - # ordering), "bson atts (a simplifed property selection) and - # "bson parameter" which translates from a bson structure (tuple) - # into a dashed parameter list used in object creation. + # bson queries), "bson opts" (options like e.g. ordering), "bson + # atts (a simplifed property selection) and "bson parameter" which + # translates from a bson structure (tuple) into a dashed parameter + # list used in object creation. # :method "bson cond" {cond} { @@ -461,23 +461,32 @@ return $bson } - :method "bson query" {{-cond ""} {-orderby ""}} { - #puts "bson query -cond <$cond> -orderby <$orderby>" - set bson [:bson cond $cond] - set result [list \$query document $bson] - - if {[llength $orderby] > 0} { - set bson [list] - foreach attspec $orderby { - lassign $attspec att direction - lappend bson $att int [expr {$direction eq "desc" ? -1 : 1}] - } - lappend result \$orderby document $bson + :method "bson opts" {{-orderby ""} {-atts ""} -limit:integer -skip:integer} { + set result "" + if {$atts ne ""} { + lappend result projection document [:bson atts $atts] } - #puts "bson query -cond <$cond> -orderby <$orderby> => $result" + if {[info exists limit]} { + lappend result limit int64 $limit + } + if {[info exists skip]} { + lappend result skip int64 $skip + } + if {$orderby ne ""} { + lappend result sort document [:bson orderby $orderby] + } return $result } + :method "bson orderby" {orderby} { + set bson [list] + foreach attspec $orderby { + lassign $attspec att direction + lappend bson $att int [expr {$direction eq "desc" ? -1 : 1}] + } + return $bson + } + :method "bson atts" {atts} { set result {} foreach {att value} $atts { @@ -662,9 +671,9 @@ {-orderby ""} } { set tuple [lindex [::nx::mongo::db query ${:mongo_ns} \ - [:bson query -cond $cond -orderby $orderby] \ - -atts [:bson atts $atts] \ - -limit 1] 0] + [:bson cond $cond] \ + -opts [:bson opts -atts $atts -limit 1 -orderby $orderby] \ + ] 0] if {$tuple eq ""} { return "" } @@ -676,17 +685,16 @@ {-atts ""} {-cond ""} {-orderby ""} - {-limit} - {-skip} + {-limit:integer} + {-skip:integer} } { set result [list] set opts [list] if {[info exists limit]} {lappend opts -limit $limit} if {[info exists skip]} {lappend opts -skip $skip} set fetched [::nx::mongo::db query ${:mongo_ns} \ - [:bson query -cond $cond -orderby $orderby] \ - -atts [:bson atts $atts] \ - {*}$opts] + [:bson cond $cond] \ + -opts [:bson opts -orderby $orderby -atts $atts {*}$opts] ] foreach tuple $fetched { lappend result [:bson create $tuple] @@ -706,9 +714,8 @@ if {[info exists limit]} {lappend opts -limit $limit} if {[info exists skip]} {lappend opts -skip $skip} set fetched [::nx::mongo::db query ${:mongo_ns} \ - [:bson query -cond $cond -orderby $orderby] \ - -atts [:bson atts $atts] \ - {*}$opts] + [:bson cond $cond] \ + -opts [:bson opts -orderby $orderby -atts $atts {*}$opts] ] set tuples [list] foreach tuple $fetched { lappend tuples "\{[:bson pp -indent 4 $tuple]\n\}"