📅  最后修改于: 2023-12-03 15:17:50.791000             🧑  作者: Mango
NeDB is a lightweight, embedded, in-memory database written in JavaScript for Node.js applications. It provides a flexible and easy-to-use API that allows developers to quickly implement persistent storage for their applications.
NeDB is open-source and licensed under the MIT license. It provides full CRUD (Create, Read, Update, Delete) functionality and supports both synchronous and asynchronous operations.
To use NeDB in your Node.js application, you can install it using npm:
npm install nedb
The first step in using NeDB is to create a new instance of the Datastore
class. You can do this by calling the Datastore
constructor and passing in an options object:
const Datastore = require('nedb');
const options = {
filename: 'data.db',
autoload: true
};
const db = new Datastore(options);
This creates a new Datastore
instance that will store its data in a file named data.db
and will automatically load the data from the file when the application starts.
Once you have a Datastore
instance, you can use its methods to perform CRUD operations. For example, to insert a new document into the database:
const doc = {
name: 'John Doe',
email: 'john.doe@example.com'
};
db.insert(doc, function(err, newDoc) {
if (err) {
console.error(err);
} else {
console.log('Inserted new document:', newDoc);
}
});
This will insert a new document into the database and return the inserted document with an additional _id
property.
To query the database, you can use the find
method:
db.find({ name: 'John Doe' }, function(err, docs) {
if (err) {
console.error(err);
} else {
console.log('Found documents:', docs);
}
});
This will return an array of all documents in the database that match the query. You can also use other methods like findOne
, count
and update
to perform different types of queries and operations.
NeDB supports a flexible and powerful querying API that allows you to search for documents based on different criteria. Here are some of the most commonly used querying methods:
db.find(query, projection, callback)
: Returns all documents that match the query. You can optionally specify a projection object to limit which fields are returned.
db.findOne(query, projection, callback)
: Returns the first document that matches the query. You can also specify a projection object.
db.count(query, callback)
: Returns the number of documents that match the query.
$lt
: Less than.$lte
: Less than or equal to.$gt
: Greater than.$gte
: Greater than or equal to.$ne
: Not equal to.db.find({ age: { $gt: 18 } }, function(err, docs) {
// Returns all documents with age greater than 18.
});
$or
: Logical OR.$and
: Logical AND.$not
: Logical NOT.$in
: Matches any of the values in an array.$nin
: Matches none of the values in an array.db.find({ $or: [{ name: 'John Doe' }, { age: { $lt: 30 } }] }, function(err, docs) {
// Returns all documents with name "John Doe" or age less than 30.
});
db.find({ name: /doe/i }, function(err, docs) {
// Returns all documents with a name that matches the regular expression /doe/i.
});
NeDB is a great choice for developers who need an embedded, lightweight database for their Node.js applications. It is easy to use, flexible, and provides a powerful querying API. Give it a try today and see how it can simplify your data storage needs!