MongoDB.Find
------------

Query on collection, passing arbitrary query options to the server in options.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [MongoDB](component_MongoDB.md) | [12.3](newinversion123.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "MongoDB.Find"; MongoDBRef; FilterJSON { ; OptionsJSON } ) 

**MBS**( **"MongoDB.Find";** /\* Query on collection, passing arbitrary query options to the server in options. \*/  
**$MongoDBRef**; /\* The reference number for the mongo connection.e.g. $MongoDB \*/   
**$FilterJSON**; /\* The JSON with filter details. \*/   
**$OptionsJSON**) /\* Optional; The JSON with options. \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| MongoDBRef | The reference number for the mongo connection. | $MongoDB |  |
| FilterJSON | The JSON with filter details. |  |  |
| OptionsJSON | The JSON with options. |  | Optional |

### Result

Returns OK or error.

### Description

Query on collection, passing arbitrary query options to the server in options.  
Sets the current cursor.  
  
See also find command documented here:  
<https://www.mongodb.com/docs/manual/reference/command/find/>  
### Examples

Find records:

 Set Variable \[ $Mongo ; Value: MBS ( "[MongoDB.New](MongoDBNew.md)" ) \]   
Set Variable \[ $r ; Value: MBS ( "[MongoDB.SetURI](MongoDBSetURI.md)"; $Mongo ; "mongodb://localhost/" ) \]   
Set Variable \[ $r ; Value: MBS ( "[MongoDB.Connect](MongoDBConnect.md)"; $Mongo ) \]   
Set Variable \[ $r ; Value: MBS ( "[MongoDB.OpenDatabase](MongoDBOpenDatabase.md)"; $Mongo ; "local" ) \]   
Set Variable \[ $r ; Value: MBS ( "[MongoDB.OpenCollection](MongoDBOpenCollection.md)"; $Mongo ; "test" ) \]   
\# get cursor for records describing collections   
Set Variable \[ $r ; Value: MBS ( "MongoDB.Find"; $Mongo ; "{\\"Hello\\": \\"World\\"}"; "{\\"limit\\": 10, \\"sort\\":{\\"Hello\\": -1}}") \]   
If \[ MBS ("ISError") \]   
 Show Custom Dialog \[ "Failed to find" ; $r \]   
Else  
 # loop over records and show each  
 Loop  
 Set Variable \[ $json ; Value: MBS ( "[MongoDB.CursorNext](MongoDBCursorNext.md)"; $Mongo ) \]   
 Exit Loop If \[ MBS ("IsError") // some error like connection dropped \]   
 Exit Loop If \[ Length ( $json ) = 0 // end of cursor \]   
 Show Custom Dialog \[ "Record" ; $json \]   
 End Loop  
End If  
Set Variable \[ $r ; Value: MBS ( "[MongoDB.Release](MongoDBRelease.md)"; $Mongo ) \]  
Find records where OtherField &gt; 123:

 MBS ( "MongoDB.Find"; $Mongo ;   
"{\\"OtherField\\": { \\"$gt \\" : 123}}")  
Load image from binary data in a record:

 Set Variable \[ $r ; Value: MBS ( "MongoDB.Find"; $Mongo ; "{\\"FileID\\": 123}") \]   
If \[ MBS ("ISError") \]   
 Show Custom Dialog \[ "Failed to find" ; $r \]   
Else  
 # JSON like this: {"Data":{"$binary ":{"base64":"iVBORw0K...ElFTkSuQmCC\\r\\n","subType":0}},"FileID":123,"filename":"test.png"}  
 Set Variable \[ $json ; Value: MBS ( "[MongoDB.CursorNext](MongoDBCursorNext.md)"; $Mongo ) \]   
 If \[ Length ( $json ) &gt; 0 \]   
 Set Field \[ test::Insert JSON ; $json \]   
 Set Field \[ test::Image ; Base64Decode ( JSONGetElement ( test::Insert JSON ; "Data.$binary .base64" ); JSONGetElement ( test::Insert JSON ; "filename" ) ) \]   
 End If  
End If  
Find records and only return FirstName and LastName:

 Set Variable \[ $r ; Value: MBS ( "MongoDB.Find"; $Mongo ;   
"{\\"GroupId\\": 123}"; "{\\"projection\\": { \\"FirstName\\" : 1, \\"LastName\\" : 1}}") \]  
Find user by objectId:

 Set Variable \[ $r ; Value: MBS ( "MongoDB.Find"; $Mongo ; "{ \\"\_id\\": {\\"$oid \\": \\"652c363c7b2449618e015502\\"}}") \]  
Find using &gt;= date:

 MBS ( "MongoDB.Find"; $Mongo ; "{\\"updated\_at\\": {\\"$gte \\": {\\"$date \\": \\"2023-08-14T22:00:00Z\\" }}}")  
Find index=123:

 Set Variable \[ $r ; Value: MBS ( "MongoDB.Find"; $Mongo ; "{\\"index\\": { \\"$eq \\" : 123}}") \]  
### See also

- [MongoDB.Connect](MongoDBConnect.md)
- [MongoDB.CursorNext](MongoDBCursorNext.md)
- [MongoDB.FindCollections](MongoDBFindCollections.md)
- [MongoDB.FindDatabases](MongoDBFindDatabases.md)
- [MongoDB.FindIndexes](MongoDBFindIndexes.md)
- [MongoDB.GridFS.GetChunks](MongoDBGridFSGetChunks.md)
- [MongoDB.GridFS.GetFiles](MongoDBGridFSGetFiles.md)
- [MongoDB.OpenDatabase](MongoDBOpenDatabase.md)
- [MongoDB.Release](MongoDBRelease.md)
- [MongoDB.SetURI](MongoDBSetURI.md)

### Release notes

- **Version 12.4**
    - Added [MongoDB.FindIndexes](https://www.mbsplugins.eu/MongoDBFindIndexes.shtml) and [MongoDB.Rename](https://www.mbsplugins.eu/MongoDBRename.shtml) functions.

### Example Databases

- [MongoDB/MongoDB Blog](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/MongoDB/MongoDB%20Blog.shtml#22ScriptAnchor_)
- [MongoDB/MongoDB Test](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/MongoDB/MongoDB%20Test.shtml#4ScriptAnchor_)

### Blog Entries

- [MongoDB GridFS in the MBS FileMaker Plugin](https://www.mbsplugins.de/archive/2026-06-17/MongoDB_GridFS_in_the_MBS_File/monkeybreadsoftware_blog_filemaker)
- [Store containers in a MongoDB](https://www.mbsplugins.de/archive/2022-09-04/Store_containers_in_a_MongoDB/monkeybreadsoftware_blog_filemaker)
- [New MongoDB Example](https://www.mbsplugins.de/archive/2022-07-25/New_MongoDB_Example/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 1/2025, Seite 24 bis 25](https://filemaker-magazin.de/neuigkeit/4282-Appetithappen-FMM_202501)

This function checks for a license.

Created 22nd May 2022 , last changed 27th July 2024

  
[MongoDB.EstimatedDocumentCount](MongoDBEstimatedDocumentCount.md) - [MongoDB.FindCollections](MongoDBFindCollections.md)

[HTML Version](MongoDBFind.shtml)