Sorting embedded arrays

17 views
Skip to first unread message

Sam

unread,
Dec 14, 2009, 6:28:26 PM12/14/09
to mongodb-user
Hi all,

Sorting doesn't work correctly in the example below:

> db.foo.save({thread: [ { name: 'apple', timestamp: 1 }, { name: 'orange', timestamp: 5} ]})
> db.foo.save({thread: [ { name: 'banana', timestamp: 10 } ] })

The following sorts return records in the same order (don't know
if MongoDB is actually sorting).
> db.foo.find().sort({'thread.timestamp' : 1})
> db.foo.find().sort({'thread.timestamp' : -1})

Is sorting in embedded arrays not supported? Thanks.

Sam

Eliot Horowitz

unread,
Dec 14, 2009, 8:45:06 PM12/14/09
to mongod...@googlegroups.com
There is currently a bug where sorting by fields in arrays doesn't
work without an index.
See this case: http://jira.mongodb.org/browse/SERVER-480 for more
notes and a discussion on the "correct" behavior.
> --
>
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>
>

Kfir Shay

unread,
Dec 15, 2009, 10:44:35 AM12/15/09
to mongod...@googlegroups.com
on the topic of sorting.

while these two queries return the correct sorted result set:
db.coll_name.find().sort( { created_at : -1 } ).limit(1)
db.coll_name.find().sort( { created_at : 1 } ).limit(1)

all these three queries return the same result set:
db.coll_name.find().sort( { created_at : 'ascending' } ).limit(1)
db.coll_name.find().sort( { created_at : 'descending' } ).limit(1)
db.coll_name.find().sort( { created_at : 'whatever' } ).limit(1)

Eliot Horowitz

unread,
Dec 15, 2009, 10:53:10 AM12/15/09
to mongod...@googlegroups.com
the string there is ignored since its invalid.

Kfir Shay

unread,
Dec 15, 2009, 11:57:26 AM12/15/09
to mongod...@googlegroups.com
oh, I thought '(ascending|descending)' was replacing -1|1

Kfir Shay

unread,
Dec 15, 2009, 1:28:57 PM12/15/09
to mongod...@googlegroups.com
am I missing something what is wrong with the syntax?
db.coll_name.find().sort( { created_at : 'descending' } ).limit(1)
db.coll_name.find().sort( { created_at : 'ascending' } ).limit(1)

Michael Dirolf

unread,
Dec 15, 2009, 1:30:38 PM12/15/09
to mongod...@googlegroups.com
you should use -1 and 1 to specify direction, the shell doesn't interpret "descending" and "ascending" iirc

Kfir Shay

unread,
Dec 16, 2009, 3:35:04 PM12/16/09
to mongod...@googlegroups.com
ok yet when doing the same queries using the ruby driver on Solaris I
get a log message telling me the -1, 1 syntax has been deprecated

Kyle Banker

unread,
Dec 16, 2009, 4:05:05 PM12/16/09
to mongod...@googlegroups.com
which version of the ruby driver are you using?  in the latest, this produces no warnings:

@collection.find().sort([ [created_at, 1] ]).limit(1).to_a

Kfir Shay

unread,
Dec 16, 2009, 4:39:31 PM12/16/09
to mongod...@googlegroups.com
got the code from github

(git clone git://github.com/mongodb/mongo-ruby-driver.git)

Michael Dirolf

unread,
Dec 16, 2009, 4:41:22 PM12/16/09
to mongod...@googlegroups.com
it's also different in the ruby driver - :ascending and :descending
should work there IIRC. your first example looked like the shell,
where 1 and -1 are the only options.

Geir Magnusson Jr.

unread,
Dec 16, 2009, 5:37:50 PM12/16/09
to mongod...@googlegroups.com
why not make them consistent?

Michael Dirolf

unread,
Dec 16, 2009, 5:42:06 PM12/16/09
to mongod...@googlegroups.com
As kyle points out, 1 and -1 work in the ruby driver as well. Since
ruby provides symbols, which are sort of a perfect fit here, I think
it makes sense to extend the API and let developers do it either way.
There's nothing like symbols in JS for the shell, so can't do it
there.

Geir Magnusson Jr.

unread,
Dec 16, 2009, 5:47:36 PM12/16/09
to mongod...@googlegroups.com
there are strings...

Just wondering, as this led to a bit of confusion here - we thought it was part of standard things you could pass as values for sort direcion.

geir

Michael Dirolf

unread,
Dec 16, 2009, 5:55:56 PM12/16/09
to mongod...@googlegroups.com
Yeah the sorting APIs are a bit inconsistent right now. It's
compounded by the fact that JS objects are ordered and Ruby hashes /
Python dicts aren't - so the API in Ruby and Python takes a list of
tuples while in JS it takes an object.

I guess that's probably the reason for the naming inconsistency -
since the Ruby driver is already doing some extra work to translate
the sort specifier we added in the :ascending thing as well.

I do think it's okay for the APIs to differ somewhat when it makes
sense to be idiomatic, though. Open to suggestions for how to improve
things.
Reply all
Reply to author
Forward
0 new messages