初学者-尝试将JSON输出从Node.js保存到MongoDB

时间:2019-05-31 17:08:39

标签: node.js mongodb

我有一个脚本,该脚本在运行时返回JSON。我想将输出保存到MongoDB。随着数据的不断变化,我没有架构设置。

有人可以帮助指导我找到正确的指南/教程来解决这个问题吗?

{
  "urls":{
    "http://example.com/":{
      "status":200
    },
    "https://example.com/":{
      "status":200
    },
    "applications":[
      {
        "name":"Font Awesome",
        "confidence":"100",
        "version":null,
        "icon":"Font Awesome.png",
        "website":"http://fontawesome.io",
        "categories":[
          {
            "17":"Font Scripts"
          }
        ]
      },
      {
        "name":"Google Font API",
        "confidence":"100",
        "version":null,
        "icon":"Google Font API.png",
        "website":"http://google.com/fonts",
        "categories":[
          {
            "17":"Font Scripts"
          }
        ]
      },
      {
        "name":"Google Tag Manager",
        "confidence":"100",
        "version":null,
        "icon":"Googl                                                                                      e Tag Manager.png",
        "website":"http://www.google.com/tagmanager",
        "categories":[
          {
            "42":"Tag Managers"
          }
        ]
      },
      {
        "name":"Gravatar",
        "confidence":"100",
        "version":n                                                                                      ull,
        "icon":"Gravatar.png",
        "website":"http://gravatar.com",
        "categories":[
          {
            "19":"Miscellaneous"
          }
        ]
      },
      {
        "name":"Lightbox",
        "confidence":"100",
        "version":null,
        "                                                                                      icon":"Lightbox.png",
        "website":"http://lokeshdhakar.com/projects/lightbox2/",
        "categories":[
          {
            "59":"JavaScript Libraries"
          }
        ]
      },
      {
        "name":"Nginx",
        "confidence":"100",
        "version":null,
        "icon":"Nginx.svg",
        "website":"http://nginx.org/en",
        "categories":[
          {
            "22":"Web Servers"
          },
          {
            "64":"Reverse Proxy"
          }
        ]
      },
      {
        "name":"Revslider                                                                                      ",
        "confidence":"100",
        "version":"5.4.8.1",
        "icon":"revslider.png",
        "website":"https://revolution.themepunch.com/",
        "categories":[
          {
            "19":"Miscellaneous"
          }
        ]
      },
      {
        "name":"WordPress",
        "confidence":"100",
        "version":"5.1.1",
        "icon":"WordPress.svg",
        "website":"https://wordpress.org",
        "categories":[
          {
            "1":"CMS"
          },
          {
            "11":"Blogs                                                                                      "
          }
        ]
      },
      {
        "name":"Yoast SEO",
        "confidence":"100",
        "version":"10.0",
        "icon":"Yoast SEO.png",
        "website":"http://yoast.com",
        "categories":[
          {
            "54":"SEO"
          }
        ]
      },
      {
        "name":"                                                                                      animate.css",
        "confidence":"100",
        "version":null,
        "icon":"default.svg",
        "website":"https://daneden.github.io/animate.css/",
        "categories":[
          {
            "18":"Web Framewo                                                                                      rks"
          }
        ]
      },
      {
        "name":"prettyPhoto",
        "confidence":"100",
        "version":null,
        "icon":"prettyPhoto.png",
        "website":"http://no-margin-for-errors.com/projects/prettyphot                                                                                      o-jquery-lightbox-clone/",
        "categories":[
          {
            "59":"JavaScript Libraries"
          }
        ]
      },
      {
        "name":"PHP",
        "confidence":"0",
        "version":null,
        "icon":"PHP.svg",
        "website":"http:                                                                                      //php.net",
        "categories":[
          {
            "27":"Programming Languages"
          }
        ]
      },
      {
        "name":"MySQL",
        "confidence":"0",
        "version":null,
        "icon":"MySQL.svg",
        "website":"http://mysql.co                                                                                      m",
        "categories":[
          {
            "34":"Databases"
          }
        ]
      },
      {
        "name":"jQuery",
        "confidence":"0",
        "version":null,
        "icon":"jQuery.svg",
        "website":"https://jquery.com",
        "categories":[
          {
            "59":"JavaScript Libraries"
          }
        ]
      }
    ],
    "meta":{
      "language":null
    }
  }

1 个答案:

答案 0 :(得分:0)

// Example for 'applications' array from your JSON collection in database
// You need to replace url, DB collection name (applications in example), put your real json after 'var applications'

var url = "REPLACE_YOUR_DB_URL"; // depends on your setup, for example: mongodb://localhost:27017/newdb";

// Create a client to mongodb
var MongoClient = require('mongodb').MongoClient;

// Make client connect to MongoDB service
MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    // db pointing to newdb
    console.log("Switched to "+db.databaseName+" database");

    // Reaplce with your JSON
    var applications = [{ "name":"Font Awesome" },
                { "name":"Google Font API" }];

    // Insert multiple documents to 'applications' collection using insertOne
    db.collection("applications").insertMany(applications, function(err, res) {
        if (err) throw err;
        console.log(res.insertedCount+" applications inserted");
        // Close the connection to DB
        db.close();
    });
});

文档: