This library is work-in-progress for a client-side MongoDB implementation.
Install using bower
bower install mongostorage
Before we can actually start creating or retrieving documents, we should select the database we wish to use. You should call the use()
method to change the selected database.
var db = new MongoStorage();
db.use('foo_dev');
This will load all the collections from the foo_dev database.
To create a new collection, you should call the createCollection()
method of the database.
var db = new MongoStorage();
db.use('foo_dev');
db.createCollection('users');
This will expose a new user property in the database that can be used to access or modify the data in that collection.
This will drop the current database.
db.dropDatabase();
Retrieves the list of collections.
db.collections();
It's possible to insert document per document or insert multiple documents at once.
db.collection.insert(documents, [function]);
db.users.insert({firstName: 'Foo', name: 'Bar'});
db.users.insert([{firstName: 'Foo 1', name: 'Bar'}, {firstName: 'Foo 2', name: 'Bar'}]);
Retrieves an array of documents that matches the query criteria.
db.collection.find([query, [options]], function);
db.users.find({age: {$gte: 25}}, function(users) {
// All the users with an age greater then or equal to 25
});
db.users.find({age: {$gte: 25}}, {limit: 5}, function(users) {
// The first 5 users with an age greater then or equal to 25
});
db.users.find({age: {$gte: 25}}, {limit: 5, skip: 5}, function(users) {
// The next 5 users with an age greater then or equal to 25
});
Retrieves the first document that matches the query criteria.
db.collection.findOne([query, [options]], function);
db.users.findOne({name: 'Bar', age: {$lt: 24}}, function(user) {
// The first user with an age less then 24
});
db.users.findOne({name: 'Bar', age: {$lt: 24}}, {sort: {age: -1}}, function(user) {
// The oldest user with an age less then 24
});
To remove data out of a collection, you can use the remove()
method. It has an optional query parameter and an optional callback function that will tell you how many records where removed.
db.collection.remove([query], [function]);
db.users.remove();
db.users.remove(function(nr) {
// nr holds the number of documents removed out of the collection
});
db.users.remove({age: {$gt: 25}}, function(nr) {
// nr holds the number of documents removed out of the collection
});
Counts the number of documents in the collection that matches the query criteria.
db.collection.count([query], function);
db.users.count(function(count) {
// count holds the total number of users in the database
});
db.users.count({age: {$gt: 25}}, function(count) {
// count holds the total number of users with an age greater then 25
});
Drops the collection from the database
db.collection.drop([function]);
db.users.drop();
db.users.drop(function() {
// The users collection has been removed
});
For comparison of different type values.
Matches values that are greater than the value specified in the query.
Matches values that are greater than or equal to the value specified in the query.
Matches values that are less than the value specified in the query.
Matches values that are less than or equal to the value specified in the query.
Matches all values that are not equal to the value specified in the query.
Matches any of the values that exist in an array specified in the query.
Matches values that do not exist in an array specified to the query.
Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
Joins query clauses with a logical NOR returns all documents that fail to match both clauses.
Inverts the effect of a query expression and returns documents that do not match the query expression.
- Sam Verschueren [[email protected]]
MIT © Sam Verschueren