Home > Web Front-end > JS Tutorial > node.js operation mongodb learning summary_node.js

node.js operation mongodb learning summary_node.js

WBOY
Release: 2016-05-16 16:02:25
Original
1359 people have browsed it

1. Preparation

1. Create the table to be read in mongodb

Create database mongotest

Copy code The code is as follows:

use mongotest;

Insert data into the user table
Copy code The code is as follows:

db.user.insert({
name:'flyoung',
age:'18',
sex:true
});

2. Install node-mongodb-native
Copy code The code is as follows:

npm install mongodb

2. Example (node.js reads mongodb)

Refer to the documentation of node-mongodb-native: https://github.com/mongodb/node-mongodb-native

Copy code The code is as follows:

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(function(error,doc){
               if(doc){
console.log("name:" doc.name "age:" doc.age);
            }
        });
});
});


Run:
Copy code The code is as follows:

node mongodbTest.js

Result:
Copy code The code is as follows:

name:flyoung age:18

3. The last words

Demo reference document for adding, deleting, modifying and checking

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template