# mquery
`mquery` is a fluent mongodb query builder designed to run in multiple environments.
[![Build Status](https://travis-ci.org/aheckmann/mquery.svg?branch=master)](https://travis-ci.org/aheckmann/mquery)
[![NPM version](https://badge.fury.io/js/mquery.svg)](http://badge.fury.io/js/mquery)
[![npm](https://nodei.co/npm/mquery.png)](https://www.npmjs.com/package/mquery)
## 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
## 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](#find)
- [findOne](#findOne)
- [count](#count)
- [remove](#remove)
- [update](#update)
- [findOneAndUpdate](#findoneandupdate)
- [findOneAndDelete, findOneAndRemove](#findoneandremove)
- [distinct](#distinct)
- [exec](#exec)
- [stream](#stream)
- [all](#all)
- [and](#and)
- [box](#box)
- [circle](#circle)
- [elemMatch](#elemmatch)
- [equals](#equals)
- [exists](#exists)
- [geometry](#geometry)
- [gt](#gt)
- [gte](#gte)
- [in](#in)
- [intersects](#intersects)
- [lt](#lt)
- [lte](#lte)
- [maxDistance](#maxdistance)
- [mod](#mod)
- [ne](#ne)
- [nin](#nin)
- [nor](#nor)
- [near](#near)
- [or](#or)
- [polygon](#polygon)
- [regex](#regex)
- [select](#select)
- [selected](#selected)
- [selectedInclusively](#selectedinclusively)
- [selectedExclusively](#selectedexclusively)
- [size](#size)
- [slice](#slice)
- [within](#within)
- [where](#where)
- [$where](#where-1)
- [batchSize](#batchsize)
- [collation](#collation)
- [comment](#comment)
- [hint](#hint)
- [j](#j)
- [limit](#limit)
- [maxScan](#maxscan)
- [maxTime, maxTimeMS](#maxtime)
- [skip](#skip)
- [sort](#sort)
- [read, setReadPreference](#read)
- [readConcern, r](#readconcern)
- [slaveOk](#slaveok)
- [snapshot](#snapshot)
- [tailable](#tailable)
- [writeConcern, w](#writeconcern)
- [wtimeout, wTimeout](#wtimeout)
## Helpers
- [collection](#collection)
- [then](#then)
- [thunk](#thunk)
- [merge](#mergeobject)
- [setOptions](#setoptionsoptions)
- [setTraceFunction](#settracefunctionfunc)
- [mquery.setGlobalTraceFunction](#mquerysetglobaltracefunctionfunc)
- [mquery.canMerge](#mquerycanmerge)
- [mquery.use$geoWithin](#mqueryusegeowithin)
### 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.fin
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【项目资源】: 包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】: 所有源码都经过严格测试,可以直接运行。 功能在确认正常工作后才上传。 【适用人群】: 适用于希望学习不同技术领域的小白或进阶学习者。 可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】: 项目具有较高的学习借鉴价值,也可直接拿来修改复刻。 对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】: 有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 鼓励下载和使用,并欢迎大家互相学习,共同进步。
资源推荐
资源详情
资源评论
收起资源包目录
毕设&课设&项目&实训-GIS自主学习网的接口部分,其中有毕设两个前端页面的所有使用到的接口.zip (1406个子文件)
.babelrc 27B
range.bnf 619B
semver.cmd 260B
mkdirp.cmd 260B
mime.cmd 254B
.DS_Store 6KB
.editorconfig 399B
.editorconfig 173B
preamble.error 65B
.eslintignore 9B
.eslintignore 5B
.eslintrc 647B
.eslintrc 422B
.eslintrc 219B
.eslintrc 180B
.eslintrc 53B
.gitattributes 13B
part7.header 116B
part6.header 116B
part2.header 110B
part2.header 110B
preamble.header 90B
part3.header 70B
part3.header 70B
part4.header 67B
part4.header 67B
part2.header 62B
part2.header 62B
part5.header 61B
part5.header 61B
part1.header 56B
part1.header 56B
part6.header 55B
part7.header 55B
part1.header 53B
part1.header 53B
lcov.info 6KB
n8.jpeg 47KB
wx.jpg 131KB
bb2.jpg 124KB
total.jpg 93KB
zhongshan.jpg 92KB
v6.jpg 92KB
v10.jpg 85KB
zheda.jpg 85KB
v8.jpg 84KB
js.jpg 74KB
v11.jpg 71KB
b1.jpg 69KB
b12.jpg 68KB
b3.jpg 68KB
b11.jpg 67KB
b13.jpg 64KB
v15.jpg 61KB
v13.jpg 57KB
b4.jpg 56KB
v14.jpg 54KB
b5.jpg 50KB
b15.jpg 47KB
j6.jpg 45KB
b10.jpg 45KB
b14.jpg 39KB
b8.jpg 38KB
dizhi.jpg 37KB
nanda.jpg 36KB
nanshi.jpg 32KB
b7.jpg 31KB
b9.jpg 29KB
b6.jpg 27KB
c++.jpg 25KB
j4.jpg 24KB
huahang.jpg 22KB
v12.jpg 20KB
v9.jpg 20KB
V7.jpg 20KB
note.jpg 19KB
v5.jpg 18KB
control.jpg 17KB
b2.jpg 17KB
h5.jpg 17KB
v3.jpg 13KB
v4.jpg 12KB
idl2.jpg 12KB
n13.jpg 12KB
baidumap.jpg 11KB
beida.jpg 10KB
echarts.jpg 10KB
idlpic.jpg 10KB
IDL.jpg 9KB
QGIS.jpg 9KB
j5.jpg 9KB
landa.jpg 8KB
j9.jpg 8KB
google.jpg 8KB
rumen.jpg 8KB
j7.jpg 6KB
wuhan.jpg 5KB
bson.js 543KB
encoding-indexes.js 541KB
browser.umd.js 504KB
共 1406 条
- 1
- 2
- 3
- 4
- 5
- 6
- 15
资源评论
普通网友
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 用QT实现的桌面端聊天室软件,含服务端和客户端,使用经过SSL加密的TCP通
- 一款基于 MATLAB 的 EEG 神经反馈训练系统 在神经反馈实验过程中可实时观察并记录 EEG 信号和神经反馈实验标记
- Java SSM 商户管理系统 客户管理 库存管理 销售报表 项目源码 本商品卖的是源码,合适的地方直接拿来使用,不合适的根据
- 基于Spring boot 的Starter机制提供一个开箱即用的多数据源抽取工具包,计划对RDMS(关系型
- 水泵系统水力计算公式-标准版
- Wesley是一套为经销商量身定制的全业务流程渠道 分销管理系统(手机APP称为经销商管家)
- Adaptive Autosar EM 标准规范
- 鼓谱图片转MuseScore超文本文档实验程序
- 自动驾驶感知动态障碍物算法上车效果 (Xavier jetson&autoware)
- 【实验指导书-2024版】实验一:查验身份证.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功