加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – Mongoid如何知道字符串值和符号值之间的区别?

发布时间:2020-12-16 21:00:56 所属栏目:百科 来源:网络整理
导读:考虑这个例子: x = User.first # or any persisted Mongoid::Document= #User _id: 52014532a6356d1ac9000001,... x.set :foo,:bar= :bar x.set :foo2,'bar'= "bar" 请注意,“foo”和“foo2”未在Ruby中声明. 那么,在MongoDB shell中: db.users.findOne({_
考虑这个例子:
> x = User.first # or any persisted Mongoid::Document
=> #<User _id: 52014532a6356d1ac9000001,...>
> x.set :foo,:bar
=> :bar
> x.set :foo2,'bar'
=> "bar"

请注意,“foo”和“foo2”未在Ruby中声明.

那么,在MongoDB shell中:

> db.users.findOne({_id: ObjectId('52014532a6356d1ac9000001')})    
{
  "_id" : ObjectId("52014532a6356d1ac9000001"),"foo" : "bar","foo2" : "bar",...
}

但现在,回到Ruby:

> x = User.find x.id; nil # to clear out any possibility of metadata on the instance
=> nil
> [x.read_attribute(:foo),x.read_attribute(:foo2)]
=> [:bar,"bar"]

怎么知道的?

解决方法

BSON等像Seens支持值的符号类型,谷歌搜索
我找到了:

https://github.com/mongodb/mongo-ruby-driver/wiki/FAQ#FrequentlyAskedQuestions-Ruby-IseethatBSONsupportsasymboltype.DoesthismeanthatIcanstoreRubysymbolsinMongoDB%3F

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读