Skip to content

Commit

Permalink
Add -comparebson and -sort options to search method.
Browse files Browse the repository at this point in the history
BUGZID:
  • Loading branch information
lehenbauer committed Feb 19, 2014
1 parent 6766a58 commit 547b0db
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
---
Expand Down Expand Up @@ -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.
38 changes: 38 additions & 0 deletions mongo.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ proc _search {args} {
set limit $value
}

"-comparebson" {
set compareBson $value
}

"-sort" {
set sort $value
}

"-namespace" {
set namespace $value
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 547b0db

Please sign in to comment.