skip to Main Content

In this case I have seen the official documentation of choreo, ballerina, but I could not find how 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.

//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());
            });

2

Answers


  1. Ballerina changes json string to BSON object. For this purpose, 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"}};
    

    {"$oid": "<ObjectId>"} can be used for any field of type ObjectId.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search