B4J Library [SithasoMongoDB] A jMongo class helper for everything MongoDB CRUD

Hi there

This helper class is to help anyone explore the jMongo library for CRUD for the less adventurous. Demo attached.

SithasoMongoDB
  • Initialize (sHost As String, iPort As Int, dbName As String, tblName As String) As SithasoMongoDB
    '
    B4X:
    'initialize the class
    Dim dbMongo As SithasoMongoDB
    dbMongo.Initialize("127.0.0.1", 27017, "test", "users")
  • PrepareRecord As SithasoMongoDB
    prepare the record for insert/update
  • SetField (fld As String, value As Object) As SithasoMongoDB
    set a field on the record
  • SetLimit (l As Int) As SithasoMongoDB
    set limit
  • SetSkip (l As Int) As SithasoMongoDB
    set skip
  • MoveFirst
    movefirst
  • MoveLast
    movelast
  • MovePrevious
    moveprevious
  • MoveNext
    movenext
  • ClearWhere As SithasoMongoDB
    clear where clause
  • AddFields (flds As List) As SithasoMongoDB
    add projection
  • AddSort (fld As String, desc As Boolean) As SithasoMongoDB
    add a sort order
  • AddWhere (fld As String, operator As String, value As Object) As SithasoMongoDB
    add a where clause for your select where statement
    _id will be ignored
  • Update As Int
    B4X:
    'update a record, returns number of records updated
    'you can specify a where clause optionally
    'dbConnect.ClearWhere
    'dbConnect.AddWhere("name", dbConnect.EQ_OP, "Anele")
    dbConnect.PrepareRecord
    dbConnect.SetField("age", 50)
    dbConnect.SetField("work", "Software Engineer")
    dbConnect.Update
    if dbConnect.RowCount >= 1 then
    'the records were updated
    end if
  • Delete (id As String) As Int
    B4X:
    'delete record and returns number of records deleted
    dbMongo.Delete("1234567")
    if dbMongo.RowCount = 1 then
    'the record was deleted
    end if
  • DeleteWhere As Int
    B4X:
    'delete record by id or where clause returns number of records deleted
    'dbConnect.ClearWhere
    'dbConnect.AddWhere("name", dbConnect.EQ_OP, "Anele")
    dbMongo.DeleteWhere
    if dbMongo.RowCount = 1 then
    'the record was deleted
    end if
  • DeleteAll As Int
    B4X:
    'delete all records
    dbMongo.DeleteAll
    if dbMongo.RowCount > 0 then
    'the records were deleted
    end if
  • Read (id As String) As Map
    B4X:
    'read record by id, get result
    dbMongo.Read("1234567")
    if dbMongo.RowCount = 1 then
    'the record was read
    'dbConnect.MoveFirst
    dim sname As String = dbMongo.GetString("name")
    log(sname)
    end if
  • Insert
    B4X:
    'insert record, returns the number of records inserted
    '_id will be ignore
    dbMongo.PrepareRecord
    dbMongo.SetField("name", "Anele")
    dbMongo.SetField("email", "[EMAIL][email protected][/EMAIL]")
    dbConnect.Insert
  • SelectAll() As List
    B4X:
    'select where records and show specific fields
    dbConnect.ClearWhere
    dbConnect.AddFields(array("name", "lastname", "_id"))
    dbConnect.AddWhere("name", dbConnect.EQ_OP, "Anele")
    dbConnect.SetSkip(1)
    dbConnect.SetLimit(1)
    dbConnect.SelectAll
    if dbConnect.RowCount >= 1 then
    'the records were selected
    end if
  • setPosition (pos As Int)
    set the position of the current record
  • GetInt (fld As String) As Int
    get an integer from the current record
  • GetLong (fld As String) As Long
    get a long from the current record
  • GetID As String
    get the id of this record
  • GetString (fld As String) As String
    get a string from the current record
  • GetBoolean (fld As String) As Boolean
    get a boolean from the current record
  • GetDouble (fld As String) As Double
    get a double from the current record
Related Content
BANanoMongoDB - A helper class for MongoDB (PHP) for BANano.
 

Attachments

  • SithasoMongoDBDemo.zip
    4.5 KB · Views: 165
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Let me explain.

The C in CRUD

Lets initialize the helper class and add some records. We create a family database and a members table.

