简体   繁体   中英

mongodb with nodejs(npm mongodb)

In my application i have to store data to mongodb using nodejs(npm mongodb). I have installed mongodb , nodejs and npm mongodb but the problem here is I installed them in separate folders. I am unable to organize the directory structure properly that's way I am getting errors.If you tell me the folder structure it will help me a lot.

Thanks in advance

Try mongoose - npm install mongoose , mongoose is a ODM for mongodb. API Documentation - http://mongoosejs.com/

Short example from official documentation:

var mongoose = require('mongoose');
mongoose.connect('localhost', 'test');

var schema = mongoose.Schema({ name: 'string' });
var Cat = mongoose.model('Cat', schema);

var kitty = new Cat({ name: 'Zildjian' });
kitty.save(function (err) {
  if (err) // ...
  console.log('meow');
});

Run npm install mongodb command in your application directory. For example, nodejsapp folder is the application directory. The folder structure inside the directory will look like :

nodejsapp
    ├───────────  node_modules <-- All the node modules installed using npm comes here --> 
    |                ├──────────── .bin
    |                ├──────────── express
    |                └──────────── mongodb
    ├───────────  app        <-- holds all our files for node components (models, routes)--> 
    ├───────────  config     <-- all our configuration will be here -->
    ├───────────  public     <-- holds all our files for our front-end application -->
    ├───────────  server.js  <-- Node configuration -->

The node_modules folder is created when you use npm command in the application directory.
This is a better way to organize the node packages for a project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM