#mquery
===========
`mquery` is a fluent mongodb query builder designed to run in multiple environments. As of v0.1, `mquery` runs on `Node.js` only with support for the MongoDB shell and browser environments planned for upcoming releases.
##Features
- fluent query builder api
- custom base query support
- MongoDB 2.4 geoJSON support
- method + option combinations validation
- node.js driver compatibility
- environment detection
- [debug](https://github.com/visionmedia/debug) support
- separated collection implementations for maximum flexibility
[![Build Status](https://travis-ci.org/aheckmann/mquery.png)](https://travis-ci.org/aheckmann/mquery)
##Use
```js
require('mongodb').connect(uri, function (err, db) {
if (err) return handleError(err);
// get a collection
var collection = db.collection('artists');
// pass it to the constructor
mquery(collection).find({..}, callback);
// or pass it to the collection method
mquery().find({..}).collection(collection).exec(callback)
// or better yet, create a custom query constructor that has it always set
var Artist = mquery(collection).toConstructor();
Artist().find(..).where(..).exec(callback)
})
```
`mquery` requires a collection object to work with. In the example above we just pass the collection object created using the official [MongoDB driver](https://github.com/mongodb/node-mongodb-native).
##Fluent API
###find()
Declares this query a _find_ query. Optionally pass a match clause and / or callback. If a callback is passed the query is executed.
```js
mquery().find()
mquery().find(match)
mquery().find(callback)
mquery().find(match, function (err, docs) {
assert(Array.isArray(docs));
})
```
###findOne()
Declares this query a _findOne_ query. Optionally pass a match clause and / or callback. If a callback is passed the query is executed.
```js
mquery().findOne()
mquery().findOne(match)
mquery().findOne(callback)
mquery().findOne(match, function (err, doc) {
if (doc) {
// the document may not be found
console.log(doc);
}
})
```
###count()
Declares this query a _count_ query. Optionally pass a match clause and / or callback. If a callback is passed the query is executed.
```js
mquery().count()
mquery().count(match)
mquery().count(callback)
mquery().count(match, function (err, number){
console.log('we found %d matching documents', number);
})
```
###remove()
Declares this query a _remove_ query. Optionally pass a match clause and / or callback. If a callback is passed the query is executed.
```js
mquery().remove()
mquery().remove(match)
mquery().remove(callback)
mquery().remove(match, function (err){})
```
###update()
Declares this query an _update_ query. Optionally pass an update document, match clause, options or callback. If a callback is passed, the query is executed. To force execution without passing a callback, run `update(true)`.
```js
mquery().update()
mquery().update(match, updateDocument)
mquery().update(match, updateDocument, options)
// the following all execute the command
mquery().update(callback)
mquery().update({$set: updateDocument, callback)
mquery().update(match, updateDocument, callback)
mquery().update(match, updateDocument, options, function (err, result){})
mquery().update(true) // executes (unsafe write)
```
#####the update document
All paths passed that are not `$atomic` operations will become `$set` ops. For example:
```js
mquery(collection).where({ _id: id }).update({ title: 'words' }, callback)
```
becomes
```js
collection.update({ _id: id }, { $set: { title: 'words' }}, callback)
```
This behavior can be overridden using the `overwrite` option (see below).
#####options
Options are passed to the `setOptions()` method.
- overwrite
Passing an empty object `{ }` as the update document will result in a no-op unless the `overwrite` option is passed. Without the `overwrite` option, the update operation will be ignored and the callback executed without sending the command to MongoDB to prevent accidently overwritting documents in the collection.
```js
var q = mquery(collection).where({ _id: id }).setOptions({ overwrite: true });
q.update({ }, callback); // overwrite with an empty doc
```
The `overwrite` option isn't just for empty objects, it also provides a means to override the default `$set` conversion and send the update document as is.
```js
// create a base query
var base = mquery({ _id: 108 }).collection(collection).toConstructor();
base().findOne(function (err, doc) {
console.log(doc); // { _id: 108, name: 'cajon' })
base().setOptions({ overwrite: true }).update({ changed: true }, function (err) {
base.findOne(function (err, doc) {
console.log(doc); // { _id: 108, changed: true }) - the doc was overwritten
});
});
})
```
- multi
Updates only modify a single document by default. To update multiple documents, set the `multi` option to `true`.
```js
mquery()
.collection(coll)
.update({ name: /^match/ }, { $addToSet: { arr: 4 }}, { multi: true }, callback)
// another way of doing it
mquery({ name: /^match/ })
.collection(coll)
.setOptions({ multi: true })
.update({ $addToSet: { arr: 4 }}, callback)
// update multiple documents with an empty doc
var q = mquery(collection).where({ name: /^match/ });
q.setOptions({ multi: true, overwrite: true })
q.update({ });
q.update(function (err, result) {
console.log(arguments);
});
```
###findOneAndUpdate()
Declares this query a _findAndModify_ with update query. Optionally pass a match clause, update document, options, or callback. If a callback is passed, the query is executed.
When executed, the first matching document (if found) is modified according to the update document and passed back to the callback.
#####options
Options are passed to the `setOptions()` method.
- `new`: boolean - true to return the modified document rather than the original. defaults to true
- `upsert`: boolean - creates the object if it doesn't exist. defaults to false
- `sort`: if multiple docs are found by the match condition, sets the sort order to choose which doc to update
```js
query.findOneAndUpdate()
query.findOneAndUpdate(updateDocument)
query.findOneAndUpdate(match, updateDocument)
query.findOneAndUpdate(match, updateDocument, options)
// the following all execute the command
query.findOneAndUpdate(callback)
query.findOneAndUpdate(updateDocument, callback)
query.findOneAndUpdate(match, updateDocument, callback)
query.findOneAndUpdate(match, updateDocument, options, function (err, doc) {
if (doc) {
// the document may not be found
console.log(doc);
}
})
```
###findOneAndRemove()
Declares this query a _findAndModify_ with remove query. Optionally pass a match clause, options, or callback. If a callback is passed, the query is executed.
When executed, the first matching document (if found) is modified according to the update document, removed from the collection and passed to the callback.
#####options
Options are passed to the `setOptions()` method.
- `sort`: if multiple docs are found by the condition, sets the sort order to choose which doc to modify and remove
```js
A.where().findOneAndRemove()
A.where().findOneAndRemove(match)
A.where().findOneAndRemove(match, options)
// the following all execute the command
A.where().findOneAndRemove(callback)
A.where().findOneAndRemove(match, callback)
A.where().findOneAndRemove(match, options, function (err, doc) {
if (doc) {
// the document may not be found
console.log(doc);
}
})
```
###distinct()
Declares this query a _distinct_ query. Optionally pass the distinct field, a match clause or callback. If a callback is passed the query is executed.
```js
mquery().distinct()
mquery().distinct(match)
mquery().distinct(match, field)
mquery().distinct(field)
// the following all execute the command
mquery().distinct(callback)
mquery().distinct(field, callback)
mquery().distinct(match, callback)
mquery().distinct(match, field, function (err, result) {
没有合适的资源?快使用搜索试试~ 我知道了~
毕设 linux监控系统基于nodejsexpressjsmongoosejs.zip
共709个文件
js:344个
txt:155个
md:30个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 163 浏览量
2023-10-24
11:26:18
上传
评论
收藏 1.33MB ZIP 举报
温馨提示
matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行! matlab算法,毕设、课设程序,全部源码均已进行严格测试,可以直接运行!
资源推荐
资源详情
资源评论
收起资源包目录
毕设 linux监控系统基于nodejsexpressjsmongoosejs.zip (709个子文件)
AUTHORS 366B
kerberosgss.c 21KB
kerberos_sspi.c 7KB
base64.c 4KB
base64.c 4KB
security_context.cc 42KB
bson.cc 33KB
kerberos.cc 20KB
security_credentials.cc 17KB
security_buffer_descriptor.cc 6KB
security_buffer.cc 4KB
kerberos_context.cc 2KB
kerberos.cc 2KB
worker.cc 66B
worker.cc 66B
express.cmd 135B
stylus.cmd 131B
jasmine.css 2KB
style.css 110B
style.css 31B
strict.dtd 34KB
serverStatus.ejs 4KB
comments.ejs 272B
menu.ejs 250B
functions.ejs 183B
list.ejs 128B
newlines.ejs 89B
no.newlines.ejs 89B
include.ejs 79B
messed.ejs 71B
error.ejs 62B
include.css.ejs 61B
double-quote.ejs 40B
item.ejs 38B
fail.ejs 34B
single-quote.ejs 33B
menu-item.ejs 32B
pet.ejs 24B
user.ejs 17B
para.ejs 10B
backslash.ejs 4B
express 278B
.gitattributes 483B
.gitignore 3KB
.gitmodules 256B
.gitmodules 0B
binding.gyp 1KB
binding.gyp 336B
bson.h 11KB
kerberos_sspi.h 4KB
security_context.h 2KB
kerberosgss.h 2KB
security_credentials.h 2KB
kerberos.h 1KB
kerberos.h 1KB
security_buffer.h 1KB
security_buffer_descriptor.h 1KB
kerberos_context.h 1KB
worker.h 798B
base64.h 746B
base64.h 746B
worker.h 734B
LICENSE-W3C.html 12KB
3.html 3KB
json-test.html 3KB
json-test-2.html 3KB
4.html 2KB
json-test-2.html 1KB
7.html 1KB
6.html 1KB
8.html 1KB
5.html 1KB
json-example.html 1KB
flash-variable.html 614B
client.html 588B
index.html 496B
chart.html 326B
test.html 138B
comments.html 107B
menu.html 98B
newlines.html 76B
include.html 76B
no.newlines.html 66B
messed.html 48B
include.css.html 37B
double-quote.html 26B
single-quote.html 24B
backslash.html 4B
jquery.js 242KB
bson.js 153KB
db.js 80KB
index.js 77KB
query.js 75KB
model.js 72KB
collection.js 71KB
jasmine.js 65KB
bson.js 64KB
mquery.js 56KB
index.js 54KB
gridstore.js 52KB
共 709 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
天天501
- 粉丝: 606
- 资源: 4665
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功