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
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,
{"$oid": "<ObjectId>"}
can be used for any field of typeObjectId
.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:
And through this syntax it is possible to filter by any field that is of type ObjectId