Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Actual combat between Node and Mongodb

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Listening to Chen Hongyu's "ideal Thirty". It sounds good.

The last two blog posts mentioned the installation and connection of Mongodb under Mac, this time let's take a look at how to operate Mongodb through Node.

PS: the data set in this article uses the "mycollection" from the 'test' library' in the previous blog post.

One: install the MongoDB package

To use the mongodb module in Node, you need to install oh ~ open the terminal and enter the following command ~

Npm install mongodb

Two: database connection and disconnection

1: introduce mongodb module

Var mongo = require ("mongodb")

2: create a server object for the MongoDB database

Var server = new mongo.Server (host,port, [options])

Description: host: server address, default local localhost;port: server port number, default 27017 position options: optional configuration parameter.

3: create a db object for MongoDB

Var db = new mongo.Db (databasename,server, [options])

Description: databasename: database name, here we use the "test" database in the previous article; server: server object; options: optional configuration parameters.

4: execute the open method of db and connect to the database

Db.open (callback (err,db))

Description: callback callback method. If the connection fails, an err error will be thrown, and a db object will be returned if the connection to the database is successful.

5: execute the close method of db to disconnect the database

Db.close ()

Note: when you close a database connection, a listening close event will be triggered. This event has two parameters, err and db, which have the same meaning as above.

Function (err,db) {

/ / callback method

}

Attached: code snippet. Save the following code in the testMongo.js file.

Var mongo = require ("mongodb")

Var host = "localhost"

Var port = "27017"

Var server = new mongo.Server (host,port, {auto_reconnect:true})

Var db = new mongo.Db ("test", server, {safe:true})

Db.open (function (err,db) {

If (err) {

Throw err

Console.log ("database connection error")

} else {

Console.log ("successfully establish a database connection")

Db.close ()

}

});

Db.on ("close", function (err,db) {

If (err) {

Throw err

Console.log ("database connection error")

} else {

Console.log ("close database connection")

}

})

Open a new terminal and type "mongod" to open mongodb.

Open a new terminal, type "node testMongo.js", see the following result, it is successful.

TestNode node testMongo.js

Successfully establish a database connection

Close the database connection

Three: data set

MongoDb operates on data collections! The operation of data is the operation of data set.

Db.collection (collectionname, [options], callback (err,collection))

Description: collectionname: the name of the data collection in the database, here is the "mycollection" in the previous section; options: optional configuration parameter. Callback: the callback method of the connection, which will have the err parameter of the connection error and the collecction parameter of the successful connection.

Attached: code snippet. Save the following file to testMongo.js.

Var mongo = require ("mongodb")

Var host = "localhost"

Var port = "27017"

Var server = new mongo.Server (host,port, {auto_reconnect:true})

Var db = new mongo.Db ("test", server, {safe:true})

Db.open (function (err,db) {

If (err) {

Throw err

Console.log ("database connection error")

} else {

Console.log ("successfully establish a database connection")

Db.collection ('mycollection',function (err,collection) {

If (err) {

Throw err

Console.log ("error connecting data set")

} else {

Console.log ("successfully connect data collection")

Db.close ()

}

});

}

});

Db.on ("close", function (err,db) {

If (err) {

Throw err

Console.log ("database connection error")

} else {

Console.log ("close database connection")

}

})

Open a new terminal, type "node testMongo.js", see the following result, it is successful.

TestNode node testMongo.js

Successfully establish a database connection

Successfully connect the data set

Close the database connection

Fourth: MongoDb- increase

The insert method of the data collection to realize the operation of adding data.

Collection.insert (docs, [options], [callback (err,docs)])

Description: docs: data to be inserted; options: optional configuration parameters. Optional callback: the callback method for insertion, the err parameter that is inserted incorrectly and the docs (inserted data) parameter when the insertion is successful.

Attached: code snippet. Save the following file to testMongo.js. -- insert 5 Cailala into the mycollection data collection of the test library

Var mongo = require ("mongodb")

Var host = "localhost"

Var port = "27017"

Var server = new mongo.Server (host,port, {auto_reconnect:true})

Var db = new mongo.Db ("test", server, {safe:true})

Db.open (function (err,db) {

If (err) {

Throw err

Console.log ("database connection error")

} else {

Console.log ("successfully establish a database connection")

Db.collection ('mycollection',function (err,collection) {

If (err) {

Throw err

Console.log ("error connecting data set")

} else {

Console.log ("successfully connect data collection")

/ / insert

For (var I = 1 position I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report