B4X:
Dim smongo As SithasoMongoDB
    'create a database named family with a table named members
    smongo.Initialize("127.0.0.1", 27017, "family", "members")
    'create the record to add
    Log("INSERT RECORD...")
    smongo.PrepareRecord
    smongo.SetField("firstname", "Anele")
    smongo.SetField("lastname", "Mbanga")
    smongo.SetField("gender", "Male")
    smongo.Insert
 

Mashiane

Expert
Licensed User
Longtime User
The R in CRUD

In this example, (well we dont have an id we know of, so we will select the records, move to the last record added, get its _id and then read that.

Getting the last record added
B4X:
'* get the last record
    smongo.ClearWhere
    smongo.SelectAll
    smongo.MoveLast
    Dim id As String = smongo.GetID
    Log(id)

Now lets read that record using that ID.

B4X:
'read a record using id
    smongo.Read(id)
    Log(smongo.RowCount & " record(s) read!")
    If smongo.RowCount = 1 Then
        smongo.MoveFirst
        Dim sname As String = smongo.GetString("firstname")
        Log(sname)
        Log(smongo.Record)
    End If
 

Mashiane

Expert
Licensed User
Longtime User
The U in CRUD

In this example, we use the last read id we got in the previous post. We then update the lastname and the age of the person.

B4X:
'update an existing record by _id
    smongo.PrepareRecord
    smongo.SetField("_id", id)
    smongo.SetField("lastname", "Nguta")
    smongo.SetField("age", 52)
    smongo.Update
    Log(smongo.RowCount & " record(s) updated!")

We can also execute something like "UPDATE WHERE..." like this

B4X:
Log("UPDATE RECORD...")
    smongo.ClearWhere
    'update where firstname = anele
    smongo.AddWhere("firstname", smongo.EQ_OP, "Anele")
    smongo.PrepareRecord
    'set these fields
    smongo.SetField("work", "Software Engineer")
    smongo.SetField("age", 51)
    smongo.Update
    Log(smongo.RowCount & " record(s) updated!")

This finds all records where the first name = "Anele", then the work and age fields are updated.

Using the same approach, you can also include fields that didnt exist before. Here we update all Males and add a "loves" field.

B4X:
'update where
    Log("UPDATE WHERE")
    smongo.ClearWhere
    smongo.AddWhere("gender", smongo.EQ_OP, "Male")
    smongo.PrepareRecord
    smongo.SetField("loves", "b4x")
    smongo.Update
    Log(smongo.RowCount & " record(s) updated!")
 

Mashiane

Expert
Licensed User
Longtime User
The D in CRUD

In this example, we execute a delete and a delete where. To delete a single record, just like before...

We get the last id added to the table... (just to demonstrate) and then use that id to delete our record of interest.

B4X:
smongo.ClearWhere
    smongo.SelectAll
    smongo.MoveLast
    Dim lastID As String = smongo.GetID
    Log(lastID)
    smongo.Delete(lastID)
    Log(smongo.RowCount & " record(s) deleted.")

We can also execute a "DELETE WHERE..."

B4X:
smongo.ClearWhere
    smongo.AddWhere("age", smongo.EQ_OP, 52)
    smongo.DeleteWhere
    Log(smongo.RowCount & " record(s) deleted.")

And perhaps a "TRUNCATE TABLE ..." to delete all records

B4X:
smongo.DeleteAll
    Log(smongo.RowCount & " record(s) deleted.")
 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
We can also SELECT WHERE ... ORDER BY .... LIMIT.... OFFSET...

B4X:
smongo.ClearWhere
    smongo.AddSort("surname", True)
    smongo.SetLimit(5)
    smongo.SetSkip(1)
    smongo.SelectAll
    Log(smongo.RowCount & " record(s) selected!")
    For Each rec As Map In smongo.result
        Log(rec)
    Next

Here we select all records, sorted by surname in descending order, with a limit of 5 and an offset of 1
 

Mashiane

Expert
Licensed User
Longtime User
Of just a basic SELECT WHERE statement

B4X:
smongo.ClearWhere
    smongo.AddFields(Array("_id", "firstname", "lastname", "age"))
    smongo.AddWhere("age", smongo.EQ_OP, 52)
    smongo.SelectAll
    Log(smongo.RowCount & " record(s) selected!")
    For Each rec As Map In smongo.result
        Log(rec)
    Next

Here we use a projection to only show _id, firstname, lastname and age fields. Only these fields will be shown even if records have more fields.
We select all records where the age is 52

See the attached demo for more details.

#SharingTheGoodNess
 
Top