fluent queries for mongodb using promises
$ npm install minq
var minq = require('minq')
minq.connect(connectionString)
minq
.from('foo')
.where({name: minq.like('John')})
.select(['name', 'email', 'homeAddress.zipCode'])
.limit(1000)
.toArray()
minq
.from('foo')
.skip(20)
.limit(50)
.sort('name')
.toArray()
minq
.from('foo')
.stream()
minq
.from('foo')
.where({email: /lol\.com$/)
.count()
minq
.from('foo')
.insert({name: 'Melissa', email: 'm@m.com'})
minq
.drop('foo')
minq queries are contstructed starting with a db and collection, then by adding various options and constraints, and ending with a finalizer. Finalizers return a Q promise.
For .one
and .toArray
, an .expect(number)
option can be used. If the query does not match the expected number, the promise will be rejected.
Read Finalizers are: .toArray
.one
.stream
.count
.assertExists
.checkExists
Note, .stream
returns a node Stream, not a promise
Mutator Finalizers are: .insert
.update
.upsert
.remove
.removeAll
.pull
.findAndModify
Uses jsig notation.
where db is a mongodb db connection object
builds a RegExp for use with a where clause, minq.like
helps by escaping characters for you. It creates a case-insensitive regex. See like
Set the default connection for minq to use. connectionString
should be a MongoDb uri
type MinqDb : {
$collectionName: Query
}
MinqDb is an object with property getters for each collection, similar to the db
object in mongoshell
.
Example:
minq.connect(cs).then(function (db) {
return db.users.where({email: /@gmail.com$/}).toArray()
})
Returns a promise for an array of strings containing the collection names for the default connection.
Deep clones a query object (most useful before the query has been executed!)
returns a new Query object configured with the collection name.
alias: Query#collection
(optional) query
is a mongodb query object, with standard $
operators
where
can be called multiple times to add multiple query terms. Mongodb joins them with logical AND
, see $and.
(optional) fields
is a mongodb projection object, with keys corresponding to the fields of the document you want to return
(optional) configure any additional options, for example {multi: true}
(optional) by
is a mongodb sort order option.
alias: Query#orderBy
(optional) number
is a Number for the maximum number of documents you want to return.
alias: Query#take
(optional) number
is a Number for the number of documents which otherwise match the query that you want to skip in the result
Read Finalizer. The promise is resolved with the array of documents matching your query or an empty array.
Read Finalizer. The promise is resolved with the document matching your query or null
.
alias: Query#first
, Query#firstOrDefault
. Note, first
does not throw on null, unlike in linq. Think of it as firstOrDefault
.
Thunked Query.toArray
.
Thunked Query.one
. Other Finalizers begin executing a query immediately. This method returns a function which can be called to invoke a query and return a promise of the response. This can be useful for memoized caching and other situations.
Read Finalizer. The stream is a mongo read stream of documents matching your query.
Read Finalizer. Streams the results of a query. If iterator
returns a promise, will await each of the promises,
for example if performing batch updates.
Returns a void Promise to rejoin program execution
once all results have been iterated.
Mutator Finalizer. Insert a document collection. The promise is the inserted object, including _id if assigned by db.
Mutator Finalizer. Update documents in a collection with changes
, a mongodb setter or unsetter. Use with Query.where
or include _id
on the changes
object. The promise is the count of updated documents.
Mutator Finalizer. Create or update a document in a collection with setter
, a mongodb setter. The promise is the count of updated documents.
Mutator Finalizer. Remove documents matching a where
query. The promise is the number of documents removed. Rejected if no where
query is specified.
Mutator Finalizer. Remove all documents in a collection. The promise is the number of documents removed.
Finalizer. Drop an entire collection.
$ npm install
$ npm test
jden jason@denizac.org
MIT. (c) jden jason@denizac.org. See LICENSE.md.