On this page:
3.1 Servers
mongo?
create-mongo
mongo-list-databases
mongo-db-names
3.2 Databases
mongo-db
mongo-db-execute-command!
mongo-db-collections
mongo-db-create-collection!
mongo-db-drop-collection!
mongo-db-drop
mongo-db-profiling/ c
mongo-db-profiling
set-mongo-db-profiling!
mongo-db-profiling-info
mongo-db-valid-collection?
3.3 Collections
mongo-collection
mongo-collection-drop!
mongo-collection-valid?
mongo-collection-full-name
mongo-collection-find
mongo-collection-insert-docs!
mongo-collection-insert-one!
mongo-collection-insert!
mongo-collection-remove!
mongo-collection-modify!
mongo-collection-replace!
mongo-collection-repsert!
mongo-collection-count
3.3.1 Indexing
mongo-collection-index!
mongo-collection-indexes
mongo-collection-drop-index!
3.4 Cursors
mongo-cursor?
mongo-cursor-done?
mongo-cursor-kill!

3 Basic Operations

 (require (planet jaymccarthy/mongodb:1:6/basic/main))

The basic API of MongoDB is provided by this module.

3.1 Servers

(mongo? x)  boolean?
  x : any/c
A test for Mongo servers.

(create-mongo [#:host host #:port port])  mongo?
  host : string = "localhost"
  port : port-number? = 27017
Creates a connection to the specified Mongo server.

(mongo-list-databases m)  (vectorof bson-document/c)
  m : mongo?
Returns information about the databases on a server.

(mongo-db-names m)  (listof string?)
  m : mongo?
Returns the names of the databases on the server.

3.2 Databases

(struct mongo-db (mongo name)
  #:extra-constructor-name make-mongo-db)
  mongo : mongo?
  name : string?
A structure representing a Mongo database.

(mongo-db-execute-command! db cmd)  bson-document/c
  db : mongo-db?
  cmd : bson-document/c
Executes command cmd on the database db and returns Mongo’s response. Refer to List of Database Commands for more details.

(mongo-db-collections db)  (listof string?)
  db : mongo-db?
Returns a list of collection names in the database.

(mongo-db-create-collection! db 
  name 
  #:capped? capped? 
  #:size size 
  [#:max max]) 
  mongo-collection?
  db : mongo-db?
  name : string?
  capped? : boolean?
  size : number?
  max : (or/c false/c number?) = #f
Creates a new collection in the database and returns a handle to it. Refer to Capped Collections for details on the options.

(mongo-db-drop-collection! db name)  bson-document/c
  db : mongo-db?
  name : string?
Drops a collection from the database.

(mongo-db-drop db)  bson-document/c
  db : mongo-db?
Drops a database from its server.

mongo-db-profiling/c : contract?
Defined as (symbols 'none 'low 'all).
(mongo-db-profiling db)  mongo-db-profiling/c
  db : mongo-db?
Returns the profiling level of the database.
(set-mongo-db-profiling! db v)  boolean?
  db : mongo-db?
  v : mongo-db-profiling/c
Sets the profiling level of the database. Returns #t on success.

(mongo-db-profiling-info db)  bson-document/c
  db : mongo-db?
Returns the profiling information from the database. Refer to Database Profiler for more details.

(mongo-db-valid-collection? db name)  boolean?
  db : mongo-db?
  name : string?
Returns #t if name is a valid collection.

3.3 Collections

(struct mongo-collection (db name)
  #:extra-constructor-name make-mongo-collection)
  db : mongo-db?
  name : string?
A structure representing a Mongo collection.

(mongo-collection-drop! mc)  void
  mc : mongo-collection?
Drops the collection from its database.
(mongo-collection-valid? mc)  boolean?
  mc : mongo-collection?
Returns #t if mc is a valid collection.
(mongo-collection-full-name mc)  string?
  mc : mongo-collection?
Returns the full name of the collection.
(mongo-collection-find mc 
  query 
  [#:tailable? tailable? 
  #:slave-okay? slave-okay? 
  #:no-timeout? no-timeout? 
  #:selector selector 
  #:skip skip 
  #:limit limit]) 
  mongo-cursor?
  mc : mongo-collection?
  query : bson-document/c
  tailable? : boolean? = #f
  slave-okay? : boolean? = #f
  no-timeout? : boolean? = #f
  selector : (or/c false/c bson-document/c) = #f
  skip : int32? = 0
  limit : (or/c false/c int32?) = #f
Performs a query in the collection. Refer to Querying for more details.

If limit is #f, then a limit of 2 is sent. This is the smallest limit that creates a server-side cursor, because 1 is interpreted as -1.

(mongo-collection-insert-docs! mc docs)  void
  mc : mongo-collection?
  docs : (sequenceof bson-document/c)
Inserts a sequence of documents into the collection.
(mongo-collection-insert-one! mc doc)  void
  mc : mongo-collection?
  doc : bson-document/c
Insert an document into the collection.
(mongo-collection-insert! mc doc ...)  void
  mc : mongo-collection?
  doc : bson-document/c
Inserts any number of documents into the collection.

(mongo-collection-remove! mc sel)  void
  mc : mongo-collection?
  sel : bson-document/c
Removes documents matching the selector. Refer to Removing for more details.

(mongo-collection-modify! mc sel mod)  void
  mc : mongo-collection?
  sel : bson-document/c
  mod : bson-document/c
Modifies all documents matching the selector according to mod. Refer to Modifier Operations for more details.

(mongo-collection-replace! mc sel doc)  void
  mc : mongo-collection?
  sel : bson-document/c
  doc : bson-document/c
Replaces the first document matching the selector with obj.

(mongo-collection-repsert! mc sel doc)  void
  mc : mongo-collection?
  sel : bson-document/c
  doc : bson-document/c
If a document matches the selector, it is replaced; otherwise the document is inserted. Refer to Upserts with Modifiers for more details on using modifiers.

(mongo-collection-count mc [query])  exact-integer?
  mc : mongo-collection?
  query : bson-document/c = empty
Returns the number of documents matching the query.

3.3.1 Indexing

Refer to Indexes for more details on indexing.

(mongo-collection-index! mc spec [name])  void
  mc : mongo-collection?
  spec : bson-document/c
  name : string? = ....
Creates an index of the collection. A name will be automatically generated if not specified.
(mongo-collection-indexes mc)  mongo-cursor?
  mc : mongo-collection?
Queries for index information.
(mongo-collection-drop-index! mc name)  void
  mc : mongo-collection?
  name : string?
Drops an index by name.

3.4 Cursors

Query results are returned as Mongo cursors.

A Mongo cursor is a sequence of BSON documents.

(mongo-cursor? x)  boolean?
  x : any/c
A test for Mongo cursors.
(mongo-cursor-done? mc)  boolean?
  mc : mongo-cursor?
Returns #t if the cursor has no more answers. #f otherwise.
(mongo-cursor-kill! mc)  void
  mc : mongo-cursor?
Frees the server resources for the cursor.