0

anyone know how to insert this json array into mongodb using insertmany() and C# ??

i cant find any good source for this problem

enter image description here

JSON ARRAY

1 Answer 1

1

MongoCollectionBase.InsertMany expects an IEnumerable<TDocument>.
So you need to deserialize your data json element to TDocument[], (Datum) and then pass that array to InsertMany.

Based on the below json, which is different from your screenshot

{
    "data": [
        {
            "pulsa_code": "alfamart100",
            "pulsa_op": "Alfamart Voucher",
            "pulsa_nominal": "Voucher Alfamart Rp 100.000",
            "pulsa_price": 100000,
            "pulsa_type": "voucher",
            "masaaktif": "0",
            "status": "active"
        }
    ]
}

This should work

//...
public class Datum
{
    public string pulsa_code { get; set; }
    public string pulsa_op { get; set; }
    public string pulsa_nominal { get; set; }
    public double pulsa_price { get; set; }
    public string pulsa_type { get; set; }
    public string masaaktif { get; set; }
    public string status { get; set; }
    public double harga { get; set; }
}

public class PriceListPrepaidModel
{
    public List<Datum> data { get; set; }
}

public class PriceList : BaseDatabase
{
    //NOTE: The strongly typed IMongoCollection<T> must be the same type as the entities passed to InsertMany
    private IMongoCollection<Datum> _pricelistCollection;
    //private IMongoCollection<PriceListPrepaidModel> _pricelistCollection;
    public PriceList(IServiceProvider serviceProvider)
    {
        _pricelistCollection = DB.GetCollection<PriceListPrepaidModel>("price_prepaid");
    }
    public ResponseModel<string> PriceListPrepaidTest(PriceListPrepaidRequest request)
    {
        var entityResult = new ResponseModel<string>();
        try
        {
            Console.WriteLine(response.Content);

            //Desirialize to your model class, not JObject
            var model = JsonConvert.DeserializeObject<PriceListPrepaidModel>(message.AsString);
            foreach (var item in model.data)
            {
                item.pulsa_price = (int)item.pulsa_price + 200;
            }

            //Insert the array of `Datum`
            _pricelistCollection.InsertMany(model.data);

            entityResult.Value = JsonConvert.SerializeObject(model.data);
            entityResult.Status = true;
        }
        catch (Exception ex)
        {
            entityResult.Messages.Add(new ResponseMessageModel()
            {
                Type = ResponseMessageModel.MessageType.ERROR,
                Title = "Error",
                Message = ex.Message
            });
        }
        return entityResult;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

here it is the copy code pastebin.com/6s8myuuy idk how to do insertmany() it is necessary to use model class or not
there is an error in _prepaidCollection.InsertMany(model.data); it says "cannot convert IList<Model.Datum> to Generic.IEnumerable<Model.PriceListPrepaid>
Sorry, I've just updated it now, IMongoCollection<T> expects a list of IEnumerable<T>.So T must be either Datum or converted from Datum to Model.PriceListPrepaid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.