MongoCollectionfind()

Performs a query against Mongo returning back an array of structures of the documents matching the query

Usage

ARRAY = MongoCollectionfind( datasource, collection, query, fields, skip, size, sort )
Argument Summary
datasource datasource name. Name previously created using MongoRegister
collection collection name
query the query to which to find the object to update
fields fields to which to bring back. If left blank or not specified, then all fields for the document will be returned [optional]
skip records to skip before starting [optional]
size number of records to return [optional]
sort sort object [optional]

Calling

Supports named-parameter calling allowing you to use the function like:

MongoCollectionfind(
   datasource=?, 
   collection=?, 
   query=?, 
   fields=?, 
   skip=?, 
   size=?, 
   sort=?
);

Supports passing parameters as a structure using ArgumentCollection:

MongoCollectionfind( ArgumentCollection={
   datasource : ?, 
   collection : ?, 
   query : ?, 
   fields : ?, 
   skip : ?, 
   size : ?, 
   sort : ?
} );

Extra

Example


MongoDB's query syntax is quite different then the SQL you might got used too. A good overview how to find (select) records with MongoDB can be found at SQL to Mongo Mapping Chart. In short, the MongoDB find() syntax is close to the "chained" commands you might know from the likes of JQuery. Important to remember is that 99% of the time everything you pass to MongoDB is a JSON structure and has a JSON like syntax.

Since OpenBD is using the Java Driver and we apply a CFML tag for it, the query syntax differs slightly from the one you would enter in a mongo shell. Please find below some examples.

Find all records

<cfset myarray = MongoCollectionfind(
   datasource="mongosource", 
   collection="users", 
   query={}
)>

The above MongoCollectionfind() will return an Array with all records of the Collection and all fields.

Return only certain fields

It's a common practice not to return all fields as in a "SELECT column1, column2 FROM���". With MongoCollectionfind() you would do something like:

<cfset myarray = MongoCollectionfind(
   datasource="mongosource", 
   collection="users", 
   query={},
   fields={email:true,firstname:true}
)>

Again the MongoCollectionfind() would return an Array with all records, but this time only with the columns "email" and "firstname". Note: The unique value of "_id" is always returned!

Find a specific user

Of course, returning all records is 99% of the time not wanted. MongoDB has a very sophisticated approach to finding records. As you can see from the SQL to MongoDB map. Ok, now let's find user by his eMail address

<cfset myarray = MongoCollectionfind(
   datasource="mongosource", 
   collection="users", 
   query=({email:"nitai@openbd.com"}),
   fields={email:true,firstname:true}
)>

To find the user with his eMail address AND his first name you would do:

<cfset myarray = MongoCollectionfind(
   datasource="mongosource", 
   collection="users", 
   query=({email:"nitai@openbd.com",firstname:"nitai"}),
   fields={email:true,firstname:true}
)>

The same find() as above but this time with OR would be:

<cfset myarray = MongoCollectionfind(
   datasource="mongosource", 
   collection="users", 
   query=( { $or : [ { email : "nitai@openbd.com" } , { first_name : "nitai" } ] } ),
   fields={email:true,firstname:true}
)>

Extra

For more information on using MongoDB, visit the MongoCFML page

For functions that update data, you can pass in the writeconcern. The possible values are:

  • FSYNC_SAFE
    Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk
  • JOURNAL_SAFE
    Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk
  • MAJORITY
    Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation
  • NONE
    No exceptions are raised, even for network issues
  • NORMAL
    Exceptions are raised for network issues, but not server errors
  • REPLICAS_SAFE
    Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation
  • SAFE
    Exceptions are raised for network issues, and server errors; waits on a server for the write operation