X DevAPI User Guide for MySQL Shell in JavaScript Mode

4.3.2 Collection.find()

The find(SearchConditionStr) function is for searching documents in a collection, similar to the SELECT statement for an SQL database. It takes a search condition string (SearchConditionStr) as a parameter to specify the documents that should be returned from the database. The execute() function triggers the actual execution of the find() operation.

The SearchConditionStr can be in one of these forms:

Several methods such as fields(), sort() , and limit() can be chained to the find() function to further refine the result. For example:

myColl.find("Name LIKE 'Austra%'").fields("Code")
myColl.find("geography.Continent LIKE 'A%'").limit(10)

Parameter binding using bind() is also supported. The following example illustrates the use of bind() with find():

// Use the collection 'my_collection'
var myColl = db.getCollection('my_collection');

// Find a single document that has a field 'name' that starts with 'L'
var docs = myColl.find('name like :param').
            limit(1).bind('param', 'L%').execute();

print(docs.fetchOne());

// Get all documents with a field 'name' that starts with 'L'
docs = myColl.find('name like :param').
        bind('param','L%').execute();

var myDoc;
while (myDoc = docs.fetchOne()) {
  print(myDoc);
}

See also CollectionFindFunction for the syntax of find() in EBNF.