gpt4 book ai didi

Ruby Mongo::ObjectID 比较

转载 作者:太空宇宙 更新时间:2023-11-03 16:57:53 25 4
gpt4 key购买 nike

所以,

根据 == 和 eql,我有两个相等的 Mongo::ObjectID 对象? (他们都返回真)。但是,如果一个是哈希中的键,另一个是存储在数组中的文档中,则失败:

myhash[array_of_docs[0]['_id']] # => nil
myhash.fetch(array_of_docs[0]['_id']) # => KeyError: key not found

我的数据库有 2 个集合,“书签”,主要有标题和 url,以及“标签”,有一个指向书签文档的 _id 的“bkm_id”键和一个“名称”键。通过以下查询,我将每个书签的 _id 映射到相应的以逗号分隔的标签列表:

bkms_tags_array = tags_collection.group(['bkm_id'], nil, { tags: Array.new }, "function(tag, agg){ agg.tags.push(tag.name) }", true)
bkms_tags = {}
bkms_tags_array.each do |bt|
bkms_tags.merge! Hash[bt.values[0], bt.values[1].join(", ")]
end
bkms_tags # => {4d60b29603e5665f82000001=>"socialnw, blablabla", 4d60b44703e5665fff000001=>"mail, app, google", 4d61812f03e5661ad8000001=>"socialnw, comms, web"}

鉴于 bks 是 'bookmarks_collection.find.to_a' 的结果,这是我的问题:

bkms_tags[bks[0]['_id']] # => nil
bkms_tags.include? bks[0]['_id'] # => false ; however:
bkms_tags.keys.include? bks[0]['_id'] # => true

“hash.include”怎么来的?是 false 和 'hash.keys.include?'是真实的?不同查询返回的ObjectID有区别吗?

就像我说的,== 和 eql?返回真:

bkms_tags.each { |k,v| puts k == bks[0]['_id'] } # => true false false
bkms_tags.keys.each { |k| puts k == bks[0]['_id'] } # => true false false
bkms_tags.each { |k,v| puts k.eql? bks[0]['_id'] } # => true false false
bkms_tags.keys.each { |k| puts k.eql? bks[0]['_id'] } # => true false false

因此,通过任何可能的比较,'bks[0]['_id']' 是 bkms_tags 的键,但是当我尝试检索它的值时,Ruby 不知何故变得困惑。

有什么想法吗?谢谢!

额外信息:

一些示例文档:

书签

{ "_id" : ObjectId("4d60b29603e5665f82000001"), "url" : "http://www.facebook.com/", "title" : "Facebook", "host" : "facebook.com", "saved_at" : "Sun Feb 20 2011 01:20:06 GMT-0500 (PET)" }
{ "_id" : ObjectId("4d60b44703e5665fff000001"), "url" : "http://mail.google.com/", "title" : "gmail", "host" : "mail.google.com", "saved_at" : "Sun Feb 20 2011 01:27:19 GMT-0500 (PET)" }
{ "_id" : ObjectId("4d61812f03e5661ad8000001"), "url" : "http://twitter.com/", "title" : "twitter", "host" : "twitter.com", "saved_at" : "Sun Feb 20 2011 16:01:35 GMT-0500 (PET)" }

标签

{ "_id" : ObjectId("4d60b44703e5665fff000002"), "bkm_id" : ObjectId("4d60b44703e5665fff000001"), "name" : "mail" }
{ "_id" : ObjectId("4d60b44703e5665fff000003"), "bkm_id" : ObjectId("4d60b44703e5665fff000001"), "name" : "app" }
{ "_id" : ObjectId("4d60b29603e5665f82000003"), "bkm_id" : ObjectId("4d60b29603e5665f82000001"), "name" : "socialnw" }
{ "_id" : ObjectId("4d60b29603e5665f82000004"), "bkm_id" : ObjectId("4d60b29603e5665f82000001"), "name" : "blablabla" }
{ "_id" : ObjectId("4d61812f03e5661ad8000003"), "bkm_id" : ObjectId("4d61812f03e5661ad8000001"), "name" : "comms" }
{ "_id" : ObjectId("4d61812f03e5661ad8000004"), "bkm_id" : ObjectId("4d61812f03e5661ad8000001"), "name" : "web" }

编辑

再测试一些,我发现了更多的违规行为:

bkms_ids # => [4d60b29603e5665f82000001, 4d61812f03e5661ad8000001, 4d61ba9103e5667dbe000001, 4d61ba9103e5667dbe000001]
bkms_ids[2] == bkms_ids[3] # => true
bkms_ids[2].eql? bkms_ids[3] # => true
bkms_ids.uniq # => nothing changes: [4d60b29603e5665f82000001, 4d61812f03e5661ad8000001, 4d61ba9103e5667dbe000001, 4d61ba9103e5667dbe000001]

编辑 2

按要求,我的bson版本:

irb> BSON::VERSION # NameError: uninitialized constant BSON::VERSION
$ gem list bson

*** LOCAL GEMS ***

bson (1.2.2)
bson_ext (1.2.2)

并检查和分类我的文档数组:

array_of_docs = bookmarks_collection.find.to_a
array_of_docs[0].inspect # => "{\"_id\"=>4d60b29603e5665f82000001, \"url\"=>\"http://www.facebook.com/\", \"title\"=>\"Facebook\", \"host\"=>\"facebook.com\", \"saved_at\"=>2011-02-20 06:20:06 UTC}"
array_of_docs[0].class # => OrderedHash
array_of_docs[0]['_id'].class # => Mongo::ObjectID

最佳答案

uniq 更改为 uniq! :

bkms_ids

bkms_ids[2] == bkms_ids[3]

bkms_ids[2].eql? bkms_ids[3]

bkms_ids.uniq!

关于Ruby Mongo::ObjectID 比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5061172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com