vibe.d beta banner
get vibe.d
0.10.1

Asynchronous I/O that doesn’t get in your way, written in D

Function MongoCursor.sort

Controls the order in which the query returns matching documents.

MongoCursor sort(T) (
  T order
);

This method must be called before starting to iterate, or an exception will be thrown. If multiple calls to sort() are issued, only the last one will have an effect.

Parameters

NameDescription
order A BSON object convertible value that defines the sort order of the result. This BSON object must be structured according to the MongoDB documentation (see below).

Returns

Reference to the modified original cursor instance.

Throws

An exception if there is a query or communication error. Also throws if the method was called after beginning of iteration.

See Also

http://docs.mongodb.org/manual/reference/method/cursor.sort

Example

import vibe.core.log;
import vibe.db.mongo.mongo;

void test()
@safe {
	auto db = connectMongoDB("127.0.0.1").getDatabase("test");
	auto coll = db["testcoll"];

	// find all entries in reverse date order
	foreach (entry; coll.find().sort(["date": -1]))
		() @safe { logInfo("Entry: %s", entry); } ();

	// the same, but using a struct to avoid memory allocations
	static struct Order { int date; }
	foreach (entry; coll.find().sort(Order(-1)))
		logInfo("Entry: %s", entry);
}

Authors

Sönke Ludwig

Copyright

© 2012-2014 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.