Topics   All   MacOS (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server   Client   Old   Deprecated   Guides   Examples   Videos
New in version: 12.1   12.2   12.3   12.4   12.5   13.0   13.1   13.2   13.3   13.4    Statistic    FMM    Blog  

MongoDB.Find

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

Component Version macOS Windows Linux Server iOS SDK
MongoDB 12.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "MongoDB.Find"; MongoDBRef; FilterJSON { ; OptionsJSON } )   More

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" ) ]
Set Variable [ $r ; Value: MBS( "MongoDB.SetURI"; $Mongo; "mongodb://localhost/" ) ]
Set Variable [ $r ; Value: MBS( "MongoDB.Connect"; $Mongo) ]
Set Variable [ $r ; Value: MBS( "MongoDB.OpenDatabase"; $Mongo; "local" ) ]
Set Variable [ $r ; Value: MBS( "MongoDB.OpenCollection"; $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"; $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"; $Mongo ) ]

Find records where OtherField > 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"; $Mongo) ]
    If [ Length ( $json ) > 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:

MBS("MongoDB.Find"; $$MongoDB; "{customer: { \"$oid\" : \"" & $userID & "\"}")

Find using >= date:

MBS( "MongoDB.Find"; $Mongo; "{\"updated_at\": {\"$gte\": {\"$date\": \"2023-08-14T22:00:00Z\" }}}")

See also

Release notes

Example Databases

Blog Entries

This function checks for a license.

Created 22nd May 2022, last changed 15th August 2023


MongoDB.EstimatedDocumentCount - MongoDB.FindCollections

💬 Ask a question or report a problem