我有一个由数组类型BSON::ObjectId
我希望它比较对一些ID作为字符串。
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
? 如果是这样,怎么样?
您可以使用BSON::ObjectId(@my_id_as_a_string)
为代表的ID为BSON::ObjectId
裁判http://api.mongodb.org/ruby/current/BSON.html#ObjectId-class_method
Mongoid 2.X与10gen公司的司机:
BSON::ObjectId.new('506144650ed4c08d84000001')
Mongoid 3助力车:
Moped::BSON::ObjectId.from_string('506144650ed4c08d84000001')
Mongoid 4(机动脚踏型车)/ Mongoid 5/6/7(蒙戈):
BSON::ObjectId.from_string('506144650ed4c08d84000001')
collection.delete_one({"_id"=>BSON::ObjectId(params['id'])})
这个工作对我来说,它成功地删除了数据库中的记录