0

I asked a question a couple of days ago to collect data from MongoDB as a tree.

MongoDB create an array within an array

I am a newbie to MongoDB, but have used JSON quite substantially. I thought using a MongoDB to store my JSON would be a great benefit, but I am just experiencing immense frustration.

I am using .NET 4.5.2

I have tried a number of ways to return the output from my aggregate query to my page.

public JsonResult GetFolders()
{
    IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("DataStore");

    PipelineDefinition<BsonDocument, BsonDocument> treeDocs = new BsonDocument[]
    {
        // my query, which is a series of new BsonDocument
    }

    var documentGroup = collection.Aggregate(treeDocs).ToList();

    // Here, I have tried to add it to a JsonResult Data,
    // as both documentGroup alone and documentGroup.ToJson()
    // Also, loop through and add it to a List and return as a JsonResult
    // Also, attempted to serialise, and even change the JsonWriterSettings.

}

When I look in the Immediate Window at documentGroup, it looks exactly like Json, but when I send to browser, it is an escaped string, with \" surrounding all my keys and values.

I have attempted to create a model...

public class FolderTree
{
    public string id { get; set; }
    public string text { get; set; }
    public List<FolderTree> children { get; set; }
}

then loop through the documentGroup

foreach(var docItem in documentGroup)
{
    myDocs.Add(BsonSerializer.Deserialize<FolderTree>(docItem));
}

but Bson complains that it cannot convert int to string. (I have to have text and id as a string, as some of the items are strings)

How do I get my MongoDB data output as Json, and delivered to my browser as Json?

Thanks for your assistance.

========= EDIT ===========

I have attempted to follow this answer as suggested by Yong Shun below, https://stackoverflow.com/a/43220477/4541217 but this failed.

I had issues, that the "id" was not all the way through the tree, so I changed the folder tree to be...

public class FolderTree
{
    //[BsonSerializer(typeof(FolderTreeObjectTypeSerializer))]
    //public string id { get; set; }
    [BsonSerializer(typeof(FolderTreeObjectTypeSerializer))]
    public string text { get; set; }
    public List<FolderTreeChildren> children { get; set; }

}

public class FolderTreeChildren
{
    [BsonSerializer(typeof(FolderTreeObjectTypeSerializer))]
    public string text { get; set; }
    public List<FolderTreeChildren> children { get; set; }
}

Now, when I look at documentGroup, I see...

[0]: {Plugins.Models.FolderTree}
[1]: {Plugins.Models.FolderTree}

To be fair to sbc in the comments, I have made so many changes to get this to work, that I can't remember the code I had that generated it.

Because I could not send direct, my json result was handled as...

JsonResult json = new JsonResult();
json.Data = documentGroup;
//json.Data = JsonConvert.SerializeObject(documentGroup);
json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

return json;

Note, that I also tried to send it as...

json.Data = documentGroup.ToJson();
json.Data = documentGroup.ToList();
json.Data = documentGroup.ToString();

all with varying failures.

If I leave as documentGroup, I get {Current: null, WasFirstBatchEmpty: false, PostBatchResumeToken: null}

If I do .ToJson(), I get "{ \"_t\" : \"AsyncCursor`1\" }"

If I do .ToList(), I get what looks like Json in json.Data, but get an error of Unable to cast object of type 'MongoDB.Bson.BsonInt32' to type 'MongoDB.Bson.BsonBoolean'.

If I do .ToString(), I get "MongoDB.Driver.Core.Operations.AsyncCursor`1[MongoDB.Bson.BsonDocument]"

=========== EDIT 2 =================

As this way of extracting the data from MongoDB doesn't want to work, how else can I make it work?

I am using C# MVC4. (.NET 4.5.2)

I need to deliver json to the browser, hence why I am using a JsonResult return type.

I need to use an aggregate to collect from MongoDB in the format I need it.

My Newtonsoft.Json version is 11.0.2

My MongoDB.Driver is version 2.11.1

My method is the simplest it can be.

What am I missing?

David
  • 214
  • 3
  • 15
  • Does this answer your question? [mongodb C# exception Cannot deserialize string from BsonType Int32](https://stackoverflow.com/questions/19664394/mongodb-c-sharp-exception-cannot-deserialize-string-from-bsontype-int32). Think this question & answer would help you to cast integer to string in a BSON document. – Yong Shun Nov 04 '21 at 23:58
  • And would be better if you can attach the output document in the question from your mongo aggregate. Thanks. – Yong Shun Nov 05 '21 at 00:00
  • *but when I send to browser, it is an escaped string, with \" surrounding all my keys and values.* - then please share a [mcve] showing how you are sending the data to the browser. You may be double-serializing your JSON as shown in [Strings sent through Web API's gets wrapped in quotes](https://stackoverflow.com/q/33681978/3744182). In particular, if you have already serialized your response to JSON, you may return it as-is as shown in [this answer](https://stackoverflow.com/a/33684128/3744182) by [Brian Rogers](https://stackoverflow.com/users/10263/brian-rogers) – dbc Nov 05 '21 at 13:23
  • Editted my question with new information based on the comments here. – David Nov 05 '21 at 19:00

0 Answers0