Nodejs mongo-lite.js -- simple and compact API for MongoDB
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){}) })
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:
- GitHub Repository: https://github.com/alexeypetrushin/mongo-lite
- Documentation: http://alexeypetrushin.github.io/mongo-lite/docs/index.html
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得不偿失, 如果为了日后维护看懂, 直接在源代码注释里写上数据结构就行