在MongoDB的Collection中存在一些字段 reading 原本应该是string 类型,但是在写入时错误的写成了int,因此需要更改int类型为string类型。其中数据结构如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{
    "_id" : ObjectId("5d79097debeae9d530225b93"),
    "type" : 2,
    "name" : "ZhiHu",
    "intime" : NumberLong(1568213373),
    "data" : [ 
        {
            "top" : "1",
            "title" : "六种配色、5499 元起的 iPhone 11 系列发布,配置和性能如何?",
            "reading" : 72920000,
            "url" : "https://www.zhihu.com/question/345431917",
            "state" : ""
        }, 
        {
            "top" : "2",
            "title" : "男生找对象看重什么?",
            "reading" : 35880000,
            "url" : "https://www.zhihu.com/question/292443025",
            "state" : ""
        }, 
        {
            "title" : "如何评价谢娜辟谣与何老师关系破裂?",
            "reading" : 24700000,
            "url" : "https://www.zhihu.com/question/345356641",
            "state" : "",
            "top" : "3"
        }, 
        {
            "top" : "4",
            "title" : "如何看待 iPhone 11 发布会上苹果首次公开对比三星、华为等友商手机?",
            "reading" : 11130000,
            "url" : "https://www.zhihu.com/question/345432363",
            "state" : ""
        }
	]
}

解决方法:

在MongoDB的shell中执行如下语句即可:

db.zhihu.find({}).forEach(function(item){item.data.forEach(function(x){x.reading = String(x.reading); db.zhihu.save(item)})});

执行修改后的reading变成了string, 结构如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{
    "_id" : ObjectId("5d79097debeae9d530225b93"),
    "type" : 2,
    "name" : "ZhiHu",
    "intime" : NumberLong(1568213373),
    "data" : [ 
        {
            "top" : "1",
            "title" : "六种配色、5499 元起的 iPhone 11 系列发布,配置和性能如何?",
            "reading" : "72920000",
            "url" : "https://www.zhihu.com/question/345431917",
            "state" : ""
        }, 
        {
            "top" : "2",
            "title" : "男生找对象看重什么?",
            "reading" : "35880000",
            "url" : "https://www.zhihu.com/question/292443025",
            "state" : ""
        }, 
        {
            "title" : "如何评价谢娜辟谣与何老师关系破裂?",
            "reading" : "24700000",
            "url" : "https://www.zhihu.com/question/345356641",
            "state" : "",
            "top" : "3"
        }, 
        {
            "top" : "4",
            "title" : "如何看待 iPhone 11 发布会上苹果首次公开对比三星、华为等友商手机?",
            "reading" : "11130000",
            "url" : "https://www.zhihu.com/question/345432363",
            "state" : ""
        }
	]
}