Index: library/mongodb/example-nsf-mongo.tcl =================================================================== diff -u -rbcf94f0b3bac771ebd9a65a8eb9dd6030651bb7e -r9248d253eb37bcefbfa38a1d86df306f40922444 --- library/mongodb/example-nsf-mongo.tcl (.../example-nsf-mongo.tcl) (revision bcf94f0b3bac771ebd9a65a8eb9dd6030651bb7e) +++ library/mongodb/example-nsf-mongo.tcl (.../example-nsf-mongo.tcl) (revision 9248d253eb37bcefbfa38a1d86df306f40922444) @@ -20,7 +20,7 @@ classes array {0 object {$ref string courses $id oid 1}}] ::mongo::insert $mongoConn tutorial.persons [list name string Gustaf projects string nsf age int 53] ::mongo::insert $mongoConn tutorial.persons [list name string Stefan projects string nsf] - ::mongo::insert $mongoConn tutorial.persons [list name string Franz info object {x int 203 y int 102} age int 29] + ::mongo::insert $mongoConn tutorial.persons [list name string Franz info object {x int 203 y int 102} age int 29 projects string gtat] ::mongo::insert $mongoConn tutorial.persons [list name string Victor a array {0 string "x" 1 string "y"} age int 31] ::mongo::insert $mongoConn tutorial.persons [list name string Selim ts timestamp {1302945037 1} d date 1302947619279] @@ -48,8 +48,11 @@ puts [join [::mongo::query $mongoConn tutorial.persons [list \$query object {a string "x"}]] \n] puts stderr "\nEmbedded object has some value (info.y > 100)" -puts [join [::mongo::query $mongoConn tutorial.persons [list \$query object {info.y object {$gt int 100}}]] \n] +puts [join [::mongo::query $mongoConn tutorial.persons [list \$query object {info.y object {$gt int 100}}]] \n] +puts stderr "\nProjects in {nsf gtat}" +puts [join [::mongo::query $mongoConn tutorial.persons [list \$query object {projects object {$in array {0 string nsf 1 string gtat}}}]] \n] + if {0} { puts stderr "\nDelete members of project abc" ::mongo::remove $mongoConn tutorial.persons [list projects string abc] Index: library/mongodb/example-nx-mongo.tcl =================================================================== diff -u -rc85ca5808548fc506bfc39600bafb8b11ecfac43 -r9248d253eb37bcefbfa38a1d86df306f40922444 --- library/mongodb/example-nx-mongo.tcl (.../example-nx-mongo.tcl) (revision c85ca5808548fc506bfc39600bafb8b11ecfac43) +++ library/mongodb/example-nx-mongo.tcl (.../example-nx-mongo.tcl) (revision 9248d253eb37bcefbfa38a1d86df306f40922444) @@ -28,14 +28,14 @@ # Insert a tuple to the database via creating an object, saving and # destroying it: # -set p [Person new -name Gustaf -projects nsf -age 53] +set p [Person new -name Gustaf -projects {nsf nx nxmongo} -age 53] $p save; $p destroy # # The insert operation of above can be achieved with less typing via # the conveniance method "insert": # -Person insert -name Stefan -projects nsf +Person insert -name Stefan -projects {nsf nx} Person insert -name Joe -projects abc -age 23 Person insert -name Franz -projects {gtat annobackend abc} -age 29 @@ -65,6 +65,17 @@ puts "\t$p:\t[$p name]" } +puts "\nProject members of nsf or gtat:" +foreach p [Person find all -cond {projects in {nsf gtat}}] { + puts "\t$p:\t[$p name]" +} + +puts "\nProject members on both nsf and nxmongo:" +foreach p [Person find all -cond {projects all {nsf nxmongo}}] { + puts "\t$p:\t[$p name]" +} + + puts "\nAll Persons sorted by name (ascending):" foreach p [Person find all -orderby name] { puts "\t$p:\t[$p name]" Index: library/mongodb/example-nx-reference-many.tcl =================================================================== diff -u -r8f4ba2af8b01697a3aab727a2794cb3b456d6632 -r9248d253eb37bcefbfa38a1d86df306f40922444 --- library/mongodb/example-nx-reference-many.tcl (.../example-nx-reference-many.tcl) (revision 8f4ba2af8b01697a3aab727a2794cb3b456d6632) +++ library/mongodb/example-nx-reference-many.tcl (.../example-nx-reference-many.tcl) (revision 9248d253eb37bcefbfa38a1d86df306f40922444) @@ -52,8 +52,8 @@ # Retrieve the entry from the database: set g [Group find first -cond {name = "grp2"}] set members [list] -foreach m [$g members] { - lappend members [Member find first -cond [list _id = $m]] +foreach m [Member find all -cond [list _id in [$g members]]] { + lappend members $m } puts stderr "Members of group [$g name]:" foreach m $members {puts stderr "\t[$m name]"} Index: library/mongodb/example-nx-reference-one.tcl =================================================================== diff -u -r8f4ba2af8b01697a3aab727a2794cb3b456d6632 -r9248d253eb37bcefbfa38a1d86df306f40922444 --- library/mongodb/example-nx-reference-one.tcl (.../example-nx-reference-one.tcl) (revision 8f4ba2af8b01697a3aab727a2794cb3b456d6632) +++ library/mongodb/example-nx-reference-one.tcl (.../example-nx-reference-one.tcl) (revision 9248d253eb37bcefbfa38a1d86df306f40922444) @@ -1,8 +1,8 @@ # # This is an introductory example how to use the nx mongo mapping for # referencing some object. We use here an example of an Posting having -# a (possible compound) user as originator. All example work the same -# way as well with with multivalued attributes. +# a single (possible compound) user as originator. All example work +# the same way as well with with multivalued attributes. # # Gustaf Neumann fecit, May 2011 # Index: library/mongodb/nx-mongo.tcl =================================================================== diff -u -rbcf94f0b3bac771ebd9a65a8eb9dd6030651bb7e -r9248d253eb37bcefbfa38a1d86df306f40922444 --- library/mongodb/nx-mongo.tcl (.../nx-mongo.tcl) (revision bcf94f0b3bac771ebd9a65a8eb9dd6030651bb7e) +++ library/mongodb/nx-mongo.tcl (.../nx-mongo.tcl) (revision 9248d253eb37bcefbfa38a1d86df306f40922444) @@ -131,12 +131,16 @@ } } - :method "bson encode" {value} { - if {[:isMultivalued]} { - set c -1 - set array [list] - foreach v $value {lappend array [incr c] {*}[:bson encodeValue $v]} - return [list array $array] + :method "bson encodeArray" {value} { + set c -1 + set array [list] + foreach v $value {lappend array [incr c] {*}[:bson encodeValue $v]} + return [list array $array] + } + + :public method "bson encode" {-array:switch value} { + if {[:isMultivalued] || $array} { + return [:bson encodeArray $value] } else { return [:bson encodeValue $value] } @@ -224,7 +228,7 @@ } :public method "get relop" {op} { - array set "" {< $lt > $gt <= $lte >= $gte != $ne} + array set "" {< $lt > $gt <= $lte >= $gte != $ne in $in all $all} return $($op) } @@ -245,6 +249,9 @@ ">" - "<" - "<=" - ">=" - "!=" { lappend bson $att object [list [:get relop $op] [$slot mongotype] $value] } + "in" - "all" { + lappend bson $att object [list [:get relop $op] {*}[$slot bson encode -array $value]] + } default {error "unknown operator $op"} } } @@ -329,11 +336,11 @@ # # index method # - :public method index {att {-type 1}} { + :public method index {att {-type 1} args} { if {![info exists :mongo_ns]} {:mongo_setup} - # todo: 2d index will need a different type - #::mongo::index $::mongoConn ${:mongo_ns} [list $att int $type] - db index ${:mongo_ns} [list $att int $type] + # todo: 2nd index will need a different type + # todo: multi-attribute indices + db index ${:mongo_ns} [list $att int $type] {*}$args } #