比较字符串ID与BSON :: ObjectId [英] Compare string ID to BSON::ObjectId

查看:93
本文介绍了比较字符串ID与BSON :: ObjectId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由BSON::ObjectId类型组成的数组,我希望它与某些ID作为字符串进行比较.

I have an array of made up of type BSON::ObjectId and I want it to compare against some IDs as strings.

if my_array_of_BSON_ObjectIds.include?(@my_id_as_a_string)
   # delete the item from the array
else
   # add the item to the array as a BSON::ObjectId
end

这是行不通的,因为类型不同,我可以将字符串转换为BSON::ObjectId吗?如果可以,怎么办?

This is not working as the types are different, can I turn my string into a BSON::ObjectId? If so, how?

推荐答案

您可以使用BSON::ObjectId(@my_id_as_a_string)将您的ID表示为BSON::ObjectId

You can use BSON::ObjectId(@my_id_as_a_string) for representation your id as BSON::ObjectId

引用 http://api.mongodb.org/ruby/current /BSON.html#ObjectId-class_method

这篇关于比较字符串ID与BSON :: ObjectId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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