5
考慮這個例子:Mongoid如何知道字符串值和符號值之間的區別?
> 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的外殼:
> db.users.findOne({_id: ObjectId('52014532a6356d1ac9000001')})
{
"_id" : ObjectId("52014532a6356d1ac9000001"),
"foo" : "bar",
"foo2" : "bar",
...
}
但現在,早在紅寶石:
> 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"]
它是如何知道的?
謝謝!我想知道你應該如何區分mongo shell。 – lawrence
嗨,你可以使用類型的函數,如:'typeof my_var' –
@RafaelOliveira這不適合我。兩者都返回「字符串」。 – mlovic