0

我试图更新数据库中的一个字段,结果非常混乱:

查看之前的“邮政编码”字段:

''

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 }
4

1 回答 1

1

在 JavaScript 中,以 0 开头的数字被解释为八进制。

因此,要么将您的zipcode值存储为字符串:

{$set: {zipcode: '02201'}}

或使用十进制值:

{$set: {zipcode: 2201}}
于 2013-02-21T17:57:43.233 回答