简介
MongoDB 是一个基于文档存储的 NoSQL 数据库,它以高性能、易扩展、高可用性和灵活性为特点。在前端开发中,我们常常需要使用 MongoDB 存储数据,因此学习 MongoDB 是非常有必要的。
本文将介绍 MongoDB 的基本概念和常用 API 操作,帮助读者快速入门 MongoDB。
安装 MongoDB
首先,我们需要安装 MongoDB。MongoDB 可以在官网下载安装包进行安装,也可以使用包管理器进行安装。这里以 Ubuntu 为例,使用包管理器进行安装:
sudo apt-get update sudo apt-get install mongodb
安装完成后,启动 MongoDB:
sudo service mongodb start
连接 MongoDB
连接 MongoDB 的方式有两种:使用命令行工具 mongo,或使用 MongoDB 的 Node.js 驱动程序。
使用命令行工具
在命令行中输入以下命令连接 MongoDB:
mongo
连接成功后,将进入 MongoDB 的 Shell 界面,可以进行数据库操作。
使用 Node.js 驱动程序
在 Node.js 中,我们可以使用 mongodb 模块来连接 MongoDB。首先,安装 mongodb 模块:
npm install mongodb --save
然后,在代码中连接 MongoDB:
// www.javascriptcn.com code example
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
const db = client.db(dbName);
client.close();
});创建集合
在 MongoDB 中,集合相当于关系型数据库中的表。我们可以使用 createCollection() 方法创建集合。
// www.javascriptcn.com code example
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
const db = client.db(dbName);
// Create a collection
db.createCollection("users", function(err, res) {
console.log("Collection created");
client.close();
});
});插入文档
在 MongoDB 中,文档相当于关系型数据库中的行。我们可以使用 insertOne() 或 insertMany() 方法插入文档。
// www.javascriptcn.com code example
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
const db = client.db(dbName);
// Insert a single document
db.collection("users").insertOne({
name: "John",
age: 30
}, function(err, res) {
console.log("Document inserted");
client.close();
});
// Insert multiple documents
db.collection("users").insertMany([{
name: "Jane",
age: 25
}, {
name: "Bob",
age: 35
}], function(err, res) {
console.log("Documents inserted");
client.close();
});
});查询文档
在 MongoDB 中,我们可以使用 find() 方法查询文档。find() 方法返回一个游标对象,我们可以使用 toArray() 方法将游标转换成数组。
// www.javascriptcn.com code example
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
const db = client.db(dbName);
// Find all documents
db.collection("users").find().toArray(function(err, result) {
console.log(result);
client.close();
});
// Find documents with a condition
db.collection("users").find({ age: { $gt: 30 } }).toArray(function(err, result) {
console.log(result);
client.close();
});
});更新文档
在 MongoDB 中,我们可以使用 updateOne() 或 updateMany() 方法更新文档。
// www.javascriptcn.com code example
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
const db = client.db(dbName);
// Update a single document
db.collection("users").updateOne({ name: "John" }, { $set: { age: 35 } }, function(err, res) {
console.log("Document updated");
client.close();
});
// Update multiple documents
db.collection("users").updateMany({ age: { $lt: 30 } }, { $set: { age: 30 } }, function(err, res) {
console.log("Documents updated");
client.close();
});
});删除文档
在 MongoDB 中,我们可以使用 deleteOne() 或 deleteMany() 方法删除文档。
// www.javascriptcn.com code example
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
const db = client.db(dbName);
// Delete a single document
db.collection("users").deleteOne({ name: "John" }, function(err, res) {
console.log("Document deleted");
client.close();
});
// Delete multiple documents
db.collection("users").deleteMany({ age: { $lt: 30 } }, function(err, res) {
console.log("Documents deleted");
client.close();
});
});结语
本文介绍了 MongoDB 的基本概念和常用 API 操作,希望读者可以通过本文快速入门 MongoDB。需要注意的是,MongoDB 的操作语法与传统的关系型数据库有所不同,需要适应一段时间。在实际使用中,我们还需要了解 MongoDB 的其他高级特性和使用技巧。
Source: FunTeaLearn,Please indicate the source for reprints https://funteas.com/post/67953507504e4ea9bdadeb76