On this page:
3.1 Servers
mongo?
create-mongo
close-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 db/mongodb/basic/main) package: mongodb

The basic API of MongoDB is provided by this module.

3.1 Servers

procedure

(mongo? x)  boolean?

  x : any/c
A test for Mongo servers.

procedure

(create-mongo [#:host host #:port port])  mongo?

  host : string = "localhost"
  port : port-number? = 27017
Creates a connection to the specified Mongo server.

procedure

(close-mongo! m)  void?

  m : mongo?
Closes the connection to the Mongo server.

procedure

(mongo-list-databases m)  (vectorof bson-document/c)

  m : mongo?
Returns information about the databases on a server.

procedure

(mongo-db-names m)  (listof string?)

  m : mongo?
Returns the names of the databases on the server.

3.2 Databases

struct

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

procedure

(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.

procedure

(mongo-db-collections db)  (listof string?)

  db : mongo-db?
Returns a list of collection names in the database.

procedure

(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.

procedure

(mongo-db-drop-collection! db name)  bson-document/c

  db : mongo-db?
  name : string?
Drops a collection from the database.

procedure

(mongo-db-drop db)  bson-document/c

  db : mongo-db?
Drops a database from its server.

Defined as (symbols 'none 'low 'all) .

procedure

(mongo-db-profiling db)  mongo-db-profiling/c

  db : mongo-db?
Returns the profiling level of the database.

procedure

(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.

procedure

(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.

procedure

(mongo-db-valid-collection? db name)  boolean?

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

3.3 Collections

struct

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

procedure

(mongo-collection-drop! mc)  void

  mc : mongo-collection?
Drops the collection from its database.

procedure

(mongo-collection-valid? mc)  boolean?

  mc : mongo-collection?
Returns #t if mc is a valid collection.
Returns the full name of the collection.

procedure

(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.

procedure

(mongo-collection-insert-docs! mc docs)  void

  mc : mongo-collection?
  docs : (sequenceof bson-document/c)
Inserts a sequence of documents into the collection.

procedure

(mongo-collection-insert-one! mc doc)  void

  mc : mongo-collection?
  doc : bson-document/c
Insert an document into the collection.

procedure

(mongo-collection-insert! mc doc ...)  void

  mc : mongo-collection?
  doc : bson-document/c
Inserts any number of documents into the collection.

procedure

(mongo-collection-remove! mc sel)  void

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

procedure

(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.

procedure

(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.

procedure

(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.

procedure

(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.

procedure

(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.
Queries for index information.

procedure

(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.

procedure

(mongo-cursor? x)  boolean?

  x : any/c
A test for Mongo cursors.

procedure

(mongo-cursor-done? mc)  boolean?

  mc : mongo-cursor?
Returns #t if the cursor has no more answers. #f otherwise.

procedure

(mongo-cursor-kill! mc)  void

  mc : mongo-cursor?
Frees the server resources for the cursor.