[JAVA-5274] Add method toJson() to class BsonArray Created: 20/Dec/23  Updated: 03/Jan/24

Status: Investigating
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Unknown
Reporter: Steel Li Assignee: Ashni Mehta
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I will create a PR on GitHub to implement the feature.



 Comments   
Comment by Steel Li [ 03/Jan/24 ]

Hi tom.selander@mongodb.com, we are migrating a system using MongoDB written by C#. In the system, we don't use concrete types to process data. There is an API used to find documents as following code:

 

public async Task<string> Find(FilterRequest model)
{
    var col = getCollecton(model);
    var data = await col.Find(model.GetFilter()).ToListAsync();
    var json = data.ToJson(new JsonWriterSettings()
    {
        OutputMode = JsonOutputMode.Strict
    });
    return json;
}

 

But there is no toJson function in BsonArray or other BsonValue implemented classes such as BsonInt32, etc. in Java driver. If we need to create same API, we need to iterate documents then convert to JSON one by one and combine them.

json = "[" + document1.toJson() + "," + document2.toJson() + "," ... + "]";

 

If we have the toJson() function, we can use following code to implement it:

 

@Override
public String find(FilterDto dto) {
    BsonDocument filter = convertMapToBsonDocument(dto.getFilter());
    MongoCollection<BsonDocument> collection = getCollection(dto);
    return convertMongoIterableToJson(collection.find(filter));
}
private String convertMongoIterableToJson(MongoIterable<? extends BsonValue> iterable) {
    BsonArray bsonArray = new BsonArray();
    try (MongoCursor<? extends BsonValue> cursor = iterable.cursor()) {
        while (cursor.hasNext()) {
            bsonArray.add(cursor.next());
        }
    }
    return bsonArray.toJson();
}

 

The worst is we have another API distinct, which can provide the distinct result.

public async Task<string> Distinct(DistinctRequest model)
 {
    var col = getCollecton(model);
    var data = await col.Distinct(model.GetField(), model.GetFilter()).ToListAsync();
    var json = data.ToJson(new JsonWriterSettings()
    {
        OutputMode = JsonOutputMode.Strict
    });
    return json;}

But without the toJson() function, we cannot implement the API using Java since each element of the distinct result are just like BsonInt32, BsonInt64... but not BsonDocument.

If we have the function, we can use following code to implement it:

    @Override
    public String distinct(DistinctDto dto) {
        MongoCollection<BsonDocument> collection = getCollection(dto);
        BsonDocument filter = convertMapToBsonDocument(dto.getFilter());
        DistinctIterable<BsonValue> iterable = collection.distinct(dto.getField(), filter, BsonValue.class);
        return convertMongoIterableToJson(iterable);
    }
    private String convertMongoIterableToJson(MongoIterable<? extends BsonValue> iterable) {
        BsonArray bsonArray = new BsonArray();
        try (MongoCursor<? extends BsonValue> cursor = iterable.cursor()) {
            while (cursor.hasNext()) {
                bsonArray.add(cursor.next());
            }
        }
        return bsonArray.toJson();
    }

Above are my user cases, and do you have a simple approach to implement my request regarding the code written by C#?

Comment by Tom Selander [ 02/Jan/24 ]

Hi steelcg@outlook.com, This is not something we support currently because MongoDB server only supports arrays embedded within documents so this isn't supported in BSON. Can you describe what your use case is for this functionality? 

Comment by Steel Li [ 20/Dec/23 ]

the PR on GitHub: https://github.com/mongodb/mongo-java-driver/pull/1284

Comment by PM Bot [ 20/Dec/23 ]

Hi steelcg@outlook.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Thu Feb 08 09:04:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.