const classafMongo::Collection
sys::Obj afMongo::Collection
Represents a MongoDB collection.
- aggregate
Str:Obj?[] aggregate(Str:Obj?[] pipeline, [Str:Obj?]? options := null)Performs an aggregation operation using a sequence of stage-based manipulations.
The
optionsparameter is merged with the Mongo command and may contain the following:Options
Type
Desc
explain
Bool
Returns pipeline processing information.
allowDiskUse
Bool
If
trueallows temp data to be stored on disk.cursor
Doc
Controls the cursor creation.
@see
- aggregateCursor
Obj? aggregateCursor(Str:Obj?[] pipeline, |Cursor->Obj? func)Same as
aggregate()but returns a cursor to iterate over the results.@see
- create
This create([Str:Obj?]? options := null)Creates a new collection explicitly.
There is usually no no need to call this unless you wish explicitly set collection options.
@see http://docs.mongodb.org/manual/reference/command/create/
- createCapped
This createCapped(Int sizeInBytes, Int? maxNoOfDocs := null, [Str:Obj?]? options := null)Creates a capped collection.
@see http://docs.mongodb.org/manual/reference/command/create/
- delete
Int delete(Str:Obj? query, Bool deleteAll := false, [Str:Obj?]? writeConcern := null)Deletes documents that match the given query. Returns the number of documents deleted.
If
deleteAllistruethen all documents matching the query will be deleted, otherwise only the first match will be deleted.@see http://docs.mongodb.org/manual/reference/command/delete/
- deleteMulti
Str:Obj? deleteMulti(Str:Obj?[] deletes, Bool? ordered := null, [Str:Obj?]? writeConcern := null)Executes multiple delete queries.
@see http://docs.mongodb.org/manual/reference/command/delete/
- distinct
Obj[] distinct(Str field, [Str:Obj?]? query := null)Finds the distinct values for a specified field.
@see http://docs.mongodb.org/manual/reference/command/distinct/
- drop
This drop(Bool force := false)Drops this collection, but only if it exists.
If
forceistruethen the collection is dropped regardless. Note this may result in an error if the collection doesn't exist.- dropAllIndexes
This dropAllIndexes()Drops ALL indexes on the collection. Be careful!
@see http://docs.mongodb.org/manual/reference/command/dropIndexes/
- exists
Bool exists()Returns
trueif this collection exists.- find
Obj? find(Str:Obj? query, |Cursor->Obj? func)Creates a Cursor over the given
queryallowing you to iterate over results. Documents are downloaded from MongoDB in batches behind the scene as and when required. Usefind()to optomise iterating over a massive result set.Returns what is returned from the given cursor function.
second := collection.find([:]) |cursor->Obj?| { first := cursor.next second := cursor.next return second }- findAll
Str:Obj?[] findAll([Str:Obj?]? query := null, Obj? sort := null, Int skip := 0, Int? limit := null, [Str:Obj?]? projection := null)Returns the result of the given
queryas a list of documents.If
sortis a Str it should the name of an index to use as a hint. Ifsortis a[Str:Obj?]map, it should be a sort document with field names as keys. Values may either be the standard Mongo1and-1for ascending / descending or the stringsASC/DESC.The
sortmap, should it contain more than 1 entry, must be ordered.projectionalters / limits which fields returned in the query results.Note that
findAll(...)is a convenience for callingfind(...)and returning the cursor as a list.- findAndDelete
Str:Obj? findAndDelete(Str:Obj? query, [Str:Obj?]? options := null)Deletes and returns a single document. If the query returns multiple documents then the first one is delete.
The
optionsparameter is merged with the Mongo command and may contain the following:Options
Type
Desc
sort
Doc
Orders the result to determine which document to delete.
fields
Doc
Defines which fields to return.
Example:
collection.findAndDelete(query, ["fields": ["myEntity.myField":1]]
@see http://docs.mongodb.org/manual/reference/command/findAndModify/
- findAndUpdate
[Str:Obj?]? findAndUpdate(Str:Obj? query, Str:Obj? updateCmd, Bool returnModified, [Str:Obj?]? options := null)Updates and returns a single document. If the query returns multiple documents then the first one is updated.
If
returnModifiedistruethen the document is returned after the updates have been applied.Returns
nullif no document was found.The
optionsparameter is merged with the Mongo command and may contain the following:Options
Type
Desc
upsert
Bool
Creates a new document if no document matches the query
sort
Doc
Orders the result to determine which document to update.
fields
Doc
Defines which fields to return.
Example:
collection.findAndUpdate(query, cmd, true, ["upsert":true, "fields": ["myEntity.myField":1]]
@see http://docs.mongodb.org/manual/reference/command/findAndModify/
- findCount
Int findCount([Str:Obj?]? query := null)Returns the number of documents that would be returned by the given
query.@see Cursor.count
- findOne
[Str:Obj?]? findOne([Str:Obj?]? query := null, Bool checked := true)An (optomised) method to return one document from the given
query.Throws
MongoErrif no documents are found andcheckedis true, returnsnullotherwise. Always throwsMongoErrif the query returns more than one document.@see http://docs.mongodb.org/manual/reference/operator/query/
- get
@Operator
[Str:Obj?]? get(Obj? id, Bool checked := true)Convenience / shorthand notation for
findOne(["_id" : id], checked)- group
Str:Obj?[] group(Obj key, Str:Obj? initial, Code reduceFunc, [Str:Obj?]? options := null)Groups documents by the specified key and performs simple aggregation functions.
keymust either be a list of field names (Str[]) or a function that creates a "key object" (Str).The
optionsparameter is merged with the Mongo command and may contain the following:Options
Type
Desc
cond
Doc
Determines which documents in the collection to process.
finalize
Func
Runs on each item in the result set before the final value is returned.
@see http://docs.mongodb.org/manual/reference/command/group/
- index
Index index(Str indexName)Returns an
Indexof the given name.Note this just instantiates the Fantom object, it does not create anything in the database.
- indexNames
Str[] indexNames()Returns all the index names of this collection.
- insert
Int insert(Str:Obj? document, [Str:Obj?]? writeConcern := null)Inserts the given document. Returns the number of documents inserted.
@see http://docs.mongodb.org/manual/reference/command/insert/
- insertMulti
Str:Obj? insertMulti(Str:Obj?[] inserts, Bool? ordered := null, [Str:Obj?]? writeConcern := null)Inserts multiple documents.
@see http://docs.mongodb.org/manual/reference/command/insert/
- makeFromDatabase
new makeFromDatabase(Database database, Str name, |This? f := null)Creates a
Collectionwith the given name under the database.Note this just instantiates the Fantom object, it does not create anything in the database.
- makeFromQname
new makeFromQname(ConnectionManager conMgr, Str qname, |This? f := null)Creates a
Collectionwith the given qualified (dot separated) name.Note this just instantiates the Fantom object, it does not create anything in the database.
- mapReduce
Str:Obj? mapReduce(Code mapFunc, Code reduceFunc, [Str:Obj?]? options := null)Run a map-reduce aggregation operation over the collection.
If
outis a Str, it specifies the name of a collection to store the results. Ifoutis a Map, it specifies the action to take.The
optionsparameter is merged with the Mongo command and may contain the following:Options
Type
Desc
query
Doc
The selection criteria for input documents.
sort
Doc
Sorts the input documents.
limit
Int
The maximum number of documents given to the map function.
finalize
Func
Follows the
reducemethod and modifies the output.scope
Doc
global variables used in the
map,reduceandfinalizefunctions.out
Obj
If a
Strthen it's the name of a collection to store the results in, if a Doc, then it specifies the action to take.jsMode
Bool
If
false(default) objects from themapfunction are converted into BSON before being handed to thefinalizefunction.verbose
Bool
If
true(default) then timing information is returned in the result.Note if an
outoption is not specified, it is taken to be inline and the returned document contains the results.@see http://docs.mongodb.org/manual/reference/command/mapReduce/
- name
Str name { private set }The simple name of the collection.
- qname
Str qname { private set }The qualified name of the collection. It takes the form of:
<database>.<collection>
- runCmd
Str:Obj? runCmd(Str:Obj? query)Runs an arbitrary command against this
Collection. Example, to return the size of the collection:size := runCmd( ["count" : "<collectionName>"] )["n"]->toInt
This is a low level operation.
- size
Int size()Returns the number of documents in the collection.
@see http://docs.mongodb.org/manual/reference/command/count/
- stats
Str:Obj? stats(Int scale := 1)Returns storage statistics for this collection.
@see http://docs.mongodb.org/manual/reference/command/collStats/
- update
Int update(Str:Obj? query, Str:Obj? updateCmd, Bool? multi := (Bool?)false, Bool? upsert := (Bool?)false, [Str:Obj?]? writeConcern := null)Runs the given
updateCmdagainst documents returned byquery. Returns the number of documents modified. Note this does not throw an Err should the query not match any documents.If
multiistruethen the multiple documents may be updated, otherwise the update is limited to one.If
upsertistrueand no documents are updated, then one is inserted.@see http://docs.mongodb.org/manual/reference/command/update/
- updateMulti
Str:Obj? updateMulti(Str:Obj?[] updates, Bool? ordered := null, [Str:Obj?]? writeConcern := null)Runs multiple update queries.
@see http://docs.mongodb.org/manual/reference/command/update/