MongoDB-Inserting data

Source: Internet
Author: User
Tags mongoclient mongodb install node

Db.use.js

1 /**2 * Before use: Install node environment, Express, MongoDB first.3  * 4 * 1.1 Install node5 * 1.2 npm i-d Express MongoDB && npm I-g Express MongoDB6 * 1.3 db.action: Add, delete, check, change (currently only added as an example), Db.config: The configuration database connection name also has an IP address and port number.7  */8 9Const EXPRESS = require (' Express ');TenConst APP =Express (); OneConst DB = require ('./db.action ')); AConst DBCONFIG = require ('./db.config ')); -  -App.get ('/',function(req, res) { the  -     //inserting more than one data - Db.insertmany (Dbconfig.dbname, [ -{' name ': ' Little Blue ', ' age ': ' Home ': ' Chenzhou ' }, +{' name ': ' Little green ', ' age ': +, ' home ': ' Yongzhou ' }, -{' name ': ' Little Red ', ' age ': ' Home ': ' Beijing ' } +],function(err, result) { A         if(ERR)Throwerr; atRes.send (' Insert succeeded '); -         //db.close (); -     }); -  -}). Listen (80);

Db.config.js

1 //Database name2Let dbname = ' test ';3 4 //IP Address5Let IP = ' 127.0.0.1 ';6 7 //Port number8Let port = 27017;9 Ten  One //ES5 Export. AModule.exports = { -URL: ' mongodb://' + IP + ': ' + port + '/', - Dbname:dbname the } -  - //ES6 Export - //export {url,db};

Db.action.js

1 //Import {url,db} from './db.config ' ES6;2(function () {3Const Mongoclient = require (' MongoDB '). mongoclient;4Const DBCONFIG = require ('./db.config '));5 6     function_connectdb (callback) {7Let Dburl =Dbconfig.url;8Let dbname =Dbconfig.dbname;9         //Link DatabaseTenMongoclient.connect (Dburl + dbname,function(err, db) { One callback (err, DB); A         }); -     } -  the     //inserting A single piece of data -Exports.insertone =function(CollectionName, data, callback) { -_connectdb (function(err, db) { -  +             if(err) { - callback (err, DB); +                 return; A             } at  -db =db.db (collectionname); -  -Db.collection (Db.databasename). Insertone (Data,function(err, result) { - callback (err, result); -                 //Db.close (); Close the database in             }); -         }); to     } +  -  the     //inserting more than one data *Exports.insertmany =function(CollectionName, data, callback) { $_connectdb (function(err, db) {Panax Notoginseng             if(err) { - callback (err, DB); the                 return; +             } A  thedb =db.db (collectionname); +  -Db.collection (CollectionName). Insertmany (Data,function(err, result) { $ callback (err, result); $                 //Db.close (); Close the database -             }); -         }); the     } -})()

MongoDB-Inserting data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.