使用C#在mongodb中进行异步更新 - 遇到问题

时间:2017-06-29 00:57:34

标签: c# asp.net-mvc mongodb

我有以下内容:

[HttpPost]
    public IActionResult ViewAll(Patient p)
    {
        var client = new MongoClient("mongodb://localhost:27017");
        var userId = _userManager.GetUserId(HttpContext.User);
        string db_name = "test" + userId;
        var database = client.GetDatabase(db_name);
        var collection = database.GetCollection<BsonDocument>("patients");
        var filter = Builders<BsonDocument>.Filter.Eq("Name", p.Name.ToString());


        NewPatient np = new NewPatient();
        np.Age = 100;
        np.Name = p.Name;

        np.Encounters.Add(new Encounter(p.Date, p.History_Illness, p.Past_History, p.Physical_Exam, 
            p.Tests, p.Diagnosis, p.Treatment, p.Prescriptions, p.Referrals));

        string json = JsonConvert.SerializeObject(np);


        MongoDB.Bson.BsonDocument document2
            = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(json);

      collection.InsertOne(document2);
        var h = Mongo(p, filter, document2);

        return Content(json);       

    }


    private async Task Mongo(Patient p, FilterDefinition<BsonDocument> f, BsonDocument document)
    {
        MongoClient client = new MongoClient("mongodb://localhost:27017");
        var userId = _userManager.GetUserId(HttpContext.User);
        string db_name = "test" + userId;
        var database = client.GetDatabase(db_name);

        var collection = database.GetCollection<BsonDocument>("patients");
        var filter = Builders<BsonDocument>.Filter.Eq("Name", p.Name.ToString());

        var result = await collection.UpdateOneAsync(f, document);

    }

我想要更新,但我无法更新。但是,我确实成功插入。但是,Insert只在那里调试,我真的只想更新。同时,我想删除一条记录并插入一条记录基本上等同于一次更新,所以我想我现在的快速修复只是复制和删除。 但是真的很想知道是否可以使这个更新功能起作用。

0 个答案:

没有答案