Recently, it's time to start using MongoDB to store JSON model files, and then you can implement online editing and management of model files. This morning implemented the JSON file storage code, as follows:
varFs=require ("FS");varMongoclient = require (' MongoDB '). mongoclient;varDbname= "Tbobjects";varurl = "mongodb://localhost:27017/" +DbName; Mongoclient.connect (URL,function(err, db) {if(ERR)Throwerr; Console.log ("Database" +dbname+ "created!"); varDbase=db.db (dbName); //determine whether a collection exists //* varColname= "Models"; Dbase.listcollections ({name:colname}). Next (function(Err, collinfo) {if(collinfo) {//The collection existsConsole.log ("collection" +colname+ "already exists"); } Else{Console.log ("Collection" +colname+ "does not exist"); } }); //*/ //* //creates a collection that does not produce an exception even if the collection already existsDbase.createcollection (ColName,function(err,res) {if(ERR)Throwerr; Console.log ("Create Collection" +colname+ "Success"); //Insert Tbobjects file varFilename= "e:\\ Data Warehouse. Tbobjectsjson"; Console.log ("Read JSON file:" +fileName); varFilecontent=Fs.readfilesync (fileName); if(filecontent) {Console.log ("Filecontent. len=" +filecontent.length); //Write to Database vartbfile=Json.parse (filecontent); Dbase.collection (colname). Insertone (Tbfile,function(err,res) {if(ERR)Throwerr; Console.log ("Tbfile file was successfully written to the database"); Db.close (); }) } }) });
Nodejs read JSON file, write to MongoDB database