Skip to content

Instantly share code, notes, and snippets.

@s9tpepper
Created July 10, 2011 00:30
Show Gist options
  • Save s9tpepper/1074094 to your computer and use it in GitHub Desktop.
Save s9tpepper/1074094 to your computer and use it in GitHub Desktop.
Using the findOne() method
/* The findOne() method will take a query and return a single document. The
query is built using the Document class. If no second argument is used in
the find() one method all fields are returned for the document. */
var query:Document = new Document("fieldName:searchValue");
mongo.db("myDatabase").collection("myCollection").findOne(query).addOnce(findOneReply);
/* Returned fields can be limited by using the return fields document. */
var returnFields:Document = new Document("fieldName:1");
var query:Document = new Document("fieldName:searchValue");
mongo.db("myDatabase").collection("myCollection").findOne(query, returnFields)
.addOnce(findOneReply);
/* The callback handler must expect a FindOneResult object */
function findOneReply(findOneResult:FindOneResult):void
{
// The success property is true if a document is retrieved.
if (true == findOneResult.success)
{
// The document property will contain the document found.
var myDoc:Object = findOneResult.document;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment