是否可以从javascript文件将数据加载到mongodb?如果是这样的话?

时间:2017-09-13 01:45:46

标签: javascript mongodb

我正在关注一个稍微旧一点的教程,该教程要求使用以下命令将javascript文件加载到mongo数据库中。

babel-node loadTestData.js

结果如下:

  

您错误地安装了babel软件包,这是一个无操作的软件包   巴贝尔6。

     

Babel的CLI命令已从babel包移到   babel-cli包。

npm uninstall babel
npm install --save-dev babel-cli

有关设置说明,请参阅http://babeljs.io/docs/usage/cli/

我试过./node_modules/babel-cli/bin/babel-node.js loadTestData.js但没有雪茄。相反,我得到了这个错误。

url.js:88
throw new TypeError('Parameter "url" must be a string, not ' + typeof url);
^

TypeError: Parameter "url" must be a string, not undefined
    at Url.parse (url.js:88:11)
    at Object.urlParse [as parse] (url.js:82:5)
    at module.exports (/Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/my-fullstack-javascript-app/node_modules/mongodb/lib/url_parser.js:16:23)
    at connect (/Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/my-fullstack-javascript-app/node_modules/mongodb/lib/mongo_client.js:486:16)
    at Function.MongoClient.connect (/Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/my-fullstack-javascript-app/node_modules/mongodb/lib/mongo_client.js:250:3)
    at Object.<anonymous> (/Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/my-fullstack-javascript-app/loadTestData.js:5:13)
    at Module._compile (module.js:570:32)
    at loader (/Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/my-fullstack-javascript-app/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/antonio-pavicevac-ortiz/Dropbox/developer_folder/my-fullstack-javascript-app/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:487:32)

这是文件:

import { MongoClient } from 'mongodb';
import assert from 'assert';
import config from './config';

MongoClient.connect(config.mongodbUri, (err, db) => {
  assert.equal(null, err);

  db
    .collection('contests')
    .insertMany([
      {
        id: 1,
        categoryName: 'Business/Company',
        contestName: 'Cognitive Building Bricks',
        description: `
This product is a classroom tool that scaffolds higher order thinking. Its a collaborative strategy that using building bricks to help structure students ideas. Learners build knowledge structures with information (attached to different coloured bricks). Students desks are turned into workshops where they physically manipulate information into meaningful creations. They show sequences of information (like stories), rank information by importance and pretty much all other essential cognitive skills you need at school. The end result is clarity in thought and better collaborative conversations. I want this to be marketed as a sophisticated knowledge tool applicable to all ages and subjects. It gives students the cognitive edge, they get a little more 'RAM'!.

I want to continue with the construction/building theme as well as the mind/brain/learning theme. They need to be blended somehow. Teachers find it easier to talk about building/scaffolding analogies as its less abstract.
      `,
        nameIds: [101, 102]
      },
      {
        id: 2,
        categoryName: 'Magazine/Newsletter',
        contestName: 'Educating people about sustainable food production',
        description: `
Educating people about sustainable food production
      `,
        nameIds: []
      },
      {
        id: 3,
        categoryName: 'Software Component',
        contestName: 'Big Data Analytics for Cash Circulation',
        description: `
Data is created at every touch point in a notes life-cycle. Because of the volume of the data, it can be difficult to store, analyse and gain insight. Collecting, processing and analysing the data using big data technologies and displaying the results in an interactive display makes it easy to make informative decisions, overcome problem and plan for the future.

It works using big data technologies and displays the results in modern browsers, combining powerful visualisation components and a data-driven approach to interact with the data.

It enables you to analyse data that were not previously possible. The volume, variety, complexity of the analytical processing involved, and the responsiveness required are now achievable with the product. Gaining smarter decision making but also provide faster time to value.
    `,
        nameIds: [103, 104, 105]
      },
      {
        id: 4,
        categoryName: 'Website',
        contestName: 'Free programming books',
        description: `
A list of free online programming books, categorized by languages/topics
    `,
        nameIds: []
      }
    ])
    .then(response => {
      console.info('Contests', response.insertedCount);
      db
        .collection('names')
        .insertMany([
          { id: 101, name: 'Mind Assembly', timestamp: new Date() },
          { id: 102, name: 'Brain Scaffold', timestamp: new Date() },
          { id: 103, name: 'Cash View', timestamp: new Date() },
          { id: 104, name: 'Currency Map', timestamp: new Date() },
          { id: 105, name: 'Cash Board', timestamp: new Date() },
          { id: 106, name: 'RootLib', timestamp: new Date() }
        ])
        .then(response => {
          console.info('Names', response.insertedCount);
          db.close();
        });
    });
});

教师让它工作,因为他然后进入mongo shell并获得此命令:

`db.contests.count()` // returns 4

可以这样做吗?

更新 这是我的配置......

const env = process.env;

const nodeEnv = env.NODE_ENV || 'development';

const logStars = function(message) {
  console.info('**********');
  console.info(message);
  console.info('**********');
};

const config = {
  mongodbUri: 'mongodb://localhost:27017/test',
  port: env.PORT || 8016,
  host: env.HOST || 'localhost',
  get serverUrl() {
    return 'http://localhost:8016';
  }
};

module.exports = {
  nodeEnv,
  logStars,
  config
};

0 个答案:

没有答案