B4J Library [BANanoMongoDB] A helper class for everything MongoDB PHP CRUD.

Hi there

Finally just managed to get this working. What a journey...

If you need MongoDB PHP CRUD, I got you. The structure of the code is also simple, you just initialize the class and then refer to the examples for an easy peasy experience.

Here is a demo of what we have been able to do using our UI framework of choice. You can use this with Skeleton and any other framework.

YT Demo:

Demo Source Code

 

Attachments

  • BANanoMongoDB.bas
    15 KB · Views: 154
Last edited:

Mashiane

Expert
Licensed User
Longtime User
The C in CRUD

B4X:
Dim mongo As BANanoMongoDB
    mongo.Initialize("localhost", 27017, "bookslibrary", "books")
    '
    Dim rec As Map = CreateMap()
    rec.Put("firstname", "Anele")
    rec.Put("lastname", "Mbanga")
    rec.Put("age", 49)
    rec.Put("title", "Software Engineer")
    banano.Await(mongo.InsertWait(rec))
    If mongo.RowCount = 1 Then
        vuetify.ShowSnackBarSuccess("Person added successfully.")
    Else
        vuetify.ShowSnackBarError("Person could not be added!")
    End If
 

Mashiane

Expert
Licensed User
Longtime User
The R in CRUD

Here we move to the last record of the table, get its id and then read it. If you know the _id of each record, you are good to go.

B4X:
Dim mongo As BANanoMongoDB
    mongo.Initialize("localhost", 27017, "bookslibrary", "books")
    'we will read the last record
    banano.Await(mongo.SelectWait)
    'lets move to the last record
    mongo.MoveLast
    'lets get the id of the last record
    Dim id As String = mongo.GetString("_id")
    Log("To Read")
    Log(id)
    Dim user As Map = banano.Await(mongo.ReadWait(id))
    Log(user)
 

Mashiane

Expert
Licensed User
Longtime User
The U in CRUD

Also here, we move to the last record in our table, read its _id and then use that to update the record.

B4X:
Dim mongo As BANanoMongoDB
    mongo.Initialize("localhost", 27017, "bookslibrary", "books")
    'we will update the last record
    banano.Await(mongo.SelectWait)
    'lets move to the last record
    mongo.MoveLast
    'lets get the id of the last record
    Dim id As String = mongo.GetString("_id")
    Log("To Update")
    Log(id)
    '
    Dim rec As Map = CreateMap()
    rec.Put("_id", id)
    rec.Put("lastname", "Mbangalicisous")
    rec.Put("age", 50)
    banano.Await(mongo.UpdateWait(rec))
    If mongo.RowCount = 1 Then
        vuetify.ShowSnackBarSuccess("Person updated successfully.")
    Else
        vuetify.ShowSnackBarError("Person could not be updated!")
    End If
 

Mashiane

Expert
Licensed User
Longtime User
The D in CRUD

Also here, we move to the last record of our table (just to demonstrate), read the record _id and then use that to delete the record.

B4X:
Dim mongo As BANanoMongoDB
    mongo.Initialize("localhost", 27017, "bookslibrary", "books")
    'we will delete the last record
    banano.Await(mongo.SelectWait)
    'lets move to the last record
    mongo.MoveLast
    'lets get the id of the last record
    Dim id As String = mongo.GetString("_id")
    Log("To Delete")
    Log(id)
    'lets delete the record
    banano.Await(mongo.DeleteWait(id))
    If mongo.RowCount = 1 Then
        vuetify.ShowSnackBarSuccess("Person deleted successfully.")
    Else
        vuetify.ShowSnackBarError("Person could not be deleted!")
    End If
 

Mashiane

Expert
Licensed User
Longtime User
SELECTING WHERE ? RECORDS FROM THE TABLE

B4X:
Dim mongo As BANanoMongoDB
    mongo.Initialize("localhost", 27017, "bookslibrary", "books")
    mongo.CLEAR_WHERE
    'mongo.ADD_FIELD("firstname", True)
    'mongo.ADD_FIELD("lastname", True)
    'mongo.ADD_FIELD("age", True)
    'mongo.ADD_FIELD("title", False)
    mongo.ADD_WHERE("age", mongo.EQ_OP, 50)
    '
    banano.Await(mongo.SelectWait)
    For Each row As Map In mongo.result
        Log(row)
    Next
    vuetify.ShowSnackBarInfo(mongo.RowCount & " record(s) found!")

#Sharing the goodness

#Enjoy
 

Mashiane

Expert
Licensed User
Longtime User
UPDATE WHERE

B4X:
Dim mongo As BANanoMongoDB
    mongo.Initialize("localhost", 27017, "bookslibrary", "books")
    mongo.CLEAR_WHERE
    mongo.ADD_WHERE("firstname", mongo.EQ_OP, "Anele")        'find all records with firstname > Anele
    Dim rec As Map = CreateMap()
    rec.Put("firstname", "Xolani")            'update the firstname and age
    rec.Put("age", 51)
    banano.Await(mongo.UpdateWhereWait(rec))
    vuetify.ShowSnackBarSuccess($"${mongo.RowCount} record(s) updated!"$)
 

Mashiane

Expert
Licensed User
Longtime User
DELETE WHERE

B4X:
Dim mongo As BANanoMongoDB
    mongo.Initialize("localhost", 27017, "bookslibrary", "books")
    mongo.CLEAR_WHERE
    'delete where age = 50
    mongo.ADD_WHERE("age", mongo.EQ_OP, 50)        'delete all records where age = 50
    banano.Await(mongo.DeleteWhereWait)
    vuetify.ShowSnackBarSuccess($"${mongo.RowCount} record(s) deleted!"$)
 
Top