Home > Web Front-end > JS Tutorial > body text

node.js operation MongoDB instance sharing

小云云
Release: 2018-01-25 10:52:44
Original
1434 people have browsed it

This article mainly introduces the relevant information about the detailed examples of node.js operating MongoDB. I hope it can help everyone through instinct and let everyone understand and master this part of the content. Friends in need can refer to it. I hope it can help everyone.

When node.js operates MongoDB, you need to install the mongodb package

1. Use npm to install cnpm


npm install -g cnpm --registry=https://registry.npm.taobao.org
Copy after login

2. Use cnpm to install the mongodb package


cnpm install mongodb
Copy after login

node.js operation MongoDB instance sharing

node.js operation MongoDB instance sharing

##Four steps when operating MongoDB with node.js Methods: insert data, update data, delete data, search data.

Operation steps

Step 1 Create the executable file xx.js
Step 2 The terminal calls the executable file node xx.js

Note:

You need to start the server before the operation
At the same time, you need to set the operation database and operation collection

node.js operation MongoDB instance sharing

node.js operation MongoDB instance sharing

node.js operation MongoDB instance sharing

node.js operation MongoDB instance sharing

node.js operation MongoDB instance sharing

1. Insert data


var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/col';

var writeData = function(db, callback) {
  // 连接到集合
  var collection = db.collection('person');
  // node.js operation MongoDB instance sharing
  var data = [{'name':'20170906','age':'22'}];
  collection.insert(data, function(error, result) {
    if (error) {
      console.log('error:' + error);
      return;
    };

    callback(result);
  });
}

MongoClient.connect(DB_CONN_STR, function(error, db) {
  console.log('连接成功');
  writeData(db, function(result) {
    console.log(result);
    db.close();
  })
})
Copy after login

2. Update data


var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/col';

var updateData = function(db, callback) {
  // 连接到集合
  var collection = db.collection('person');
  // 修改数据
  var where = {'name':'20170906'};
  var update = {$set:{'age':'33'}};
  collection.update(where, update, function(error, result) {
    if (error) {
      console.log('error:' + error);
      return;
    };

    callback(result);
  });
}

MongoClient.connect(DB_CONN_STR, function(error, db) {
  console.log('连接成功');
  updateData(db, function(result) {
    console.log(result);
    db.close();
  })
})
Copy after login

3. Delete data


var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/col';

var removeData = function(db, callback) {
  // 连接到集合
  var collection = db.collection('person');
  // node.js operation MongoDB instance sharing
  var where = {'age':'22'};
  collection.remove(where, function(error, result) {
    if (error) {
      console.log('error:' + error);
      return;
    };

    callback(result);
  });
}

MongoClient.connect(DB_CONN_STR, function(error, db) {
  console.log('连接成功');
  removeData(db, function(result) {
    console.log(result);
    db.close();
  })
})
Copy after login

4. Search data


var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/col';

var readData = function(db, callback) {
  // 连接到集合
  var collection = db.collection('person');
  // 查询数据
  var where = {'name':'20170906'};
  collection.find(where).toArray(function(error, result) {
    if (error) 
    {
      console.log('error:' + error);
      return;
    };

    callback(result);
  });
}

MongoClient.connect(DB_CONN_STR, function(error, db) {
  console.log('连接成功');
  readData(db, function(result) {
    console.log(result);
    db.close();
  })
})
Copy after login
Related recommendations:

tp5 operates mongoDB database instance

MongoDB singleton mode instance operation sharing implemented by php

Completely master the add, delete, modify and query functions of nodejs to operate mongodb

The above is the detailed content of node.js operation MongoDB instance sharing. For more information, please follow other related articles on the PHP Chinese website!

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