Home > Blockchain >  Mongodb find with objectid in ballerina
Mongodb find with objectid in ballerina

Time:11-20

in this case I have seen the official documentation of choreo, ballerina, and I could not find to execute a query where I need to filter by the ObjectId, in Java I could do it by importing BSON, but I could not find the same in ballerina.

In the following example, it does not give an error, because that field is mapped to that type.

Thanks a lot.

//map<json> queryString = {user_id: new object"61b75a0a08f2bf69b98a174c" };
        
map<json> queryString = {unique_id: 1 };
        map<json> projectionDoc = {unique_id: true, destination_address: true, _id: true};
        stream<Historial, error?> h_viajes = check mongoClient->find(collectionName = "trip_histories",projection = projectionDoc,filter = queryString);
        check h_viajes.forEach(function(Historial datas){
                io:println(datas.unique_id.toString());
                io:println(datas._id.toString());
                log:printInfo(datas.unique_id.toString());
            });

CodePudding user response:

The filterQuery has to be compatible with MongoDB Extended JSON. The correct filter for ObjectId type will be,

map<json> queryString = {_id: {"$oid": "<ObjectId>"}};
map<json> queryString = {_id: {"$oid": "63789aed271ba8943ff92574"}};

CodePudding user response:

Automatic casting of a string to The 12-byte ObjectId data type is not available in Ballerina, however it is possible to build it using the JSON notation provided by MongoDB.

For example:

map<json> filtersFields = {user_id: {"$oid": "60744b37d9bc8741f6edb714"}};

And through this syntax it is possible to filter by any field that is of type ObjectId

  • Related