Permalink
Browse files

mongo.distinct() and mongo-c-driver.zip added

  • Loading branch information...
1 parent c6dc558 commit bc3137f6ff8d9a9cc80d6129c85dadf0ddd68d79 @gerald-lindsly committed Mar 5, 2012
Showing with 36 additions and 0 deletions.
  1. +30 −0 MongoDB.pas
  2. BIN MongoDelphiDriver.res
  3. +6 −0 Test.dpr
  4. BIN Test.res
  5. BIN mongo-c-driver.zip
View
@@ -190,6 +190,18 @@ TMongo = class(TObject)
The collection namespace (ns) is in the form 'database.collection'.
key is the name of the field on which to index.
Returns nil if successful; otherwise, a TBson document that describes the error. }
+ function distinct(ns : string; key : string) : TBson;
+ { Returns a BSON document containing a field 'values' which
+ is an array of the distinct values of the key in the given collection (ns).
+ Example:
+ var
+ b : TBson;
+ names : TStringArray;
+ begin
+ b := mongo.distinct('test.people', 'name');
+ names := b.find('values').GetStringArray();
+ end
+ }
function indexCreate(ns : string; key : string) : TBson; overload;
{ Create an index for the given collection so that accesses by the given
key are faster.
@@ -829,6 +841,24 @@ implementation
bson_dispose(res);
end;
+ function TMongo.distinct(ns : string; key : string) : TBson;
+ var b : TBson;
+ buf : TBsonBuffer;
+ p : Integer;
+ db, collection : string;
+ begin
+ p := pos('.', ns);
+ if p = 0 then
+ Raise Exception.Create('Expected a ''.'' in the namespace');
+ db := Copy(ns, 1, p-1);
+ collection := Copy(ns, p+1, Length(ns) - p);
+ buf := TBsonBuffer.Create();
+ buf.append('distinct', collection);
+ buf.append('key', key);
+ b := buf.finish;
+ Result := command(db, b);
+ end;
+
function TMongo.command(db : string; cmdstr : string; arg : OleVariant) : TBson;
begin
Result := command(db, BSON([cmdstr, arg]));
View
Binary file not shown.
View
@@ -258,6 +258,12 @@ begin
WriteLn(mongo.count(ns, query));
+ (* get distinct names *)
+ x := mongo.distinct(ns, 'name');
+ i := x.find('values').subiterator;
+ while i.next do
+ writeln(i.value);
+
(* add a user to database 'admin' *)
mongo.addUser('Gerald', 'P97gwep16');
View
BIN Test.res
Binary file not shown.
View
Binary file not shown.

0 comments on commit bc3137f

Please sign in to comment.