Components | All | New | MacOS | Windows | Linux | iOS | ||||
Examples | Mac & Win | Server | Client | Guides | Statistic | FMM | Blog | Deprecated | Old |
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 |
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:
Set Variable [ $r ; Value: MBS( "MongoDB.Find"; $Mongo; "{ \"_id\": {\"$oid\": \"652c363c7b2449618e015502\"}}") ]
Find using >= 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
- IsError
- MongoDB.Connect
- MongoDB.CursorNext
- MongoDB.FindCollections
- MongoDB.FindIndexes
- MongoDB.New
- MongoDB.Ping
- MongoDB.Release
- MongoDB.SetURI
- MongoDB.Time
Release notes
- Version 12.4
- Added MongoDB.FindIndexes and MongoDB.Rename functions.
Example Databases
Blog Entries
This function checks for a license.
Created 22nd May 2022, last changed 27th July 2024