Programming operation of mongodb


Mongodb series:

The previous article first introduced the installation of mongodb database system, then introduced the shell tool of mongodb to operate the database, and mastered the four basic operations of adding, deleting, modifying and querying the database from the command line. Now let’s introduce how to operate mongodb database with JavaScript programming language.

Suppose there is a programming task that wants to persistently save the data to the mongodb database in the node.js program. How can such a programming task be realized?

Let’s show the four basic operations of adding, deleting, modifying and querying mongodb database with JavaScript program through specific cases. Of course, before operation, you need to install the node.js third-party module of database operation: mongodb.

npm install mongodb

Then, write the following opt-mongo.js code file.


var MongoClient = require('mongodb').MongoClient,
    assert = require('assert');

var url = 'mongodb://localhost:27017/todo';
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);

  //db.collection('todo').insertOne({"item": "have break"});
  db.collection('todo').updateOne({"item": "have breakfast"}, {"itrem": "have launch"});
  //db.collection('todo').deleteOne({"item": "have break"});
  find(db, function() {db.close();});

function find(db, cb) {
  var cursor = db.collection('todo').find();

  cursor.each(function(err, doc) {
    if(doc !== null) { console.log(doc); } else { cb(); }

Procedure description:

  1. First, reference the mongodb client, through which you can establish a connection with the mongodb server;
  2. Reference the assert assertion module to determine whether the connection is wrong;
  3. Define the variable URL to save the address of mongodb server. Suppose we operate the database of todo to-do list;
  4. Call the connect method of the client to connect to the mongodb database;
  5. The callback function has two parameters. The first parameter is the error object and the second parameter is the database object;
  6. The four operations of add, delete, modify and query are all performed on the database object;
  7. First, define a query function find with two parameters. The first parameter is the database object and the second parameter is the callback function;
  8. In the find function, you will find that the API of JavaScript programming is almost the same as that of mongodb shell;
  9. After the find function is defined, call the find method in the callback of the connection method;
  10. Execute the program to see the effect of database query;
  11. Then, add three statements in front of the query statement: add, delete and change;
  12. Every time you add a statement, run it on the command line to view the effect;
  13. Comment out the statement, and then add the next statement;

Through the above case code, we can see that the programming operation of mongodb database is simple and easy to use. This article only introduces the API of four basic operations: add, delete, change and query. If you want to learn more, please refer to the official documentation of mongodb API. The node.js native API does not have a module for database operations, and third-party modules are used to complete database operations. Another commonly used third-party module is mongoose. You can consult relevant materials by yourself.

If this article is useful to you, please like it and collect it!