首页 文章

如何使用邮递员发布到MongoDb集合

提问于
浏览
1

我正在尝试使用邮递员将数据插入MongoDb集合 . 我该如何处理;以JSON格式硬编码数据有效,但我希望能够 insert using postman in JSON format.

这是允许我使用postman中的post函数直接输入的代码,没有输入:

public async void Insert([FromBody]string value)
    {
        var client = new MongoClient();
        var dbs = client.GetDatabase("test");
        var collection = dbs.GetCollection<BsonDocument>                                                          ("restaurants");

        BsonArray dataFields = new BsonArray { new BsonDocument { 
         { "ID" , ObjectId.GenerateNewId()}, { "_id", ""}, } };

        var document = new BsonDocument
          {

        {"borough",value },
        {"cuisine","Korean"},

        {"name","Bellaa"},    
        {"restaurant_id","143155"}

        };
    await collection.InsertOneAsync(document);

    }

1 回答

  • 2

    您可以将其作为原始数据发送 . 您将帖子类型设置为 application/json .

    enter image description here

    这来自docs .

相关问题