Tag: mongoose

处理节点js和mongooseasynchronous保存和redirect

我是node.js的新手,只是想了解这些东西的asynchronous性质。 好吧,这是一个非常简单的表单提交。模型如下所示: – var mongoose=require('mongoose'); var Schema=mongoose.Schema; var PostSchema=new Schema({ title:{type:String,required:true}, post:String, }); var PostModel=mongoose.model('blogpost',PostSchema); module.exports=PostModel; 然后路由处理程序如下: app.post("/submitpost",function(req,res){ var title=req.body.title; var post=req.body.post; var thepost=new PostModel({title:title,post:post}); thepost.save(function(err,data){ if(err)throw err; console.log(data); }) console.log("title is "+title); console.log("post is "+post); res.send("saved"); }); 现在假设在“thepost.save(callback)”期间validation失败,我想显示一个错误页面,而不是“保存”。 我将如何做到这一点?

用NodeJS检索远程文件进行存储?

我有NodeJS restify安装程序,我正在使用mongoose附件将图像附加到我的用户模型,并将图像存储在S3存储桶中。 我还允许用户使用Passport.JS使用Facebook,Google等进行注册。 问题是mongoose-attachments在调用.attach()函数时需要本地文件引用,PassportJS提供了一个远程URL – 所以我需要下载图像,然后从tmp附加它。 我应该如何用NodeJS来解决这个问题? 有没有一个好的模块,我可以用这个?

Mongo:在子目录上查询

您好,我有这个问题时检查如果一个子文档存在之前推新的子文档。 var UserSchema = new Schema({ name : String, app_key : String, app_secret : String, tasks : [{type: Schema.ObjectId, ref: 'Task'}] // assuming you name your model Task }); var TaskSchema = new Schema({ name : String, lastPerformed : Date, folder : String, user : {type: Schema.ObjectId, ref: 'User'} // assuming you name your model […]

当创build一个新的文档时,只能调用Mongoose Setter?

我使用Mongoose getter和setter我最近发现setter只适用于当我创build一个新的文档(UserSchema.create …)但setter不会被调用更新(UserSchema.findByIdAndUpdate …)。 我是对的还是我错过了什么 如果我是对的,是否有更新? ( http://mongoosejs.com/docs/2.7.x/docs/getters-setters.html 谢谢。 更新 我发现git问题: https : //github.com/LearnBoost/mongoose/issues/751 ,这意味着这是预期的行为。 不知道我是对的。

mongooseselect子文件

我有这个mongoose纲要: UserSchema = new db.Schema({ fullname : String, sale : [{ _id: [db.Schema.Types.ObjectId] }], friends : [db.Schema.Types.ObjectId] } 我怎样才能修复这个代码,使其select销售的_id字段? TIA user.Model .find({ _id : { $in: friends } }) .select('sale._id') // invalid syntax .exec(function(err, results) { console.log(results); });

在使用mongoose时,在节点和mongo之间加载连接是否正常?

我试图找出一个性能问题,我想知道是否正常有大约30(我猜想在群集configuration2 cpu上每个cpu 15)不同的连接。 这个数字接近一致,但我不知道为什么。

尝试从mongodb格式化date时出现问题

我正在尝试在显示模型之前在模型上格式化datetypes属性。 这是我正在使用的代码: // MODEL var mongoose = require('mongoose'), Schema = mongoose.Schema; var ArticleSchema = new Schema({ title: String, content: String, author: { type: String, default: 'Vlad'}, postDate: { type: Date, default: Date.now } }); ArticleSchema.methods.formatTitle = function() { var link = this.title.replace(/\s/g, '-'); return '/article/' + link; }; ArticleSchema.methods.snapshot = function() { var snapshot = […]

不正确的子文档被更新?

我有一个包含一系列子文档的Schema,我只需要更新其中的一个。 我使用子文档的ID做一个findOne ,然后将返回的数组中的位置0的子文档的响应减less。 不pipe我做什么,我只能得到父文档中的第一个子文档来更新,即使它应该是第二,第三等。只有第一个子文档被更新,不pipe是什么。 据我所知它应该工作,但我不是一个MongoDB或Mongoose专家,所以我显然是错的地方。 var template = req.params.template; var page = req.params.page; console.log('Template ID: ' + template); db.Template.findOne({'pages._id': page}, {'pages.$': 1}, function (err, tmpl) { console.log('Matched Template ID: ' + tmpl._id); var pagePath = tmpl.pages[0].body; if(req.body.file) { tmpl.pages[0].background = req.body.filename; tmpl.save(function (err, updTmpl) { console.log(updTmpl); if (err) console.log(err); }); // db.Template.findOne(tmpl._id, function (err, tpl) […]

我不能将数组推入mongoose模式

这是我的控制器,一个表格发送到这里的数据: exports.addrepair = function(req, res, next){ Person.findById( req.body.id, function (err, Person) { Person.update({id: req.body.id}, {$pushAll: { problem: req.body.problem , solution: req.body.solution , date_on: Date.now() }} ,{upsert:true},function(err){ if(err){ console.log(err); }else{ console.log("Added"); } }) }) } 该模式是: var Person = new Schema ({ name: String, Repair: [ problem: String, solution: String, date_on: Date ] }) 并不能推动任何修复人。 与console.log我可以看到所有的作品,但不是推。

用mongoosesorting阿尔法

我试图通过mongoose3.6.20sorting,我收到了一些意想不到的结果。 我有一个名字的公司名单。 起初我以为也许它是在区分大小写的方式。 基于文章,我期望是真实的。 我现在正在使用虚拟属性来排除sorting字段。 不过,我仍然得到意想不到的结果。 CompanySchema.virtual('name_lower').get(function(){ return this.name.toLowerCase(); }); 当我sorting Company.find().sort({ name_lower: 1 }); 我按照以下顺序得到它: 公司名 谷歌 公司名称(是testing重复) 我也输出我的虚拟财产的价值,它看起来是正确的。 没有可能导致第二个“公司名称”出现在Google之后的空白字符或时髦字符。 使用nodejs,express,mongoose。 我错过了什么或做错了什么? 更新: 根据答案中提供的信息,我重构了我的模式以包含一些规范化字段,并将其挂接到文档的预保存事件中,在那里我更新这些规范化字段并在将来的所有查询中使用它们进行sorting。 CompanySchema.pre('save', function(next){ this.normalized_name = this.name; }); 接下来,在我使用的模式中: var CompanySchema = mongoose.Schema({ … normalized_name: { type: String, set: normalize }, … }); 凡normalize是一个函数,现在,返回一个小写的值传递给它的值。 但是,这使得我可以很快地扩展它,并且可以快速地对其他可能需要sorting的领域进行相同的处理。