如何将字符串转换为ObjectId

发布于 2024-12-21 07:17:03 字数 520 浏览 5 评论 0原文

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

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

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 am getting data from MongoDB and binding to a WPF datagrid.

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();
}

But it does not update the record.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只想待在家 2024-12-28 07:17:03

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

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

所以像这样:

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

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

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

So something like:

viewTrue.Update(Query.EQ("_id", ObjectId.Parse(row.ID)), Update.Set("View", "False")); 
明月夜 2024-12-28 07:17:03

在为 ObjectID 设置公共属性时,我遇到了同样的问题。

我的属性使用以下代码片段将 ObjectID 转换为字符串,然后再转换回 ObjectID。

ObjectID 没有作为一个选项出现,所以我必须使用完整的命名空间,像这样 MongoDB.Bson.ObjectId.Parse 访问 .Parse()

    public string Id
    {
        get { return Convert.ToString(_id); }
        set { _id = MongoDB.Bson.ObjectId.Parse(value); }
    }

希望这有帮助!

I came across the same issue when setting up a public property for the ObjectID.

My property converted the ObjectID to a string, and back to an ObjectID using the following code snippet.

The ObjectID wasn't coming up as an option so I had to use the complete namespace, to access the .Parse() like this MongoDB.Bson.ObjectId.Parse

    public string Id
    {
        get { return Convert.ToString(_id); }
        set { _id = MongoDB.Bson.ObjectId.Parse(value); }
    }

Hope this helps!

陌上芳菲 2024-12-28 07:17:03

我发现的最简单的方法是使用: new ObjectId(yourString) ...这将从字符串中为您提供一个 MongoDB ObjectId,并且应该适用于您的任何查询。

The easiest way I found was to use: new ObjectId(yourString) ...This will give you a MongoDB ObjectId from a string and should work with any of your queries.

烟若柳尘 2024-12-28 07:17:03

您只需要从 mongo 中获取 ObjectId 函数即可。

ObjectId = require('mongodb').ObjectID;

然后你可以像这样使用它:

ObjectId(row.ID)

所以你可以将代码行更改为:

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

You just need to require the ObjectId function from your mongo.

ObjectId = require('mongodb').ObjectID;

Then you can use it like that:

ObjectId(row.ID)

So you can change your line of code to:

viewTrue.Update(Query.EQ("ID",ObjectId(row.ID)), Update.Set("View", "False"));
坦然微笑 2024-12-28 07:17:03

另一种方法是:

myString := "5f4f321d7125461260ad9d74"

objectId, err := primitive.ObjectIDFromHex(myString)

if err != nil {
    panic("Invalid id")
}

Another way is:

myString := "5f4f321d7125461260ad9d74"

objectId, err := primitive.ObjectIDFromHex(myString)

if err != nil {
    panic("Invalid id")
}
山有枢 2024-12-28 07:17:03

如果你用mgo,你可以试试这个

id := "603f4d6415177136d0583d4d"
_id := bson.ObjectIdHex(id)

if you use mgo, you can try this one

id := "603f4d6415177136d0583d4d"
_id := bson.ObjectIdHex(id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文