Tag: mongoose

摩卡testing花费太长的时间来执行

所以我正在一个express.js应用程序,我有一个mongoose模型用户。 我写了一个testing文件(使用Mocha)来testingsave()函数,但是我所有的testing都花费很长时间来执行并最终超时。 这是我得到的错误: Testing – Server – User – Model Testing save() 1) should be able to save without problems 2) should fail to save an exisitng user again 3) should should an error when try to save with empty email 4) should give an error when try to save with empty password 5) "after […]

洞悉两个REST APIdevise的可行性

过去我已经开发了几个REST API,目前正在为我们公司内部的软件解决scheme构build一个新的REST API。 我select了MEAN堆栈来构build这个和Mongoose来pipe理模式devise和数据层validation。 良好的devise,耐用性和可扩展性是我的榜单之首。 API将被我们内部的Angular应用程序,Android应用程序所使用,谁知道还有什么,有一天我们的PHP网站可能会使用API​​,或者将来我们可能会为其他业务提供平台。 我目前正在称重两种devise: 通用目的 :根据身份validation提供对除API的受限制CRUD访问权以外的所有访问权限,为复杂请求提供自定义端点。 有点像Parse 。 限制 :曾经使用过这个。 API消费者提出了一个需求,然后由服务器以端点的forms完成。 通用devise API消费者可以灵活地根据前端需求来制作查询,而非常复杂的编译则被转移到自定义的端点上。 全局限制(例如:最多可以返回100条logging),每个经过身份validation的系统用户types可以是限制性模式和logging级访问。 'use strict'; module.exports = function (app) { // Api routing var api = require('../controllers/api.controller.js'); /** * Cental API router */ app.route('/api/create') .post(api.create); app.route('/api/read') .get(api.read); app.route('/api/update') .put(api.update); app.route('/api/remove') .delete(api.remove); app.route('/api/dashboard/:country/:date') //Complex endpoint .get(api.dashboard); }; 创build调用的示例响应: { "response":[ { "_id":"54f703531c3757fe23923250", […]

用MochatestingExpress和Mongoose

我正在尝试使用Mocha和Chai来testing我的REST API端点处理程序,该应用程序是使用Express和Mongoose构build的。 我的处理程序主要是这样的forms: var handler = function (req, res, next) { // Process the request, prepare the variables // Call a Mongoose function Model.operation({'search': 'items'}, function(err, results) { // Process the results, send call next(err) if necessary // Return the object or objects return res.send(results) } } 例如: auth.getUser = function (req, res, next) { […]

连接到MMS副本集时发生Mongo身份validation错误

我使用MongoDBpipe理服务(MMS)在服务器上部署和pipe理我的MongoDB实例,并部署了一个副本集。 我还确认我在“授权和用户”选项卡中添加了具有所需权限的用户,如下图所示。 当我login到我的服务器并运行命令来检查用户是否已被添加它显示它们已被authentication,因为它返回1。 $ use admin $ db.auth('mbdb', 'xxxx') $ 1 我有一个使用mongoose连接到mongodb的节点应用程序,这是我用来连接的url格式: 'url' : 'mongodb://mbdb:xxxx@localhost:27000/dbprod?replicaSet=ProdSet' 我遇到的问题是,当我启动节点应用程序,我得到这个错误: > mobile-bulletin-node@0.0.1 start /var/TeamCity/buildAgent/work/d81f9c89b9a85a90 > node ./bin/www { [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } js-bson: Failed to load c++ bson extension, using pure JS version /var/TeamCity/buildAgent/work/d81f9c89b9a85a90/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246 throw message; ^ MongoError: auth failed at Object.toError (/var/TeamCity/buildAgent/work/d81f9c89b9a85a90/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:114:11) at /var/TeamCity/buildAgent/work/d81f9c89b9a85a90/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1156:31 […]

使用坐标geojson保存时间戳

我使用GeoJson来保存一些timeStamp到Mongoose数据库的坐标,我这样定义模型: var positionSchema = mongoose.Schema({ position: [{ properties: { timeStamp: Date }, geometry: { type: { type: String, default: 'Point' }, coordinates: { type: [Number] } } }] }); 数据从API发送如下: position.findByIdAndUpdate(values.geometries, { $push: { position: { properties: request.payload.timeStamp, geometry: lastLocation } } } lastLocation是: lastLocation = { type: 'Point', coordinates: [ request.payload.position.longitude, request.payload.position.latitude ] }; […]

将新的mongoose中间件前提条件应用于集合中所有已有的元素

恐怕我已经知道了答案,但我会问。 在mongoose,我只是增加了一个现有的模式的前提条件。 此条件基本上连接对象中所有元素的值,创build一个散列并将其作为另一个字段存储在同一个对象中。 这段代码将从现在开始保存的所有元素上执行,但是我想知道是否可以对所有现有的元素执行它,或者如果我必须编写一个脚本来更新它们。 谢谢!

在mongoose中查询和按date过滤

我有这个级别的mongoose文件 我试图做一个filter来填充位于某个date范围内的位置的几何graphics。 我尝试这样的事情: .populate('geometries', null, { 'locations.properties.timeStamp': { $gt: startDate, $lt: endDate }}) .exec(function(err, item) { //some code } 像这样: .populate('geometries') .where({ 'locations.properties.timeStamp': { $gt: startDate, $lt: endDate }}*/) .exec(function(err, item) { //some code } 有了这些查询,我无法访问时间戳进行比较,结果为空或未定义。 编辑1: variablesstartDate和endDate来自于angulars控制器的url,它们是这样定义的: var deductDate = new Date(endDate); var startDate = deductDate.setDate(deductDate.getDate()-1); Item.currentDay($stateParams.epc, url, { groupBy: '1d', endDate: moment(endDate).format(), startDate: […]

如何查询以获取Mongoose中的最后更新date

我有这个mongodb集合。 有两个不同的lastUpdatedate的file1.pkg 。 { "_id" : ObjectId("550286416fee741b0379c2d8"), "lastUpdate" : ISODate("2015-03-12T16:00:00.000Z"), "file" : "file1.pkg", "idBook" : "54f6d1d8c239491400aa495e", "idUser" : "54c83f37e2e315bfbb43e98a" }, { "_id" : ObjectId("450286416fee741e0379c2d8"), "lastUpdate" : ISODate("2015-03-10T16:00:00.000Z"), "file" : "file1.pkg", "idBook" : "54f6d1d8c239491400aa495e", "idUser" : "54c83f37e2e315bfbb43e98a", }, { "_id" : ObjectId("560286416fee741b0379c24d"), "lastUpdate" : ISODate("2015-03-12T16:00:00.000Z"), "file" : "file2.pkg", "idBook" : "54f6d1d8c239491400aa495e", "idUser" : "54c83f37e2e315bfbb43e98a" } 我想要得到最新的更新date文件,如: […]

Mongoose索引后的Mongo数据库修复名称?

Mongoose与Node和Mongo数据库交互的方式,我有一个非常奇怪的问题。 我使用express来创build一个基本的get apipath来从我的mongodb中获取一些数据。 我有一个名为testing的数据库,它有一个集合电话“billings” 所以架构和路由是非常基本的 apiRouter.route( '/计费/') .get(function(req, res) { Billing.find(function(err, billings) { if (err) res.send(err); // return the bills res.json(billings); }); }); “帐单”是我的mongoose模式。 只有1个对象{test:string} 这工作得很好,我得到了我所有的项目在我的名为“比林斯”,这是只有一个项目{testing:“成功”}的所有项目的回应 接下来我创build了一个名为“历史”的集合 我设置了与我的帐单完全相同的设置。 apiRouter.route( '/ historys /') // get all the history .get(function(req, res) { Historys.find(function(err, historys) { if (err) res.send(err); // return the history res.json(historys); }); }); 再一次“历史”是我的mongoose模式。 这个模式与我的账单设置是一样的,因为我没有任何真实的数据,字段是相同的,我只是用testing字段,所以从billings和历史回报json对象应该已经 […]

显示Mongoose模型的多个validation错误

如果我有这个模式: var userSchema = Schema( {name : { type: String } }); userSchema.path('name').validate(function(value) { return value.length > 4; }, 'Name is too short'); userSchema.path('name').validate(function(value) { return hasNoNumbers(value); }, 'Name cannot have numbers'); var User = mongoose.model('User', userSchema); 然后我创build一个这样的模型并运行validation函数: var newUser = new User({name: '1da'}); newUser.validate(function(err) { console.log(err.errors.name); }) 这只logging第一个错误消息“名称太短”。 但是,名称属性不符合validation要求。 有没有办法显示这两个错误信息? 谢谢