I’m trying to create a simple signup service using Ballerina and MongoDB (using MongoDB Compass version 7.0.11). However, I’m encountering several errors related to MongoDB integration with Ballerina. Specifically, I’m getting the following errors during compilation:
Incompatible types:
1.Error: expected ‘ballerinax/mongodb:5.0.0:Client’, found ‘(ballerinax/mongodb:5.0.0:Client|ballerinax/mongodb:5.0.0:DatabaseError|ballerinax/mongodb:5.0.0:ApplicationError|error)’
Action invocation as an expression not allowed:
2.Error: action invocation as an expression not allowed here
Invalid remote method call:
3.Error: invalid remote method call: expected a client object, but found ‘ballerina/http:2.12.1:Request’
Type mismatch errors:
4.Error: incompatible type for parameter ‘targetType’ with inferred typedesc value: expected ‘typedesc<record {| anydata…; |}>’, found ‘typedesc’
Error: incompatible types: expected ‘map’, found ‘json’
Undefined functions:
5.Error: undefined function ‘hasNext’ in type ‘stream<json,error>’
Error: undefined method ‘insert’ in object ‘ballerinax/mongodb:5.0.0:Collection’
This is my Code
Code
This is my Error
Error
This is my Folder Structure
Folder Structure
I am using MongoDB Compass, with the database hosted locally at localhost:27017. My goal is to create a simple signup system that inserts user data (username, password) into a MongoDB collection using Ballerina.
1.I updated the Ballerina.toml file to include the MongoDB dependency:
Update Ballerina.toml
2.Used check to handle potential errors when creating the MongoDB client and performing database operations, but I am still encountering the same errors.
I would like to know the correct approach for integrating MongoDB with Ballerina (Swan Lake Update 10) and understand how to fix the above compilation errors. Specifically, I want to successfully run a service that inserts signup data into MongoDB.
Ballerina Version:
Ballerina 2201.10.0 (Swan Lake Update 10)
MongoDB Version:
MongoDB Compass 7.0.11
2
Answers
Following code resolve your issues
These are the reasons for each error
When initializing the client, the return type can be either
mongodb:Client
(when the client created sucessfully) or an error(when there is an error, while creating the client).https://github.com/ballerina-platform/module-ballerinax-mongodb/blob/76cdb7eca6c5ddb305df1d5350dc630137ffc2d0/ballerina/client.bal#L28
Actions
cannot be use declare module level variablesmongoDb->getDatabase("userDB")
is a remote call, SO it is an action. One way to solve this issue is to declare the variable in the function scope.->
used to call remote functions..
use to call methods inside the class.find
functionThere are a number of issues as indicated by the compile-time errors.
The first error at L12 is because the MongoDB client initialization can result in an error. You can use
check
to cause module initialization to fail in the case of an error.Also see the
check
expression example.L22 and L23 – actions (using
->
) are not allowed at module-level. This restriction is related to sequence diagrams generated for code. If you want it to happen at module initialization, you can move the calls to a moduleinit
function.getJsonPayload()
is a normal method (non-remote/resource) on a normal objecthttp:Request
(non-client). The normal method call syntax needs to be used, rather than the remote method call action/resource access action. I.e., use.
instead of->
.There are two errors here.
i. The first error is because you’re using
json
where a subtype ofrecord {}
is expected, so a mapping type is expected here. Note thatrecord {}
orrecord {| anydata...; |}
is the same asmap<anydata>
. Thejson
type is a union which consists ofmap<json>
(representing JSON objects), but also()
,boolean
,int
,float
,decimal
,string
, andjson[]
. So if you want to bind to JSON object, you have to usemap<json>
, rather than justjson
. Note that you have to usecheck
or handle the error on the RHS appropriately sincefind
can return an error value too.ii. Similarly, the second error is because the type of
filter
isjson
rather thanmap<json>
, which is the expected type for the argument.Again, two errors.
i. Streams do not have a
hasNext
function. You can use thenext()
function and check if the value is present instead.ii. There is no
insert
method onmongodb:Collection usersCollection
. Did you mean to useinsertOne
orinsertMany
instead?API docs for
ballerinax/mongodb
– https://central.ballerina.io/ballerinax/mongodb/5.0.0If you are not using it already, please note that using the Ballerina plugin on VS Code will help identify available operations easily (with completions and suggestions) and will help you identify and fix errors easily.
It is also recommended to share code and error snippets rather than images when raising StackOverflow questions.