我试图更新数据库中的一个字段并得到非常令人困惑的结果:
查看字段' zipcode' 在它之前:
''
后
1153
但是我试着提出不同的价值
> db.users.find()
{ "_id" : ObjectId("51265a5ebdbff320f02007a4"), "region_code" : "NY", "region_name" : "New York", "secret_answer2" : "Mercedes", "l_name" : "Gonzalez", "f_name" : "Zachary", "month" : 5, "country_name" : "United States", "country_code" : "US", "year" : 1970, "password" : "xxxxxx", "day" : 2, "metrocode" : "501", "city" : "New York", "secret_answer" : "xxxxxx", "zipcode" : "", "longitude" : xxxxxxx, "areacode" : "212", "ip" : "xxxxxxx", "latitude" : xxxxxx }
> db.users.update({_id: ObjectId("51265a5ebdbff320f02007a4")}, {$set: {zipcode:02201} } )
> db.users.find()
{ "_id" : ObjectId("51265a5ebdbff320f02007a4"), "region_code" : "NY", "region_name" : "New York", "secret_answer2" : "Mercedes", "l_name" : "Gonzalez", "f_name" : "Zachary", "month" : 5, "country_name" : "United States", "country_code" : "US", "year" : 1970, "password" : "xxxxxx", "day" : 2, "metrocode" : "501", "city" : "New York", "secret_answer" : "xxxxxx", "zipcode" : 1153, "longitude" : xxxxxxx, "areacode" : "212", "ip" : "xxxxxxx", "latitude" : xxxxxx }
> db.users.find({},{zipcode:1})
{ "_id" : ObjectId("51265a5ebdbff320f02007a4"), "zipcode" : 1153 }
> db.users.update({_id:ObjectId("51265a5ebdbff320f02007a4")}, {$set:{zipcode:02201}})
> db.users.find({},{zipcode:1})
{ "_id" : ObjectId("51265a5ebdbff320f02007a4"), "zipcode" : 1153 }
答案 0 :(得分:1)
在JavaScript中,以0开头的数字被解释为八进制。
因此,要么将zipcode
值存储为字符串:
{$set: {zipcode: '02201'}}
或使用小数值:
{$set: {zipcode: 2201}}