Home >Web Front-end >JS Tutorial >Learning about addition, deletion, modification and query in node in mongoDB
The content of this article is about learning about adding, deleting, modifying and checking in nodes in mongoDB. It has a good reference value and I hope it can help friends in need.
First, to use mongoDB in node, you need to install the MongoDB Driver. The command is as follows: npm install mongodb --save
github address: node- mongodb-native
At the same time, remember to enable the mongoDB service.
The following code will insert a document into the tasks collection in the database demodb, and print the document's ID on the console.
const MongoClient = require('mongodb').MongoClient const assert = require('assert') const url = 'mongodb://localhost:27017' const dbName = 'demodb' // 增 MongoClient.connect(url, function(err, client) { assert.equal(null, err) console.log("Connected successfully to server") const db = client.db(dbName) var tasks = db.collection('tasks') // 没有则创建 tasks.insertOne( { "project": "task1", "description": "task1 description." }, {safe: true}, function(err, documents) { if (err) throw err; console.log(documents.insertedId); } ); client.close() })
Run the program and found that the following results were printed on the console
Connected successfully to server 5b59d53ae3d895184824586b
The returned 5b59d53ae3d895184824586b
is the document identifier of MongoDB. It is unique and its The essence is binary JSON (ie BSON). BSON is the main data format used by MongoDB to exchange data. The MongoDB server uses it to exchange data instead of JSON. In most cases it is more space efficient and faster to parse.
The declared {safe: true} indicates that the callback function will not be executed until the database operation is completed.
Note: For convenience, the authorization mode is not enabled here. Therefore, you can add, delete, modify and check at will without the username and password in the login URL. But in the production environment, please remember to turn on the authorization mode.
The following code will find the document whose project is task1 in the tasks collection in the database demodb and delete it.
// 删 MongoClient.connect(url, function(err, client) { assert.equal(null, err) console.log("Connected successfully to server") const db = client.db(dbName) var tasks = db.collection('tasks') tasks.deleteOne( { "project": "task1" }, function(err, result) { assert.equal(err, null); assert.equal(1, result.result.n); console.log("Removed the document"); } ); client.close() })
Note: If there are multiple documents with project task1 in the tasks collection, then only the first day's document found will be deleted.
The following code will find the document whose project is task1 in the tasks collection in the database demodb and update it.
// 改 MongoClient.connect(url, function(err, client) { assert.equal(null, err) console.log("Connected successfully to server") const db = client.db(dbName) var tasks = db.collection('tasks') tasks.updateOne( { "project": "task1" }, { $set: { "project" : "task999" } }, {safe: true}, function(err, result) { assert.equal(err, null); assert.equal(1, result.result.n); console.log("Updated the document"); } ); client.close() })
Note: If the document with project task1 is not found in the tasks collection, the program will throw an assertion error, as follows:
The following code will find all documents in the tasks collection in the database demodb and print them to the console.
// 查 MongoClient.connect(url, function(err, client) { assert.equal(null, err) console.log("Connected successfully to server") const db = client.db(dbName) var tasks = db.collection('tasks') tasks.find().toArray((err, docs) => { console.log(docs) assert.equal(null, err) // err 不等于null, 则在控制台打印err // assert.equal(3, docs.length) // 记录不等于3条, 则在控制台打印记录条数 }) client.close() })
The find() method finds all documents, toArray() converts the results into array form, and runs the program. The results are as follows:
Although the above four small programs are slightly simple, you can't reach a thousand miles without taking small steps. If you really master the basic addition, deletion, modification and search, then it is only a matter of time before you master complex applications. .
Related recommendations:
Introduction to users and permissions in mongoDB
The above is the detailed content of Learning about addition, deletion, modification and query in node in mongoDB. For more information, please follow other related articles on the PHP Chinese website!