func insert(documents: [BSON]) -> Result
Insert **documents** into the current collection returning a result status
- parameter documents: BSON documents to be inserted
- returns: Result object with status of insert
func update(updates: [(selector: BSON, update: BSON)]) -> Result
* Update the documents and return a result status
*
* - parameter updates: Tuple of (selector: BSON, update: BSON)
*
* - returns: Result object with status of update
*
* How to use it!
*
* var updates: [(selector: BSON, update: BSON)] = []
* guard var users = collection.find(query: BSON()) else {
* response.status = HTTPResponseStatus.custom(code: 404, message: "Collection users cannot perform find().")
* response.completed()
* return
}
* for user in users {
* let oldBson = BSON()
* oldBson.append(key: "_id", oid: user.oid!)
* let innerBson = BSON()
* innerBson.append(key: "firstname", string: "Ciccio")
* let newdBson = BSON()
* newdBson.append(key: "$set", document: innerBson)
* updates.append((selector: oldBson, update: newdBson))
* }
* if case .error = collection.update(updates: updates) {
* response.status = HTTPResponseStatus.custom(code: 404, message: "Collection users cannot perform multiple update().")
* response.completed()
* return
}
func rename(newDbName: String, newCollectionName: String, dropExisting: Bool) -> Result
Renames the collection using **newDbName** and **newCollectionName**, with option to drop existing collection immediately instead of after the move, returning a result status
- parameter newDbName: String name for db after move
- parameter newCollectionName: String name for collection after move
- parameter dropExisting: Bool option to drop existing collection immediately instead of after move
- returns: Result object with status of renaming
func name() -> String
The collection name as a String
- returns: String the name of the current collection
func validate(full: Bool = false) -> Result
Validates a collection. The method scans a collection’s data structures for correctness and returns a single document that describes the relationship between the logical collection and the physical representation of the data.
- parameter full: Optional. Specify true to enable a full validation and to return full statistics. MongoDB disables full validation by default because it is a potentially resource-intensive operation.
- returns: BSON document describing the relationship between the collection and its physical representation
func stats(options: BSON) -> Result
Returns statistics about the collection formatted according to the options document.
- parameter options: a BSON document defining the format of return.
- **The options document can contain the following fields and values**:
- **scale**: *number*, Optional. The scale used in the output to display the sizes of items. By default, output displays sizes in bytes. To display kilobytes rather than bytes, specify a scale value of 1024.
- **indexDetails**: *boolean*, Optional. If true, **stats()** returns index details in addition to the collection stats. Only works for WiredTiger storage engine. Defaults to false.
- **indexDetailsKey**: *document*, Optional. If **indexDetails** is true, you can use **indexDetailsKey** to filter index details by specifying the index key specification. Only the index that exactly matches **indexDetailsKey** will be returned. If no match is found, **indexDetails** will display statistics for all indexes.
- **indexDetailsName**: *string*, Optional. If **indexDetails** is true, you can use **indexDetailsName** to filter index details by specifying the index name. Only the index name that exactly matches **indexDetailsName** will be returned. If no match is found, **indexDetails** will display statistics for all indexes.
- returns: BSON document with formatted statistics or Results error document
func find(query: BSON = BSON(), fields: BSON? = nil, flags: MongoQueryFlag = MongoQueryFlag.none, skip: Int = 0, limit: Int = 0, batchSize: Int = 0) -> MongoCursor?
Selects documents in a collection and returns a cursor to the selected documents.
- parameter query: Specifies selection filter using query operators. To return all documents in a collection, omit this- Parameter or pass an empty document ({}).
- parameter fields: Optional. Specifies the fields to return in the documents that match the query filter. To return all fields in the matching documents, omit this parameter.
- parameter flags: Optional. set queryFlags for the current search
- parameter skip: Optional. Skip the supplied number of records.
- parameter limit: Optional. return no more than the supplied number of records.
- parameter batchSize: Optional. Change number of automatically iterated documents.
- returns: A cursor to the documents that match the query criteria. When the find() method “returns documents,” the method is actually returning a cursor to the documents.
func createIndex(keys: BSON, options: MongoIndexOptions) -> Result
Creates indexes on collections.
- parameter keys: A document that conains the field and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for descending index, specify a value of -1.
- parameter options: Optional. A document that contains a set of options that controls the creation of the index. see MongoIndexOptions for details.
- returns: a Result status
func dropIndex(name: String) -> Result
Drops or removes the specified index from a collection.
- parameter index: Specifies the index to drop, either by name or by the index specification document.
- returns: a Result status
func drop() -> Result
Removes a collection from the database. The method also removes any indexes associated with the dropped collection.
- returns: a Result status
func count(query: BSON, flags: MongoQueryFlag = MongoQueryFlag.none, skip: Int = 0, limit: Int = 0, batchSize: Int = 0) -> Result
The count of documents that would match a find() query.
- parameter query: The query selection criteria.
- parameter flags: Optional. set queryFlags for the current search
- parameter skip: Optional. Skip the supplied number of records.
- parameter limit: Optional. return no more than the supplied number of records.
- parameter batchSize: Optional. Change number of automatically iterated documents.
- returns: the count of documents that would match a find() query. The count() method does not perform the find() operation but instead counts and returns the number of results that match a query.
func findAndModify(query: BSON?, sort: BSON?, update: BSON?, fields: BSON?, remove: Bool, upsert: Bool, new: Bool) -> Result
Modifies and returns a single document.
- parameter query: Optional. The selection criteria for the modification. The query field employs the same query selectors as used in the db.collection.find() method. Although the query may match multiple documents, findAndModify() will only select one document to modify.
- parameter sort: Optional. Determines which document the operation modifies if the query selects multiple documents. findAndModify() modifies the first document in the sort order specified by this argument.
- parameter update: Must specify either the remove or the update field. Performs an update of the selected document. The update field employs the same update operators or field: value specifications to modify the selected document.
- parameter fields: Optional. A subset of fields to return. The fields document specifies an inclusion of a field with 1, as in: fields: { <field1>: 1, <field2>: 1, ... }.
- parameter remove: Must specify either the remove or the update field. Removes the document specified in the query field. Set this to true to remove the selected document . The default is false.
- parameter upsert: Optional. Used in conjunction with the update field. When true, findAndModify() creates a new document if no document matches the query, or if documents match the query, findAndModify() performs an update. To avoid multiple upserts, ensure that the query fields are uniquely indexed. The default is false.
- parameter new: Optional. When true, returns the modified document rather than the original. The findAndModify() method ignores the new option for remove operations. The default is false.
- returns: Modifies and returns a single document. By default, the returned document does not include the modifications made on the update. To return the document with the modifications made on the update, use the new option.
func getLastError() -> BSON
A BSON document with description of last transaction status
- returns: BSON document with description of last transaction status