# Module db_mongo

This module implements a higher level wrapper for mongodb. Example:

    import mongo, db_mongo, oids, json

    var conn = db_mongo.open()

    # construct JSON data:
    var data = %{"a": %13, "b": %"my string value",
                 "inner": %{"i": %71} }

    var id = insertID(conn, "test.test", data)

    for v in find(conn, "test.test", "this.a == 13"):
      print v

    delete(conn, "test.test", id)
    close(conn)

# Imports

mongo, oids, json

# Types

    EDb = object of EIO

  exception that is raised if a database error occurs

    TDbConn = TMongo

  a database connection; alias for `TMongo`

    FDb = object of FIO

  effect that denotes a database operation

    FReadDb = object of FDb

  effect that denotes a read operation

    FWriteDb = object of FDb

  effect that denotes a write operation

# Procs

    proc dbError(db: TDbConn; msg: string) {.noreturn, raises: [EDb], tags: [].}

  raises an EDb exception with message msg.

    proc Close(db: var TDbConn) {.tags: [FDb], raises: [].}

  closes the database connection.

    proc Open(host: string = defaultHost; port: int = defaultPort): TDbConn {.
        tags: [FDb], raises: [EDb].}

  opens a database connection. Raises EDb if the connection could not be established.

    proc jsonToBSon(j: PJsonNode; oid: Toid): TBson {.raises: [], tags: [].}

  converts a JSON value into the BSON format. The result must be `destroyed` explicitely!

    proc `[]`(obj: var TBson; fieldname: cstring): TBson {.raises: [EInvalidIndex],
        tags: [].}

  retrieves the value belonging to fieldname. Raises EInvalidKey if the attribute does not exist.

    proc getId(obj: var TBson): Toid {.raises: [EInvalidIndex], tags: [].}

  retrieves the `_id` attribute of obj.

    proc insertID(db: var TDbConn; namespace: string; data: PJsonNode): Toid {.
        tags: [FWriteDb], raises: [].}

  converts data to BSON format and inserts it in namespace. Returns the generated OID for the `_id` field.

    proc insert(db: var TDbConn; namespace: string; data: PJsonNode) {.
        tags: [FWriteDb], raises: [].}

  converts data to BSON format and inserts it in namespace.

    proc update(db: var TDbConn; namespace: string; obj: var TBson) {.
        tags: [FReadDb, FWriteDb], raises: [EInvalidIndex].}

  updates obj in namespace.

    proc update(db: var TDbConn; namespace: string; oid: Toid; obj: PJsonNode) {.
        tags: [FReadDb, FWriteDb], raises: [EInvalidIndex].}

  updates the data with oid to have the new data obj.

    proc delete(db: var TDbConn; namespace: string; oid: Toid) {.tags: [FWriteDb],
        raises: [].}

  Deletes the object belonging to oid.

    proc delete(db: var TDbConn; namespace: string; obj: var TBson) {.
        tags: [FWriteDb], raises: [EInvalidIndex].}

  Deletes the object obj.

# Iterators

    iterator find(db: var TDbConn; namespace: string): var TBson {.tags: [FReadDb],
        raises: [].}

  iterates over any object in namespace.

    iterator find(db: var TDbConn; namespace: string; query, fields: var TBson): var TBson {.
        tags: [FReadDb], raises: [].}

  yields the fields of any document that suffices query.

    iterator find(db: var TDbConn; namespace: string; query: var TBson;
                  fields: varargs[string]): var TBson {.tags: [FReadDb], raises: [].}

  yields the fields of any document that suffices query. If fields is `[]` the whole document is yielded.

    iterator find(db: var TDbConn; namespace: string; query: string;
                  fields: varargs[string]): var TBson {.tags: [FReadDb], raises: [].}

  yields the fields of any document that suffices query. If fields is `[]` the whole document is yielded.

Generated: 2013-05-21 09:55:14 UTC