简体   繁体   English

MongoDB 不会加载 js 文件

[英]MongoDB won't load js file

I have a file "world.json" imported to "mondes" collection inside "worlds" MongoDB database with the following structure我有一个文件“world.json”导入到“worlds”MongoDB数据库中的“mondes”集合,其结构如下

{
    "_id": ObjectId("5dff5a2d98dfef12b0c590b7"),
    "Code": "BFA",
    "Name": "Burkina Faso",
    "Continent": "Africa",
    "Region": "Western Africa",
    "SurfaceArea": 274000,
    "IndepYear": 1960,
    "Population": 11937000,
    "LifeExpectancy": 46.7,
    "GNP": 2425,
    "GNPOld": 2201,
    "LocalName": "Burkina Faso",
    "GovernmentForm": "Republic",
    "HeadOfState": "Blaise Compaor�",
    "Capital": {
        "ID": 549,
        "Name": "Ouagadougou",
        "District": "Kadiogo",
        "Population": 833000
    },
    "Code2": "BF",
    "Cities": [
        {
            "ID": 550,
            "Name": "Bobo-Dioulasso",
            "District": "Houet",
            "Population": 300000
        },
        {
            "ID": 551,
            "Name": "Koudougou",
            "District": "Boulkiemd�",
            "Population": 105000
        }
    ],
    "NotOffLang": [
        {
            "Language": "Busansi",
            "Percentage": 3.5
        },
        {
            "Language": "Dagara",
            "Percentage": 3.1
        },
        {
            "Language": "Dyula",
            "Percentage": 2.6
        },
        {
            "Language": "Ful",
            "Percentage": 9.7
        },
        {
            "Language": "Gurma",
            "Percentage": 5.7
        },
        {
            "Language": "Mossi",
            "Percentage": 50.2
        }
    ]
}

I wrote the following script "update.js" stored in c:\\$Mongo\\bin我编写了存储在 c:\\$Mongo\\bin 中的以下脚本“update.js”

db.mondes.find({ "Continent": "Africa" }).forEach(
    function (doc) {
        temp = doc.Capital.Population;
        if (doc.Capital.Population > 500000) {
            var temp = temp + 1000;
        }
        else if (doc.Capital.Population < 500000) {
            temp = temp + 500;
        }

        doc.Capital.Population = temp;
        db.mondes.save(doc);
    }
);

When i tried to load("update.js") in mongo's shell, it gave me this error:当我尝试在 mongo 的 shell 中加载(“update.js”)时,它给了我这个错误:

2019-12-22T23:02:46.724+0100 E  QUERY    [js] uncaught exception: TypeError: doc.Capital is undefined :
    @update.js:7:1
    DBQuery.prototype.forEach@src/mongo/shell/query.js:494:9
    @update.js:3:1
    @(shell):1:1
    2019-12-22T23:02:46.724+0100 E  QUERY    [js] Error: error loading js file: update.js :
    @(shell):1:1

Even when I wrote capital with lowercase instead of "Captial" the error above remains.即使我用小写字母而不是“大写字母”写大写字母,上述错误仍然存​​在。

最后我通过使用 ((doc || {}).Capital || {}).Population 而不是 doc.Capital.Population 解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM