db.runCommand(
{ aggregate : "article", pipeline : [
{ $group : {
_id : "$author",
docsPerAuthor : { $sum : 1 } , /* add one to "docsPerAuthor" for every document by this (_id) author */
viewsPerAuthor : { $sum : "$pageViews" } /* sum the pageViews for every document by this (_id) author */
}}
]});
just added the run_command into mongo_dbclient.cpp.
get an error during make.
mongo_dbclient.cpp:716: error: ‘luaL_tointeger’ was not declared in this
scope
haven't found luaL_tointeger nor a macro in lauxlib.h.
(box is ubuntu 10.04x64 desktop. Lua 5.1)
compiles without error when I change
-> int options = luaL_tointeger(L, 4);
to
-> int options = lua_tointeger(L, 4);
RESULTS:
local jquery = "{distinct: 'country', key: 'short', query: ''}"
res,err = mdb:run_command("test", jquery)
is delivering a correct nested (stats, values) result-table with
distinct countries. great!
a more compley query: (SQL -> SELECT m_country_ref, COUNT(*) FROM test
WHERE m_country_ref >= 100 GROUP BY m_country_ref)
local jquery = "{group : {ns : 'country' , cond : {m_country_ref: {$gt:
100}}, key : {m_country_ref : 1}, initial : {count: 0}, $reduce :
function(obj,prev) {prev.count++}}}"
is crashing with as JSON parsing error.
Both queries are running as unmodified string direct via mongo shell
command without errors.
------------------------------------------------------------------------------------------------------
http://www.mongodb.org/display/DOCS/Aggregation
EXAMPLE:
> db.addresses.insert({"zip-code": 10010})
> db.addresses.insert({"zip-code": 10010})
> db.addresses.insert({"zip-code": 99701})
DISTINCT: -> db.runCommand( { distinct: 'addresses', key: 'zip-code' } )
{
"values" : [
10010,
99701
],
"stats" : {
"n" : 3,
"nscanned" : 3,
"nscannedObjects" : 3,
"timems" : 0,
"cursor" : "BasicCursor"
},
"ok" : 1
}
GROUP: ->
db.addresses.group( { key: {'zip-code': true}, cond: {}, reduce:
function(obj,prev) {prev.count ++;}, initial: {count: 0} });
[
{
"zip-code" : 10010,
"count" : 2
},
{
"zip-code" : 99701,
"count" : 1
}
]
or via $cmd
> db.$cmd.findOne( {group : { ns: 'addresses' , key: {'zip-code':
true}, cond: {}, $reduce: function(obj,prev) {prev.count ++;}, initial:
{count: 0} }});
{
"retval" : [
{
"zip-code" : 10010,
"count" : 2
},
{
"zip-code" : 99701,
"count" : 1
}
],
"count" : 3,
"keys" : 2,
"ok" : 1
}
OR
> db.runCommand( {group : { ns: 'addresses' , key: {'zip-code': true},
cond: {}, $reduce: function(obj,prev) {prev.count ++;}, initial: {count:
0} }});
{
"retval" : [
{
"zip-code" : 10010,
"count" : 2
},
{
"zip-code" : 99701,
"count" : 1
}
],
"count" : 3,
"keys" : 2,
"ok" : 1
}
--------------------------------------------------------------------------------
get_last_error is getting me this error during compiling.
-> mongo_dbclient.cpp:762: error: ‘struct std::string’ has no member
named ‘cstr’
unfortunatly I can't help in this, since I am not a C/C++ coder.
I would like to help in testing (with big datasets mapreduce etc.) and
later some parts of documentation/examples when needed.
best
tom
changed to c_str but getting now:
mongo_dbclient.cpp:761: error: no matching function for call to
‘mongo::DBClientBase::getLastError(bool&, bool&, int&, int&)’
/mongo-cxx-driver-latest/mongo/client/dbclient.h:475: note: candidates
are: std::string mongo::DBClientWithCommands::getLastError()
make: *** [mongo_dbclient.o] Fehler 1
Query-Error-Message:
Assertion: 10340:Failure parsing JSON string near: group : {
0x7f18902ac9f9 0x7f1890298267 0x7f1890285151 0x4089d1 0x412289 0x408e9d
0x408547 0x4085c2 0x405c31 0x40441a 0x404f43 0x4089d1 0x408e54 0x408547
0x4085c2 0x405bd7 0x404351 0x7f1890561c4d 0x404149
/usr/local/lib/lua/5.1/mongo.so(_ZN5mongo11msgassertedEiPKc+0x119)
[0x7f18902ac9f9]
/usr/local/lib/lua/5.1/mongo.so(_ZN5mongo8fromjsonEPKcPi+0x5f7)
[0x7f1890298267]
/usr/local/lib/lua/5.1/mongo.so(+0x57151) [0x7f1890285151]
lua() [0x4089d1]
lua() [0x412289]
lua() [0x408e9d]
lua() [0x408547]
lua() [0x4085c2]
lua(lua_pcall+0x51) [0x405c31]
lua() [0x40441a]
lua() [0x404f43]
lua() [0x4089d1]
lua() [0x408e54]
lua() [0x408547]
lua() [0x4085c2]
lua(lua_cpcall+0x27) [0x405bd7]
lua(main+0x41) [0x404351]
/lib/libc.so.6(__libc_start_main+0xfd) [0x7f1890561c4d]
lua() [0x404149]
false Error calling mongo.Connection.run_command: Failure parsing JSON
string near: group : {
best
tom
just a quick notice.
running a real distinct query against 6.681.816 documents
direct via mongo shell takes 4.342 msec. and 3.936 msec. via luamongo.
nice! that's really fast.
thanks for this speedy binding evan...
str = db:get_last_error()
print (type(str)) -> string
print(str) -> ""
tbl = db:get_last_error_detailed()
print(tbl) -> table: 0xd3b920
pairs(tbl) ->
ok 1
connectionId 70
err NULL
n 0
tom
with a solid runCommand mongodb will combine the traditional
RDBMS strenght with noSQl advantages. very nice.
tomorrow I will download the current nightly built mongodb 2.1
which shall contain the new aggregate framework for testing.
additional I will check the mapreduce function again with lua tables
instead JSON and report what happened. a quick test for the eval
function could be interesting for server side routines also.
thx a lot evan, I own you a little more than a beer ;-)