Php 是否可以在MongoDB中插入并使用JSON格式的javascript变量?

Php 是否可以在MongoDB中插入并使用JSON格式的javascript变量?,php,javascript,jquery,json,mongodb,Php,Javascript,Jquery,Json,Mongodb,我正试图格式化一个巨大的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

我正试图格式化一个巨大的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);

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

通过将它们放在单个数组中,然后可以调用MongoDB的
insert
方法一次插入大量文档。回调是指文档(设置了它们的
\u id
字段)。如果您有数千个文档,您可能希望使用JavaScript的数组
切片
将数组分成更小的块,这样就不会因为插入的
太大而导致驱动程序超时

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解码
()功能来加载json,然后使用MongoDB PHP驱动程序插入文档。您需要将JSON文件的语法更改为纯语法。例如,文件中不能有变量声明

可能是这样的:

$bigJsonFile = file_get_contents('./variables.json', false)
$bigJsonObject = json_decode($json)
$collection->insert($bigJsonObject)

这是一次性的事情还是你经常想做的事情?这对这个项目来说似乎是可行的,即使它有点奇怪,如果这是你的意思的话。这些变量有上万个。不,您需要经常使用JSON语法插入吗?如果没有,我建议您使用Node.JS以JSON格式插入数据,然后使用PHP进行查询。我只需要插入一次,但需要经常查询。我只需要Mongo来存储变量,而不允许一次下载所有变量。我不确定将查询作为javascript变量传递给jquery的语法。