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
options
parameter is merged with the Mongo command and may contain the following:Options Type Desc ------- ---- ---- explain Bool Returns pipeline processing information. allowDiskUse Bool If 'true' allows 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(Bool? autoIndexId := (Bool?)true, Bool? usePowerOf2Sizes := (Bool?)true)
Creates a new collection explicitly.
There is no no need to call this unless you wish to disable the (default) auto indexing of IDs or disable the (default) power of 2 allocation strategy.
@see http://docs.mongodb.org/manual/reference/command/create/
- createCapped
This createCapped(Int size, Int? maxNoOfDocs := null, Bool? autoIndexId := (Bool?)true, Bool? usePowerOf2Sizes := (Bool?)true)
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
deleteAll
istrue
then 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 checked := true)
Drops this collection.
- dropAllIndexes
This dropAllIndexes()
Drops ALL indexes on the collection. Be careful!
@see http://docs.mongodb.org/manual/reference/command/dropIndexes/
- exists
Bool exists()
Returns
true
if this collection exists.- find
Obj? find(Str:Obj? query, |Cursor->Obj? func)
Creates a Cursor over the given
query
allowing 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 := ([Str:Obj?])[:], Obj? sort := null, Int skip := 0, Int? limit := null, Str[]? fieldNames := null)
Returns the result of the given
query
as a list of documents.If
sort
is a Str it should the name of an index to use as a hint. Ifsort
is a[Str:Obj?]
map, it should be a sort document with field names as keys. Values may either be the standard Mongo1
and-1
for ascending / descending or the stringsASC
/DESC
.The
sort
map, should it contain more than 1 entry, must be ordered.fieldNames
are names of the fields to be 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
options
parameter 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
returnModified
istrue
then the document is returned after the updates have been applied.Returns
null
if no document was found.The
options
parameter 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
Returns the number of documents that would be returned by the given
query
.@see Cursor.count
- findOne
[Str:Obj?]? findOne(Str:Obj? query, Bool checked := true)
An (optomised) method to return one document from the given
query
.Throws
MongoErr
if no documents are found andchecked
is true, returnsnull
otherwise. Always throwsMongoErr
if 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.
key
must either be a list of field names (Str[]
) or a function that creates a "key object" (Str
).The
options
parameter 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
Index
of 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
Collection
with 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
Collection
with 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, Obj out, [Str:Obj?]? options := null)
Run a map-reduce aggregation operation over the collection.
If
out
is a Str, it specifies the name of a collection to store the results. Ifout
is a Map, it specifies the action to take.The
options
parameter 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 'reduce' method and modifies the output. scope Doc global variables used in the 'map', 'reduce' and 'finalize' functions. jsMode Bool If 'false' (default) objects from the 'map' function are converted into BSON before being handed to the 'finalize' function. verbose Bool If 'true' (default) then timing information is returned in the result.
@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>
- 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
updateCmd
against documents returned byquery
. Returns the number of documents modified. Note this does not throw an Err should the query not match any documents.If
multi
istrue
then the multiple documents may be updated, otherwise the update is limited to one.If
upsert
istrue
and 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/