Nodejs mongo-lite.js -- simple and compact API for MongoDB

发布于 1周前 作者 nodeper 来自 nodejs/Nestjs

Nodejs mongo-lite.js – simple and compact API for MongoDB

MongoDB Driver with simple and compact API, it also eliminates some callbacks. Install it with npm install mongo-lite, examples are in /examples folder. The project hosted on GitHub You can report bugs and discuss features on the Issues page. http://alexeypetrushin.github.com/mongo-lite/docs/index.html

第一眼看去觉得挺不错的, 都是新手常用的 API 接口,

var db = require('mongo-lite').connect('mongodb://localhost/test', ['posts', 'comments'])
db.posts.insert({title: 'first'}, function(err, doc){})

db.collection(‘posts’).insert({title: first’}, function(err, doc){}) db.posts = db.collection(‘posts’) db.posts.insert({title: first’}, function(err, doc){})

db.posts.insert({title: first’}, function(err, doc){ doc.title = second db.posts.update({_id: doc._id}, doc, function(err){}) })


7 回复

Nodejs mongo-lite.js – simple and compact API for MongoDB

MongoDB Driver with a simple and compact API, which also eliminates some callbacks. It is particularly useful for beginners who are getting started with MongoDB.

Installation

You can easily install mongo-lite using npm:

npm install mongo-lite

Project Details

The project is hosted on GitHub. You can report bugs and discuss features on the Issues page:

Example Code

Let’s take a look at some basic operations using mongo-lite. The first example shows how to connect to the MongoDB database and perform basic insertions:

// Import the mongo-lite module
var db = require('mongo-lite').connect('mongodb://localhost/test', ['posts', 'comments']);

// Insert a document into the 'posts' collection
db.posts.insert({ title: 'first' }, function (err, doc) {
    if (err) {
        console.error('Error inserting document:', err);
    } else {
        console.log('Document inserted successfully:', doc);
    }
});

Alternatively, you can use the collection method to access the collection and perform the same operation:

// Access the 'posts' collection via the collection method
db.collection('posts').insert({ title: 'first' }, function (err, doc) {
    if (err) {
        console.error('Error inserting document:', err);
    } else {
        console.log('Document inserted successfully:', doc);
    }
});

You can also assign the collection directly to a variable for easier access:

// Assign the 'posts' collection to a variable
db.posts = db.collection('posts');

// Insert a document into the 'posts' collection using the assigned variable
db.posts.insert({ title: 'first' }, function (err, doc) {
    if (err) {
        console.error('Error inserting document:', err);
    } else {
        console.log('Document inserted successfully:', doc);
    }
});

Update Operation

To update an existing document, you can find the document by its _id and then update it:

// Insert a new document
db.posts.insert({ title: 'first' }, function (err, doc) {
    if (err) {
        console.error('Error inserting document:', err);
    } else {
        console.log('Document inserted successfully:', doc);

        // Update the document
        doc.title = 'second';
        db.posts.update({ _id: doc._id }, doc, function (err) {
            if (err) {
                console.error('Error updating document:', err);
            } else {
                console.log('Document updated successfully');
            }
        });
    }
});

Conclusion

mongo-lite provides a straightforward and compact API that simplifies working with MongoDB in Node.js applications. It eliminates some of the boilerplate code associated with callbacks, making it ideal for beginners or those looking for a more streamlined experience.

For detailed documentation and more advanced usage, refer to the official documentation: http://alexeypetrushin.github.io/mongo-lite/docs/index.html


我用的是 mongode 非常轻量

我对没有提供清晰文档的模块还是很本能在排斥啊, 主要是没到能读源码掌握接口的水平

表示没有ORM的数据库框架我就不想看了。。。

ORM 有多重要呢?

ORM 不是 noSQL 必须的, 小应用上ORM得不偿失, 如果为了日后维护看懂, 直接在源代码注释里写上数据结构就行

mongo-lite.js 是一个简化版的 MongoDB Node.js 驱动库,旨在提供更简洁、更紧凑的 API,并且减少回调函数的使用。以下是如何使用 mongo-lite.js 的一些示例代码和说明:

安装

首先,你需要安装 mongo-lite 包:

npm install mongo-lite

基本用法

你可以通过以下方式连接到 MongoDB 数据库,并进行基本的操作,如插入和更新文档。

连接数据库

const mongoLite = require('mongo-lite');

// 连接到本地的 MongoDB 实例上的 "test" 数据库,并指定集合 "posts" 和 "comments"
let db = mongoLite.connect('mongodb://localhost/test', ['posts', 'comments']);

插入文档

// 在 "posts" 集合中插入一条文档
db.posts.insert({ title: 'first' }, function (err, doc) {
  if (err) throw err;
  console.log('Inserted document:', doc);
});

更新文档

// 修改已插入文档的标题
db.posts.insert({ title: 'first' }, function (err, doc) {
  if (err) throw err;

  // 修改文档的标题并保存更改
  doc.title = 'second';
  db.posts.update({ _id: doc._id }, doc, function (err) {
    if (err) throw err;
    console.log('Updated document:', doc);
  });
});

使用 collection 方法

你也可以通过 collection 方法来操作特定集合:

let postsCollection = db.collection('posts');
postsCollection.insert({ title: 'first' }, function (err, doc) {
  if (err) throw err;
  console.log('Inserted document:', doc);
});

示例总结

以上代码展示了如何使用 mongo-lite.js 连接到 MongoDB 数据库,并执行基本的插入和更新操作。它简化了 API 的使用,并减少了不必要的回调函数,使得代码更加易读和易于维护。

希望这些示例对你有所帮助!如果你有任何具体问题或需要进一步的帮助,请随时提问。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!