node. js combined with MongoDB

Source: Internet
Author: User
Tags install mongodb

First, the preparatory work

1. Create the table to be read in MongoDB

Create a database Mongotest

Use Mongotest;

Inserting data into the user table

Db.user.insert ({name: ' Flyoung ', Age: ' + ', sex:true});

2, Installation Node-mongodb-native

NPM Install MongoDB

Ii. instances (node. js reads MongoDB)

Documentation for reference node-mongodb-native: https://github.com/mongodb/node-mongodb-native

var MongoDB = require (' MongoDB '); var server = new MongoDB. Server ("127.0.0.1", 27017,{});//local 27017 port new MongoDB. db (' Mongotest ', server,{}). Open (function (error,client) {///database: Mongotest    if (error) throw error;    var collection = new MongoDB. Collection (client, ' user ');//table: User    Collection.find (function (error,cursor) {        Cursor.each (Error, DOC) {            if (doc) {                console.log ("Name:" +doc.name+ "Age:" +doc.age);            }        );});    

Run:

Node Mongodbtest.js

Results:

Name:flyoung age:18

Three, written in the last words

The demo reference document for adding and deleting changes

Article Source: node. js combined with MongoDB

node. js combined with MongoDB

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.