Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Util Plugin, MBS MongoDB Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MongoDB/MongoDB Blog
This example is the version from Mon, 3rd Jul 2022.
Project "MongoDB Blog.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Function UnhandledException(error As RuntimeException) As Boolean
Dim name As String = Introspection.GetType(error).name
MessageBox name+": "+error.message
Return true
End EventHandler
End Class
Class MainWindow Inherits Window
Control List Inherits ListBox
ControlInstance List Inherits ListBox
EventHandler Sub Change()
If Me.SelectedRowIndex < 0 Then
Details.Text = ""
Else
Dim json As String = Me.RowTagAt(Me.SelectedRowIndex)
// format JSON with our JSON class
Dim j As New JSONMBS(json)
Details.Text = j.ToString
end if
End EventHandler
End Control
Control Details Inherits TextArea
ControlInstance Details Inherits TextArea
End Control
Control URI Inherits TextField
ControlInstance URI Inherits TextField
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control DatabaseName Inherits ComboBox
ControlInstance DatabaseName Inherits ComboBox
EventHandler Sub Change()
OpenDatabase me.Text
End EventHandler
EventHandler Sub TextChanged()
OpenDatabase Me.Text
End EventHandler
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control CollectionName Inherits ComboBox
ControlInstance CollectionName Inherits ComboBox
EventHandler Sub Change()
OpenCollection Me.Text
End EventHandler
EventHandler Sub TextChanged()
OpenCollection Me.Text
End EventHandler
End Control
Control ConnectButton Inherits PushButton
ControlInstance ConnectButton Inherits PushButton
EventHandler Sub Action()
Dim u As New MongoURIMBS(URI.Text)
client = new MongoClientMBS(u)
if Client <> nil then
ListDatabaseButton.Enabled = true
dim DatabaseNames() as string = client.DatabasesNames
DatabaseName.RemoveAllRows
For Each d As String In DatabaseNames
DatabaseName.AddRow d
next
DatabaseName.Enabled = true
else
ListDatabaseButton.Enabled = false
end if
End EventHandler
End Control
Control ListDatabaseButton Inherits PushButton
ControlInstance ListDatabaseButton Inherits PushButton
EventHandler Sub Action()
cursor = Client.FindDatabases
FillList
End EventHandler
End Control
Control ListCollectionButton Inherits PushButton
ControlInstance ListCollectionButton Inherits PushButton
EventHandler Sub Action()
cursor = Database.FindCollections
FillList
End EventHandler
End Control
Control FindRecords Inherits PushButton
ControlInstance FindRecords Inherits PushButton
EventHandler Sub Action()
dim Filter as new JSONItem
'filter.Value("Hello") = "World"
dim sortOrder as new JSONItem
'sortOrder.Value("hello") = -1 // descending
dim options as new JSONItem
options.Value("limit") = 20
options.Value("sort") = sortOrder
cursor = Collection.Find(filter.toString, options.toString)
FillList
End EventHandler
End Control
Control InsertRecord Inherits PushButton
ControlInstance InsertRecord Inherits PushButton
EventHandler Sub Action()
Dim NewRecord As New JSONItem
'NewRecord.Value("firstName") = "John"
'NewRecord.Value("lastName") = "Miller"
'NewRecord.Value("city") = "New York"
NewRecord.Value("firstName") = "Bob"
NewRecord.Value("lastName") = "Jones"
'NewRecord.Value("city") = "Los Angeles"
NewRecord.Value("phone") = "555-1234-567"
Dim Result As String = Collection.InsertOne(NewRecord.toString)
List.RemoveAllRows
details.Text = result
End EventHandler
End Control
Control KeysRecord Inherits PushButton
ControlInstance KeysRecord Inherits PushButton
EventHandler Sub Action()
Dim Keys() As String = Collection.Keys
List.RemoveAllRows
details.Text = Join(keys, EndOfLine)
End EventHandler
End Control
Sub CommandTest()
// get keys of a collection into a new collection
Dim j As New JSONItem
j.Value("mapReduce") = "test"
j.Value("map") = "function () { for (var key in this) { emit(key, null); } }"
j.Value("reduce") = "function(key, values) { return null; }"
j.Value("out") = "test_keys"
Dim command As String = j.tostring
Dim Result As String = Database.Command(command)
List.RemoveAllRows
details.Text = result
End Sub
Sub FillList()
List.RemoveAllRows
Details.text = ""
if cursor <> nil then
dim Record as string
while cursor.NextRecord(record)
dim preview as string = record.ReplaceLineEndings(" ")
preview = preview.ReplaceAll(encodings.UTF8.Chr(9), " ")
preview = preview.ReplaceAll(" ", " ")
preview = preview.ReplaceAll(" ", " ")
preview = preview.ReplaceAll(" : ", ": ")
preview = preview.Left(200)
list.AddRow preview
List.RowTagAt(List.LastAddedRowIndex) = record
wend
end if
cursor = nil
End Sub
Private Sub OpenCollection(name as string)
System.DebugLog "Open collection: "+name
If name.Len > 0 Then
// now we open the collection, which holds the data
Collection = Database.Collection(name)
If Collection <> Nil Then
FindRecords.Enabled = True
InsertRecord.Enabled = True
KeysRecord.Enabled = True
Return
End If
end if
FindRecords.Enabled = False
InsertRecord.Enabled = False
KeysRecord.Enabled = False
End Sub
Private Sub OpenDatabase(Name as string)
System.DebugLog "Open database: "+name
If name.Len > 0 Then
Database = Client.Database(name)
If Database <> Nil Then
ListCollectionButton.Enabled = True
// list names of collections in our combo box
Dim CollectionNames() As String = Database.CollectionNames
CollectionName.RemoveAllRows
CollectionName.Text = ""
For Each d As String In CollectionNames
CollectionName.AddRow d
Next
CollectionName.Enabled = True
return
End If
End If
ListCollectionButton.Enabled = False
CollectionName.Enabled = False
End Sub
Property Client As MongoClientMBS
Property Collection As MongoCollectionMBS
Property Database As MongoDatabaseMBS
Property cursor As MongoCursorMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem HelpMenu = "&Help"
End MenuBar
End Project
See also:
The items on this page are in the following plugins: MBS MongoDB Plugin.