const classafMongo::MongoColl
sys::Obj afMongo::MongoColl
Represents a MongoDB collection.
@see https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst
- aggregate
MongoCur aggregate(Str:Obj?[] pipeline, |MongoCmd? optsFn := null)
Processes documents through an aggregation pipeline.
@see
- connMgr
const MongoConnMgr connMgr
The underlying connection manager.
- count
Int count([Str:Obj?]? filter := null)
Returns the number of documents that match the given filter. (Uses an
aggregate
cmd.)- create
Void create(|MongoCmd? optsFn := null)
Creates a new collection explicitly.
There is usually no no need to call this unless you wish explicitly set collection options.
db.collection("name").create { it->capped = true it->size = 64 * 1024
// no of bytesit->max = 14// no of docs}@see https://www.mongodb.com/docs/manual/reference/command/create/
- db
MongoDb db()
Creates an
MongoDb
instance of the associated DB.- dbName
const Str dbName
The name of the database.
- delete
Int delete(Str:Obj? filter, |MongoCmd? optsFn := null)
Deletes all documents that match the given filter, and returns the number of documents deleted.
delete(["rick":"morty"]) { it->limit = 1 it->hint = "_indexName_" it->collation = [...] }
@see https://www.mongodb.com/docs/manual/reference/command/delete/
- deleteAll
Int deleteAll()
Deletes ALL documents in a Collection.
Note this is MUCH quicker than dropping the Collection.
- drop
Void 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
istrue
then no checks are made. This will result in an error if the collection does not exist.@see https://www.mongodb.com/docs/manual/reference/command/drop/
- dropAllIndexes
Void dropAllIndexes()
Drops ALL indexes on the collection. Be careful!
@see https://www.mongodb.com/docs/manual/reference/command/dropIndexes/
- exists
Bool exists()
Returns
true
if this collection exists.- find
MongoCur find([Str:Obj?]? filter := null, |MongoCmd? optsFn := null)
Returns documents that match the given filter. Many options are possible.
find(["rick":"morty"]) { it->sort = ["fieldName":1] it->hint = "_indexName_" it->skip = 50 it->limit = 100 it->projection = ["_id":1, "name":1] it->batchSize = 101 it->singleBatch = true it->collation = [...] }.toList
- findAndDelete
[Str:Obj?]? findAndDelete(Str:Obj? filter, |MongoCmd? optsFn := null)
Finds, deletes, and returns a single document. Returns
null
if no matching document was found.@see https://www.mongodb.com/docs/manual/reference/command/findAndModify/
- findAndUpdate
[Str:Obj?]? findAndUpdate(Str:Obj? filter, Str:Obj? update, |MongoCmd? optsFn := null)
Finds, updates, and returns a single document. Returns
null
if no matching document was found.@see https://www.mongodb.com/docs/manual/reference/command/findAndModify/
- findOne
[Str:Obj?]? findOne(Str:Obj? filter, Bool checked := true)
Return one document that matches the given
filter
.Throws an
Err
if no documents are found andchecked
istrue
.Always throws
Err
if the filter returns more than one document.@see https://www.mongodb.com/docs/manual/reference/command/find/
- get
@
Operator
[Str:Obj?]? get(Obj? id, Bool checked := true)Convenience / shorthand notation for
findOne(["_id" : id], checked)
- index
Returns an
MongoIdx
of the given name.Note this just instantiates the Fantom object, it does not create anything in MongoDB.
- insert
Void insert(Str:Obj? document)
Inserts the given document.
@see https://www.mongodb.com/docs/manual/reference/command/insert/
- insertMany
Void insertMany(Str:Obj?[] documents, |MongoCmd? optsFn := null)
Inserts many documents.
Default behaviour is to stop when inserting fails. See
ordered
option for details.@see https://www.mongodb.com/docs/manual/reference/command/insert/
- listIndexNames
Str[] listIndexNames()
Returns all the index names in this collection.
- listIndexes
MongoCur listIndexes()
Returns all the indexes in this collection.
@see https://www.mongodb.com/docs/manual/reference/command/listIndexes/
- make
new make(MongoConnMgr connMgr, Str name, Str? dbName := 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.
- name
const Str name
The simple name of the collection.
- qname
Str qname()
Returns the qualified name of this collection. It takes the form of:
<database>.<collection>
- replace
Str:Obj? replace(Str:Obj? filter, Str:Obj? replacement, |MongoCmd? optsFn := null)
Finds a single document that matches the given filter, and replaces it. The
_id
field is NOT replaced.replace(["rick":"morty"], ["rick":"sanchez"]) { it->upsert = true }
@see https://www.mongodb.com/docs/manual/reference/command/update/ @see https://www.mongodb.com/docs/manual/reference/operator/update/
- size
Int size()
Returns the number of documents in the collection.
The count is based on the collection's metadata, which provides a fast but sometimes inaccurate count for sharded clusters.
- textSearch
MongoCur textSearch(Str search, [Str:Obj?]? opts := null)
Performs a text search on the collection.
Text searching makes use of stemming and ignores language stop words. Quotes may be used to search for exact phrases and prefixing a word with a hyphen-minus (-) negates it.
Results are automatically ordered by search relevance.
To use text searching, make sure the Collection has a text Index else MongoDB will throw an Err.
col.textSearch("some text")
options
may include the following:Name
Type
Desc
$language
Bool
Determines the list of stop words for the search and the rules for the stemmer and tokenizer. See Supported Text Search Languages. Specify
none
for simple tokenization with no stop words and no stemming. Defaults to the language of the index.$caseSensitive
Bool
Enable or disable case sensitive searching. Defaults to
false
.$diacriticSensitive
Int
Enable or disable diacritic sensitive searching. Defaults to
false
.Text searches may be mixed with regular filters. See
MongoQ
for details.@see https://docs.mongodb.com/manual/reference/operator/query/text/.
- update
Str:Obj? update(Str:Obj? filter, Str:Obj? updates, |MongoCmd? optsFn := null)
Runs the given
updateCmd
against documents that match the given filter.update(["rick":"morty"], ["\$set":["rick":"sanchez"]]) { it->upsert = true it->multi = false
// defaults to trueit->hint = "_indexName_" it->collation = [...] }Inspect return value for upserted IDs.
(By default, this will update multiple documents.)
@see https://www.mongodb.com/docs/manual/reference/command/update/ @see https://www.mongodb.com/docs/manual/reference/operator/update/