- Stitch >
- MongoDB Atlas >
- Reference >
- MongoDB Actions
collection.find()¶
On this page
Definition¶
-
collection.
find
()¶
Find all documents in a collection or view that match the provided query predicates and return a handle object that allows you to access them.
Usage¶
Example¶
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
To call the collection.find()
action from a Function, get a collection handle with
database.collection()
then call the handle’s
find()
method.
To call the collection.find()
action from a JavaScript SDK,
use the RemoteMongoCollection.find()
method.
To call the collection.find()
action from the Java/Android
SDK, use the RemoteMongoCollection.find() method.
To call the collection.find()
action from the Swift/iOS
SDK, use the RemoteMongoCollection.find() method.
Parameters¶
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
The collection.find()
action has the following form:
Parameter | Description |
---|---|
Query Filter query: <document> |
Optional. A standard MongoDB query document that specifies which documents to find. You can use most query selectors except for evaluation, geospatial, or bitwise selectors. Specify an empty query filter ( |
Projection projection: <document> |
Optional. A document that specifies which fields MongoDB should return or withhold in each document that matches the query. To return all fields in the matching documents, omit this
parameter or specify an empty projection document ( To return specific fields and the document’s To withhold specific fields, specify the fields in the projection
document with a value of Note You may specify either fields to include or fields to withhold
but not both. For example, the following projection is
invalid because it simultaneously includes the The exception to this rule is the |
The find() method has the following form:
Parameter | Description |
---|---|
Query Filter query: <document> |
Optional. A standard MongoDB query document that specifies which documents to find. You can use most query selectors except for evaluation, geospatial, or bitwise selectors. Specify an empty query filter ( |
Query Options options: <document> |
“” A document that specifies configuration options for the
query. The A RemoteFindOptions object, which has the following properties: |
Projection options.projection: <document> |
Optional. A document that specifies which fields MongoDB should return or withhold in each document that matches the query. To return all fields in the matching documents, omit this
parameter or specify an empty projection document ( To return specific fields and the document’s To withhold specific fields, specify the fields in the projection
document with a value of Note You may specify either fields to include or fields to withhold
but not both. For example, the following projection is
invalid because it simultaneously includes the The exception to this rule is the |
Sort options.sort: <document> |
Optional. A document that specifies a Example The following sort document specifies that documents should be
sorted first by |
Limit options.limit: <integer> |
Optional. The maximum number of documents to include in the
query result set. If the result set contains more documents
than the specified limit , the query will return documents
in order up to the limit. |
The find() method has the following form:
Parameter | Description |
---|---|
Query Filter filter: <document> |
Optional. A standard MongoDB query document that specifies which documents to find. You can use most query selectors except for evaluation, geospatial, or bitwise selectors. Specify an empty query filter ( |
Result Class resultClass: <class> |
Optional. Indicates the type of the document returned by the operation. |
The find() method has the following form:
Parameter | Description |
---|---|
Query Filter query: <document> |
Optional. A standard MongoDB query document that specifies which documents to find. You can use most query selectors except for evaluation, geospatial, or bitwise selectors. Specify an empty query filter ( |
Query Options options: <document> |
“” A document that specifies configuration options for the
query. The A RemoteFindOptions object, which has the following properties: |
Projection options.projection: <document> |
Optional. A document that specifies which fields MongoDB should return or withhold in each document that matches the query. To return all fields in the matching documents, omit this
parameter or specify an empty projection document ( To return specific fields and the document’s To withhold specific fields, specify the fields in the projection
document with a value of Note You may specify either fields to include or fields to withhold
but not both. For example, the following projection is
invalid because it simultaneously includes the The exception to this rule is the |
Sort options.sort: <document> |
Optional. A document that specifies a Example The following sort document specifies that documents should be
sorted first by |
Limit options.limit: <integer> |
Optional. The maximum number of documents to include in the
query result set. If the result set contains more documents
than the specified limit , the query will return documents
in order up to the limit. |
Return Value¶
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
The collection.find()
action returns a cursor object that
points to any documents that match the specified query filters.
You can manipulate and access documents in the query result set
with the following cursor methods:
Method | Description |
---|---|
cursor.sort(sort) |
Sorts documents in the result set according to the Note You cannot call this method after retrieving one or more
documents using Example The following sort document specifies that documents should be
sorted first by |
cursor.limit(limit) |
Specifies the maximum number of documents to include in the
query result set. If the result set contains more documents than
the specified Note You cannot call this method after retrieving one or more
documents using |
cursor.next() |
Iterates the cursor and returns a Promise that
resolves to the next document in the cursor. If the cursor is
exhausted, the promise resolves to Example |
cursor.toArray() |
Iterates the cursor to exhaustion and returns a Promise that resolves to an array that contains all of the iterated documents. Example |
Note
You cannot return a cursor from a Function. Instead, evaluate the cursor using
cursor.next()
or cursor.toArray()
and return the
result.
The RemoteMongoCollection.find() method returns a RemoteMongoReadOperation object.
You can access documents in the query result set by calling one
of the following methods on the RemoteMongoReadOperation
object:
Method | Description |
---|---|
First Document result.first() |
“” Return a Promise that resolves to the first document from the query result set. Return a StitchResult enum to the completion handler that resolves to the first document from the query result set. See RemoteMongoReadOperation.first(). |
Array of Documents result.toArray() |
“” Return a Promise that resolves to an array that contains all documents in the query result set. Return a StitchResult enum to the completion handler that resolves to an array that contains all documents in the query result set. See RemoteMongoReadOperation.toArray(). |
Query Cursor result.iterator() |
“” Return a Promise that resolves to a RemoteMongoCursor object. You can use this cursor to iterate through each document in the query result set with the RemoteMongoCursor.next() method. Return a StitchResult enum to the completion handler that resolves to a RemoteMongoCursor object. You can use this cursor to iterate through each document in the query result set with the RemoteMongoCursor.next() method. See RemoteMongoReadOperation.iterator(). |
The RemoteMongoCollection.find() method returns a RemoteFindIterable object.
You can access documents in the query result set by calling one
of the following methods on the RemoteFindIterable
object:
Method | Description |
---|---|
Projection result.projection(<document>) |
Specifies which fields MongoDB should return or withhold in each document that matches the query. To return all fields in the matching documents, omit this
parameter or specify an empty projection document ( To return specific fields and the document’s To withhold specific fields, specify the fields in the
projection document with a value of Note You may specify either fields to include or fields to
withhold but not both. For example, the following projection
is invalid because it simultaneously includes the
The exception to this rule is the |
Limit result.limit(<integer>) |
Limits the query result set to include no more than the
specified number of documents. If the result set contains more
documents than the specified |
Sort result.sort(<document>) |
Sorts documents in the result set according to the Note You cannot call this method after retrieving one or more
documents using Example The following sort document specifies that documents should be
sorted first by |
First Document result.first() |
Return a Task that resolves to the first document from the query result set. |
Array of Documents result.into(<target>) |
Iterate all documents in the query result set into the target object. Return a Task that resolves to the populated target object. |
For Each result.forEach(<Block>) |
Iterate over the result set and apply the given block to each document. Does not return a value. |
Map Document Type result.map(<Function>) |
Return a new iterator that maps each |
Query Cursor result.iterator() |
Return a Task that resolves to a RemoteMongoCursor object. You can use this cursor to iterate through each document in the query result set with the RemoteMongoCursor.tryNext() method. |
The RemoteMongoCollection.find() method returns a StitchResult enum to its completion handler:
If the find operation was successful, the StitchResult
resolves to a RemoteMongoReadOperation object that allows you to
access documents in the query result set by calling one of the
following methods:
Method | Description |
---|---|
First Document result.first() |
“” Return a Promise that resolves to the first document from the query result set. Return a StitchResult enum to the completion handler that resolves to the first document from the query result set. See RemoteMongoReadOperation.first(). |
Array of Documents result.toArray() |
“” Return a Promise that resolves to an array that contains all documents in the query result set. Return a StitchResult enum to the completion handler that resolves to an array that contains all documents in the query result set. See RemoteMongoReadOperation.toArray(). |
Query Cursor result.iterator() |
“” Return a Promise that resolves to a RemoteMongoCursor object. You can use this cursor to iterate through each document in the query result set with the RemoteMongoCursor.next() method. Return a StitchResult enum to the completion handler that resolves to a RemoteMongoCursor object. You can use this cursor to iterate through each document in the query result set with the RemoteMongoCursor.next() method. See RemoteMongoReadOperation.iterator(). |