5

構成された型の配列があり、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ますか? もしそうなら、どのように?

4

4 に答える 4

14

Mongoid 2.x と 10gen のドライバー:

BSON::ObjectId.new('506144650ed4c08d84000001')

モペット付きのモンゴイド3:

Moped::BSON::ObjectId.from_string('506144650ed4c08d84000001')

Mongoid 4 (モペット) / Mongoid 5/6/7 (mongo):

BSON::ObjectId.from_string('506144650ed4c08d84000001')
于 2012-09-25T08:20:42.433 に答える
5

BSON::ObjectId(@my_id_as_a_string)IDを次のように表現するために使用できますBSON::ObjectId

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

于 2012-09-25T08:22:42.440 に答える
0
collection.delete_one({"_id"=>BSON::ObjectId(params['id'])})

これは私にとってはうまくいき、データベースからレコードを正常に削除しました

于 2016-06-21T10:12:33.197 に答える