# mquery
`mquery` is a fluent mongodb query builder designed to run in multiple environments.
[](https://travis-ci.org/aheckmann/mquery)
[](http://badge.fury.io/js/mquery)
[](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
公司员工管理系统,vue+express+mongodb,一天速成的代写课设,无阅读代码意义。.zip
需积分: 0 169 浏览量
更新于2023-08-03
收藏 20.12MB ZIP 举报
这是一个基于Vue、Express和MongoDB构建的公司员工管理系统,用于快速完成毕业设计或课程设施工程。这个项目可能主要用于教学目的,让学生在短时间内掌握前后端分离的Web开发技术栈。以下将详细介绍涉及的关键知识点。
1. Vue.js:Vue.js 是一个轻量级的前端JavaScript框架,用于构建用户界面。它具有组件化、易学习、高性能的特点,通过声明式渲染数据到DOM,并提供了丰富的指令和插件系统。在这个项目中,Vue.js 负责展示数据和处理用户交互。
2. Express.js:Express是Node.js上的一个web应用框架,简化了HTTP服务器的创建,提供了路由、中间件等特性,使得构建Web服务变得更加简单。在这个系统中,Express用于处理HTTP请求,如CRUD(创建、读取、更新、删除)操作,连接后端与前端。
3. MongoDB:MongoDB是一个NoSQL数据库,以JSON格式的文档存储数据,支持高效地处理大规模数据。在这个系统中,MongoDB存储员工信息和其他管理数据,提供灵活的数据模型和强大的查询能力。
4. Node.js:Node.js是基于Chrome V8引擎的JavaScript运行环境,允许开发者使用JavaScript进行服务器端编程。在这个项目中,Node.js作为后端基础,承载了Express框架,实现数据处理和业务逻辑。
5. 前后端交互:项目采用RESTful API设计原则,通过HTTP方法(GET, POST, PUT, DELETE)进行数据交换。Vue.js通过axios等库向Express发送请求,获取或更新数据。
6. 数据库设计:MongoDB的集合(相当于关系数据库中的表)可能包括"employees"、"departments"等,每个文档包含员工或部门的相关属性,如姓名、职位、部门ID等。
7. 用户认证与授权:尽管描述中没有提及,但通常这类系统会包含用户登录注册功能。这可能涉及到JWT(JSON Web Tokens)或OAuth2等认证机制,以确保用户身份的安全和合法性。
8. 路由管理:Vue Router在前端负责页面跳转和导航,而Express的路由定义则处理后端的HTTP请求,两者共同实现系统的导航和页面逻辑。
9. 错误处理:为了提高系统的健壮性,项目可能包含错误处理机制,如Express的中间件来捕获和处理异常,以及Vue的错误边界来捕获组件内部错误。
10. 开发工具与流程:可能使用Git进行版本控制,Webpack或Vite进行模块打包,NPM或Yarn管理依赖,ESLint进行代码风格检查,Prettier进行代码格式化,Jest或Mocha进行单元测试。
总结,这个项目展示了如何使用现代Web技术栈搭建一个完整的Web应用,涵盖了前端UI展示、后端服务处理和数据库操作等核心环节,为初学者提供了一个快速了解和实践全栈开发的平台。然而,由于描述中提到“无阅读代码意义”,可能意味着代码质量并不高,或者缺乏注释和最佳实践,对于深入学习和参考价值有限。


白话机器学习
- 粉丝: 1w+
最新资源
- 软件产品用户使用报告.doc
- 数字图像处理第二章课件ppt课件.ppt
- 高层框剪结构商务楼项目管理策划书.ppt
- 2023年PLC应用技术课程工学一体化教学实施方案研究.doc
- 基于PLC的X62W万能铣床电气控制.doc
- 综合布线第4章.pptx
- 基于php的网上销售系统的设计与实现.doc
- 室外电力通信电缆的敷设施工.doc
- 计算机基础培训题目.docx
- 2023年办公软件二级考试判断题及答案.doc
- 湖南航天卫星通信科技有限公司(PPT).ppt
- 做个人简历的软件ppt模板.doc
- 网络拓扑图VISIO素材大全.ppt
- 竞盛保险经纪公司的项目管理研究.doc
- 网络营销之定价策略分析.pptx
- 动态规划算法实验报告.doc