const classafMongo::Collection

sys::Obj
  afMongo::Collection

Represents a MongoDB collection.

aggregate

Source

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

Source

Obj? aggregateCursor(Str:Obj?[] pipeline, |Cursor->Obj? func)

create

Source

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

Source

This createCapped(Int sizeInBytes, Int? maxNoOfDocs := null, [Str:Obj?]? options := null)

delete

Source

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 is true 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/

deleteAll

Source

Int deleteAll([Str:Obj?]? writeConcern := null)

Convenience method for deleting ALL documents in a Collection. Returns the number of documents deleted.

Note this is MUCH quicker than dropping the Collection.

Same as calling:

deleteMulti([["q":[:], "limit":0]], false, writeConcern)["n"]
deleteMulti

Source

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

Source

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

Source

This drop(Bool force := false)

Drops this collection, but only if it exists.

Note that deleting all documents is MUCH quicker than dropping the Collection. See deleteAll for details.

If force is true then no checks are made. This will result in an error if the collection doesn't exist.

@see http://docs.mongodb.org/manual/reference/command/drop/

dropAllIndexes

Source

This dropAllIndexes()

Drops ALL indexes on the collection. Be careful!

@see http://docs.mongodb.org/manual/reference/command/dropIndexes/

exists

Source

Bool exists()

Returns true if this collection exists.

find

Source

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. Use find() 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

Source

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 query as a list of documents.

If sort is a Str it should the name of an index to use as a hint. If sort is a [Str:Obj?] map, it should be a sort document with field names as keys. Values may either be the standard Mongo 1 and -1 for ascending / descending or the strings ASC / DESC.

The sort map, should it contain more than 1 entry, must be ordered.

projection alters / limits which fields returned in the query results.

Note that findAll(...) is a convenience for calling find(...) and returning the cursor as a list.

findAndDelete

Source

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

Source

[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 is true 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

Source

Int findCount([Str:Obj?]? query := null)

Returns the number of documents that would be returned by the given query.

@see Cursor.count

findOne

Source

[Str:Obj?]? findOne([Str:Obj?]? query := null, Bool checked := true)

An (optomised) method to return one document from the given query.

Throws MongoErr if no documents are found and checked is true, returns null otherwise. Always throws MongoErr if the query returns more than one document.

@see http://docs.mongodb.org/manual/reference/operator/query/

get

Source

@Operator
[Str:Obj?]? get(Obj? id, Bool checked := true)

Convenience / shorthand notation for findOne(["_id" : id], checked)

group

Source

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

Source

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

Source

Str[] indexNames()

Returns all the index names of this collection.

insert

Source

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

Source

Str:Obj? insertMulti(Str:Obj?[] inserts, Bool? ordered := null, [Str:Obj?]? writeConcern := null)

makeFromDatabase

Source

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

Source

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

Source

Str:Obj? mapReduce(Code mapFunc, Code reduceFunc, [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. If out 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.

out

Obj

If a Str then 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 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.

Note if an out option 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

Source

Str name { private set }

The simple name of the collection.

qname

Source

Str qname { private set }

The qualified name of the collection. It takes the form of:

<database>.<collection>
runCmd

Source

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.

See https://docs.mongodb.com/manual/reference/command/

size

Source

Int size()

Returns the number of documents in the collection.

@see http://docs.mongodb.org/manual/reference/command/count/

stats

Source

Str:Obj? stats(Int scale := 1)

Returns storage statistics for this collection.

@see http://docs.mongodb.org/manual/reference/command/collStats/

update

Source

Str:Obj? update(Str:Obj? query, Str:Obj? updateCmd, Bool? multi := false, Bool? upsert := false, [Str:Obj?]? writeConcern := null)

Runs the given updateCmd against documents returned by query. Inspect return value for upserted IDs. Note this does not throw an Err should the query not match any documents.

If multi is true then the multiple documents may be updated, otherwise the update is limited to one.

If upsert is true and no documents are updated, then one is inserted.

@see http://docs.mongodb.org/manual/reference/command/update/

updateMulti

Source

Str:Obj? updateMulti(Str:Obj?[] updates, Bool? ordered := null, [Str:Obj?]? writeConcern := null)