如何将字符串转换为 ObjectId [英] How to convert string into ObjectId

查看:50
本文介绍了如何将字符串转换为 ObjectId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 MongoDB 获取数据并绑定到 WPF 数据网格.

I am getting data from MongoDB and binding to a WPF datagrid.

我的代码选择多行、检索 ID 并更新所选记录:

My code selects multiple rows, retrieves IDs and updates the selected records:

var server = MongoServer.Create(this.connectionString);
var db = server.GetDatabase(DATABASE);
var viewTrue = db.GetCollection(RISKALERT_TBL);
var count = viewTrue.Count();
foreach (RiskSettings row in grdRiskAlerts.SelectedItems)
{
    viewTrue.Update(Query.EQ("ID",row.ID), Update.Set("View", "False"));
    LoadandBindData();
}

但它不会更新记录.

我想也许 row.id 正在返回字符串,而 ID 数据类型是 objectId.

I thought maybe row.id is returning string and ID datatype is objectId.

此查询适用于上述情况以外的其他数据类型.

This query is working for other datatype except the above case.

推荐答案

要将字符串转换为 ObjectId,请使用 ObjectId.Parse(string) 方法.

To convert a string to an ObjectId, use the ObjectId.Parse(string) method.

还要尝试匹配 "_id" 而不是 "ID".

Also try to match on "_id" rather than "ID".

比如:

viewTrue.Update(Query.EQ("_id", ObjectId.Parse(row.ID)), Update.Set("View", "False")); 

这篇关于如何将字符串转换为 ObjectId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