Create a client connection to a MongoDB database.
Class MongoDB( [host],[port] )
| host | default to localhost. |
| port | default to 27017. |
| Methods | |
| addUser | Adds a user to the DB. |
| authenticate | Authenticates a user on the DB. |
| command | Run a command on a database |
| connect | Connect or reconnect to MongoDB server. |
| count | Counts the entities in a collection. |
| createIndex | Creates an index for the given namespace and key. |
| disconnect | Disconnect to MongoDB server. |
| dropCollection | Removes a collection from a database. |
| dropDatabase | Drops the database. |
| find | Finds instances corespoding to the given query. |
| findOne | Finds an instance. |
| host | When given a parameter, change the host for the next connection attempt. Else return the current host. |
| insert | Inserts an instance. |
| isConnected | Checks if the DB is connected. |
| port | When given a parameter, change the port for the next connection attempt. Else return the current port. |
| remove | Removes an instance. |
| update | Updates an existing instance. |
Adds a user to the DB.
MongoDB.addUser( db, user, pass )
| db | Database |
| user | User ID to be added |
| pass | Password for the new user. |
| Return | true if user was added |
Authenticates a user on the DB.
MongoDB.authenticate( db, user, pass )
| db | Database |
| user | User ID |
| pass | Password |
| Return | true if authenticated |
Run a command on a database
MongoDB.command( db, cmd )
| db | Database |
| cmd | BSON instance |
| Return | BSON result or nil |
Connect or reconnect to MongoDB server.
MongoDB.connect()
Counts the entities in a collection.
MongoDB.count( db, coll, [query] )
| db | Database |
| coll | Collection to be queried. |
| query | BSON instance |
| Return | Total count or -1 on error |
Creates an index for the given namespace and key.
MongoDB.createIndex( ns, key, [unique],[drop_dups] )
| ns | namespace |
| key | BSON instance |
| unique | (boolean) default false |
| drop_dups | (boolean) default false |
| Return | BSON result or nil |
Disconnect to MongoDB server.
MongoDB.disconnect()
Removes a collection from a database.
MongoDB.dropCollection( db, coll )
| db | The database |
| coll | The collection to be dropped |
| Return | true on success |
Drops the database.
MongoDB.dropDatabase( db )
| db | The database to be dropped |
| Return | true on success |
Finds instances corespoding to the given query.
MongoDB.find( [query],[fields],[skip],[limit] )
| query | BSON instance |
| fields | BSON instance |
| skip | default 0 |
| limit | default 0 (all) |
| Return | An array of BSON results or nil |
Finds an instance.
MongoDB.findOne( ns, [query] )
| ns | namespace |
| query | BSON instance |
| Return | BSON result or nil |
When given a parameter, change the host for the next connection attempt. Else return the current host.
MongoDB.host( [host] )
| host | |
| Return | self (with param) or the current host. |
Inserts an instance.
MongoDB.insert( ns, bson )
| ns | namespace |
| bson | BSONObj instance, or an array of BSON instances |
| Return | true on success |
Checks if the DB is connected.
MongoDB.isConnected()
| Return | true if connected |
When given a parameter, change the port for the next connection attempt. Else return the current port.
MongoDB.port( [port] )
| port | |
| Return | self (with param) or the current port. |
Removes an instance.
MongoDB.remove( ns, cond )
| ns | namespace |
| cond | BSON instance (conditions) |
| Return | true on success |
Updates an existing instance.
MongoDB.update( ns, cond, op, [upsert],[multiple] )
| ns | namespace |
| cond | BSON instance (conditions) |
| op | BSON instance (operations) |
| upsert | (boolean) default true |
| multiple | (boolean) default true |
| Return | true on success |