String to Json data conversion for Collections in mongodb

Asked

Viewed 62 times

-1

When I query a Collection in mongodb, it returns a field of the string type.

[{"id":"1325","nome":produto1,"estoque":"10"}]

But I need to make this field a Json.

It is possible to do this in the query using Find()?

Collection.Find(_=>true).Sort(Builders<Model>.Sort.Descending("_Id")).FirstOrDefault();


class Model
{
        [BsonId]
        public ObjectId _Id { get; set; }

        [JsonIgnore]
        public string Mensagem { get; set; }
}
  • Looking at the code, it’s unclear what you’re trying to do, what the problem is and where you’re finding difficulty, present a [MCVE]

  • I just want to take a string and turn it into json. But I wanted to know if it is possible to do this conversion directly in the query of Mongo.

  • If the object is compatible yes, it would be better for you to post the json string you are receiving and the object structure for de-ralization, this has nothing to do with Mongo. Note that your string has different attributes from the model

  • [{"id":"1325","name":product1,"stock":"10"}] The string is saved like this. It is mounted in the code and saved as a string in the bank.

  • Where? in the message field? Wouldn’t it be better for Block to have the same properties?

1 answer

0


I imagine what you have explained is that this string is returned within a field of the consulted object. That said, I’ve prepared this little example for you to try.

Similar issue #470437

class Program
{
    static void Main(string[] args)
    {
        IList<Model> entities = new List<Model>
        {
            new Model { Mensagem = "[{\"id\":\"1325\",\"nome\":\"produto1\",\"estoque\":\"10\"}]" }
        };

        var productJson = JsonConvert.DeserializeObject<List<Product>>(entities.FirstOrDefault().Mensagem).FirstOrDefault();

        Console.WriteLine(productJson);
        Console.ReadKey();
    }
}

class Product
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("nome")]
    public string Nome { get; set; }

    [JsonProperty("estoque")]
    public int Estoque { get; set; }

    public override string ToString()
    {
        return string.Join(" ", this.GetType()
                            .GetProperties()
                            .Select(prop => prop.GetValue(this)));
    }
}

class Model
{
    
    [BsonId]
    public ObjectId _Id { get; set; }

    [System.Text.Json.Serialization.JsonIgnore]
    public string Mensagem { get; set; }
}
  • It would be that yes. But for min continues to break.

  • I imagine the error is in the query made to Mongodb. What error did you have?

  • Newtonsoft.Json.Jsonreaderexception: 'Invalid Character after Parsing Property name. Expected ':' but got: S. Path '', line 1, position 10.'

  • Your saved string is formatted wrong (the product without quotation marks). Note that in my example I fixed it. Here is the string with correct formatting: [{"id":"1325","name":"product1","stock":"10"}] Tip: You can use a json validator online. In this case it is simple, but we see many more complex scenarios. https://jsonformatter.curiousconcept.com/#

Browser other questions tagged

You are not signed in. Login or sign up in order to post.