Tag: mongoose

express:保持所有DRY创build一个mongoose连接模块(使用新的createConnection方法)

这是我的实际数据库连接模块: var mongoose = require('mongoose'), conn = mongoose.createConnection('localhost', 'doto'); conn.on('error', function (err) { console.log('Error! DB Connection failed.'); }); conn.once('open', function () { console.log('DB Connection open!'); }); module.exports = conn; 有一个地方我用它 exports.list = function (req, res) { var conn = require('../lib/db_connection'); conn.once('open', function () { // if i dont wrap with this, the page will not […]

如何填充映射减less的结果

TestSessionSchema.statics.getTopTesters = function(callback){ var o = {}; o.map = function(){ emit(this._customer, this.points); }; o.reduce = function(_customer, values){ var result = {_customer: _customer, sum_points: 0, }; values.forEach(function(point){ result.sum_points += point; }); return result; }; o.out = { replace: 'createdCollectionNameForResults' }; o.verbose = true; this.mapReduce(o,function(err, model, stats){ model.find().sort({'value.sum_points': "desc"}).limit(5).populate("_customer").exec(function(error, results){ log(results); }) }); } 在map-reduce部分,我正在创build_customers点。 model.find().sort({'value.sum_points': "desc"}).limit(5).populate("_customer")不起作用 […]

mongoose如何处理密码编码很好?

我想重构我的用户架构。 这个决定的主要原因是我不想担心密码和盐的产生。 所以我想将编码逻辑从pre save处理程序移到setter。 不幸的是我没有从setter访问对象的其他属性(如盐)。 所以默认的盐不起作用,用盐编码密码也不行。 我目前的实施是: var userSchema = new mongoose.Schema({ username: { type: String, index: { unique: true, sparse: true }, required: true, lowercase: true, trim: true }, email: { type: String, index: { unique: true, sparse: true }, required: true, lowercase: true, trim: true }, salt: { type: String, select: false }, […]

如何在具有mongoose db的nodejs中存储最新的图像

你能解释一下我用什么技术来存储最新的图像? 基本上,我的目的是显示几乎最近更新的图像为我的网站。 问题是我不知道如何将最新的图像存储在mongoose数据库。

如何解决mongoose的分拣错误?

我得到这个错误,同时通过mongoosesorting:TypeError:无效的sorting值:{field:submittedDate}我的代码部分是: var image = mongoose.model('Image'); var allImages = image.find({reviewed:true },null,{sort:{"submittedDate":-1}}, function (err, images) { if(err) { logger.info("Error occured"); throw err; } else { logger.info("images sorted are:",images); cb(images); } }); 任何帮助将appriciated。

在m sort中按datesorting超过9个logging抛出错误

我正进入(状态 错误:sort()没有索引的数据太多 同时按升序sorting。 如果logging数不超过9,我只会得到这个exception。有人能告诉我我做错了什么吗? 或任何工作? 我的sorting语法是: image.find({reviewed:true},null,{sort:{“submittedDate”: – 1}},function(err,images){});

mongoose:一系列的自定义实例

根据文件我可以做这样的事情: var animalSchema = new Schema({ name: String, type: String }); animalSchema.methods.findSimilarTypes = function (cb) { return this.model('Animal').find({ type: this.type }, cb); } 假设我有另一个function: animalSchema.methods.owner ,如果animalScheme.owner是xyz,那么它将过滤search查询{…, owner: xyz} 有没有可能顺序使用实例? 我怎样才能做到这一点? var animalSchema.findSimilarTypes().owner("xyz") ? 任何提示?

在Mongoose中,如何在具有.create(),.id()和.remove()function的模型中创buildMongooseArray / Collection

我的模式: Account: mongoose.model('Account', new Schema({ account_name: String, company: String, notes: String, contact_info: [this.ContactInfo] })), ContactInfo: mongoose.model('ContactInfo', new Schema({ name: String, email: String, phone: String, notes: String })) 当我尝试对contact_info做任何操作时,它说方法不存在。 var c = new ContactInfo…); var a = new Account(…); a.contact_info.create(c); //error, create doesn't exsit a.contact_info.push(c); //works a.contact_info.id(…).remove(); //id doesn't exist 难道我做错了什么? 我的完整代码在这里(不是太多): https : //github.com/nycitt/node-survey-builder-api-server/blob/master/accounts.js 顺便说一下,我从Backbone.js接收input

我怎样才能在mongodb的输出查询上添加条件字段?

我的模式如下所示: var productSchema = new Schema({ name : String, likes : [{ user_id : Schema.Types.ObjectId, date : Date }] }); 假设我有这个集合 { name : A, likes : [ {user_id : id1, date : blahblah}, {user_id : id2, date : balh}] } { name : B, likes : [ {user_id : id1, date : blahblah}] } […]

奇怪的错误与mongoose

app.get('/showme', function (req, res){ console.log(req.session.user); UserModel.findOne({ user: req.session.user }, function (err, user){ if (err) throw err; console.log(user); res.send('Look'); }); }); 当这个被执行时,我正确接收到terminal中的用户,但之后,突然间应用程序崩溃了,这是错误TypeError: Cannot read property '_id' of null 。 req.session.user被定义,并在应用程序中。 这不是第一次调用UserModel.findOne({ user: req.session.user } ,但是是唯一返回这个错误的!为什么会发生这种情况? 编辑: 这是用户的模式 var userschema = new mongoose.Schema({ user: String, pass: String, mail: String, avatar: String, }); 编辑: 我不知道为什么,但是UserModel.find()没有出现任何错误。 它只发生在UserModel.findOne() 。 编辑: […]