0

我正在尝试格式化一个巨大的 js 文件,以便我可以将它插入到 MongoDB 集合中并通过 AJAX 读取它,但我遇到了语法问题。它是这样的:

var MyVariable1 = [ { 
  ThingsToGet: [ 
  { 
     first_thing: "SOMETHING ONE",
     second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, "x"], [6, "x"]]
     third_thing: 0,
     fourth_thing: []
     },{ 
     first_thing: "SOMETHING TWO",
     second_thing: [[1, 0], [2, 5], [3, 4], [4, 4], [5, "x"], [6, "x"]],
     third_thing: 0,
     fourth_thing: []
     },{ 
     first_thing: "SOMETHING THREE",
     second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, 2], [6, 2]],
     third_thing: 0,
     fourth_thing: []
     },
   ]
   }
 ];


 var MyVariable2 = [ { 
     ThingsToGet: [ etc, etc, etc...

我打算用 PHP 查询它:

$variable_to_send_to_jquery = "MyVariable1";

$filter = array(
    'var'=>"MyVariable1";
);


$results = $collection->findone($filter);
4

1 回答 1

1

如果您有很多预先存在的类似 JSON 的文件要加载(您拥有的语法实际上是 JavaScript),我建议您安装Node.JS并使用如下代码,如果这是一次性的事情。虽然我不了解您的数据模型(并对其进行了一点更改以便编译),但基本上我所做的只是创建一个变量,其中包含您添加的所有“变量”的数组。

通过将它们放在一个数组中,您可以调用insertMongoDB 的方法一次插入大量文档。回调是文档(_id设置了它们的字段)。如果您有数千个文档,您可能希望使用 JavaScript 的数组slice将数组分成更小的块,这样您就不会导致驱动程序超时,而insert.

var mongo = require('mongodb').MongoClient
    , Server = require('mongodb').Server;

var allVariables = [ {
    ThingsToGet: [
        {
            first_thing: "SOMETHING ONE",
            second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, "x"], [6, "x"]],
            third_thing: 0,
            fourth_thing: []
        },{
            first_thing: "SOMETHING TWO",
            second_thing: [[1, 0], [2, 5], [3, 4], [4, 4], [5, "x"], [6, "x"]],
            third_thing: 0,
            fourth_thing: []
        },{
            first_thing: "SOMETHING THREE",
            second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, 2], [6, 2]],
            third_thing: 0,
            fourth_thing: []
        },
    ]
}
,
    {
        ThingsToGet: [
            {
                first_thing: "SOMETHING ONE",
                second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, "x"], [6, "x"]],
                third_thing: 0,
                fourth_thing: []
            },{
                first_thing: "SOMETHING TWO",
                second_thing: [[1, 0], [2, 5], [3, 4], [4, 4], [5, "x"], [6, "x"]],
                third_thing: 0,
                fourth_thing: []
            },{
                first_thing: "SOMETHING THREE",
                second_thing: [[1, 0], [2, 0], [3, 4], [4, 4], [5, 2], [6, 2]],
                third_thing: 0,
                fourth_thing: []
            },
        ]
    }
];


mongo.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
    if(err) throw err;
    var collection = db.collection('variables');

    // you may want to do them in batches rather than all at once
    collection.insert(allVariables, function(err,docs){
        if(err) throw err;
        if(docs) {
            console.log("Docs inserted: " + docs.length)
            // you could do something here with the docs
        }
    });
});

另一种选择是实际使用 PHP 中的json-decode( docs ) 功能来加载 JSON,然后使用 MongoDB PHP 驱动程序插入文档。您需要将 JSON 文件的语法更改为纯JSON。例如,文件中不能有变量声明。

它可能是这样的:

$bigJsonFile = file_get_contents('./variables.json', false)
$bigJsonObject = json_decode($json)
$collection->insert($bigJsonObject)
于 2013-09-01T17:01:28.920 回答