Nodejs 写站点,用啥数据库占用资源最小?

Nodejs 写站点,用啥数据库占用资源最小?

编程还要简单点的:) 求数据库, 不要说是SQLite,貌似这货数据库很容易坏掉的:(

9 回复

当然可以!在选择用于 Node.js 应用程序的数据库时,我们通常会考虑以下几个因素:性能、资源消耗、易用性和可靠性。对于希望占用资源最小的情况,我们可以考虑使用一些轻量级的数据库解决方案。

MongoDB

MongoDB 是一个广泛使用的 NoSQL 数据库,它具有高性能和良好的可扩展性。虽然 MongoDB 本身并不是最轻量级的选择,但它提供了丰富的功能和易于集成的特点,使得它成为许多 Node.js 应用程序的首选。

示例代码

const mongoose = require('mongoose');

// 连接到 MongoDB
mongoose.connect('mongodb://localhost:27017/mydatabase', {
    useNewUrlParser: true,
    useUnifiedTopology: true
});

// 定义数据模型
const userSchema = new mongoose.Schema({
    name: String,
    email: String
});

const User = mongoose.model('User', userSchema);

// 插入数据
const newUser = new User({ name: 'John Doe', email: 'john@example.com' });
newUser.save()
    .then(() => console.log('User saved'))
    .catch(err => console.error('Error saving user:', err));

SQLite

尽管你提到 SQLite 可能容易损坏,但它是另一种轻量级的数据库选项。SQLite 是一个嵌入式数据库,不需要单独的服务器进程或系统配置。它非常适合小型应用或者测试环境。

示例代码

const sqlite3 = require('sqlite3').verbose();

let db = new sqlite3.Database(':memory:', (err) => {
    if (err) {
        return console.error(err.message);
    }
    console.log('Connected to the in-memory SQlite database.');
});

db.serialize(() => {
    db.run("CREATE TABLE users (name TEXT, email TEXT)");

    const stmt = db.prepare("INSERT INTO users VALUES(?, ?)");
    stmt.run("John Doe", "john@example.com");
    stmt.finalize();

    db.each("SELECT rowid AS id, name, email FROM users", (err, row) => {
        console.log(row.id + ": " + row.name + ", " + row.email);
    });
});

db.close((err) => {
    if (err) {
        return console.error(err.message);
    }
    console.log('Close the database connection.');
});

总结

  • MongoDB:适合需要高可扩展性和复杂查询的应用。
  • SQLite:适合小规模应用或测试环境,资源占用更少。

根据你的具体需求选择合适的数据库。如果你的应用对资源占用有严格的要求,SQLite 可能是一个不错的选择。


Access

linux。。。

PostgreSQL 吧。 数据库要求不高的话,直接保存txt,哈哈……

postgresql不是挺占资源么?

sqlite? nedb?

nedb是json数据?

针对“Node.js 写站点,用啥数据库占用资源最小?”这个问题,我们可以考虑使用 LevelDBNeDB

LevelDB

LevelDB 是一个由 Google 开发的嵌入式键值存储系统。它非常轻量级且高效,没有复杂的查询功能,适合简单的键值对存储需求。LevelDB 通过 levellevelup 模块在 Node.js 中使用。

示例代码:

const level = require('level');
const db = level('./mydb');

// 写入数据
db.put('key', 'value', function (err) {
  if (err) return console.log('Ooops!', err); // Oops! Something went wrong
});

// 读取数据
db.get('key', function (err, value) {
  if (err) return console.log('Ooops!', err); // Key not found
  console.log(value); // value = "value"
});

NeDB

NeDB 是一个内存数据库,它模仿 MongoDB 的 API,但可以将数据持久化到文件中。它非常适合小型应用或原型开发,因为它不需要任何外部服务器,并且运行时占用资源较少。

示例代码:

const Datastore = require('nedb');
const db = new Datastore({ filename: 'database.db', autoload: true });

// 插入数据
db.insert({ name: 'John' }, function (err, doc) {
  if (err) return console.log(err);
  console.log(doc);
});

// 查询数据
db.find({ name: 'John' }, function (err, docs) {
  if (err) return console.log(err);
  console.log(docs);
});

总结

如果你需要一个简单、轻量级的数据库来存储键值对或文档,那么 LevelDBNeDB 都是不错的选择。它们占用资源少,适合资源受限的环境。其中,LevelDB 更适合简单的键值存储,而 NeDB 则提供了更多的功能,包括查询等,但仍然保持了较低的资源消耗。

回到顶部