使用c#更新mongodb数据库中的更改记录

问题描述 投票:0回答:1

这是我的c#代码

public static void updateSubmit(string id,string fname,string lname,string email,string password,string address)
{
    string connectionString = "mongodb://10.10.32.125:27017";
       MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
       MongoClient mongoClient = new MongoClient(settings);
       var Server = mongoClient.GetDatabase("mongovaibhav");
       var collection = Server.GetCollection<employee>("mongov");
       ObjectId objectId = ObjectId.Parse(id);
       var filter = Builders<employee>.Filter.Eq(s => s._id, objectId);
       employee emp = new employee();
       emp.fname = fname;
       emp.lname = lname;
       emp.email = email;
       emp.pass = password;
       emp.address = address;
       collection.ReplaceOneAsync(filter, emp);
}

这是我的ajax代码,我也发送更新请求和数据

 function updateSubmit()
    {
        $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: 'Home.aspx/updateSubmit',
            data: "{'id':'" + $("#hidden").val()+ "','fname':'" + $("#fname").val() + "','lname':'" + $("#lname").val() + "','email':'" + $("#email").val() + "','password':'" + $("#password").val() + "','address':'" + $("address").val() + "'}",
            async: false,
            success: function (response) {
                alert("You Have SuccessFully Update Data");
            },
            error: function () {
                console.log('there is some error');
            }
        });
    }

现在我的问题是我收到警告消息,表示您已成功更新记录,但记录无法更改数据库中的效果

javascript c# ajax mongodb
1个回答
0
投票

我有解决方案我错误地在我的“参数”变量中我写密码而不是“通过”,因为我的员工类包含“通行证”属性谢谢大家@souvik @felix

  string connectionString = "mongodb://10.10.32.125:27017";
            MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
            MongoClient mongoClient = new MongoClient(settings);
            var Server = mongoClient.GetDatabase("mongovaibhav");
            var collection = Server.GetCollection<employee>("mongov");
            ObjectId objectId = ObjectId.Parse(id);
              var filter = Builders<employee>.Filter.Eq(s => s._id, objectId);   
            string param = "{$set: { fname:'" + fname + "',lname:'" + lname + "',email:'" + email + "',pass:'" + password + "',address :'" + address + "' } }";
            BsonDocument document = BsonDocument.Parse(param);
            collection.UpdateMany(filter, document);
© www.soinside.com 2019 - 2024. All rights reserved.