Tag: mongoose

基于其他领域的mongoosevalidation

考虑下面的模式来保存Mongoose中的时间间隔: let dateIntervalSchema = new mongoose.Schema({ begin: { type: Date, required: true }, end: { type: Date, required: true } }) 我如何确保end始终大于或等于begin使用Mongoosevalidation ?

如何使用findByIdAndUpdate将objectId推送到对象的mongodb模式字段数组

每当我打我的API我得到同样的错误 – 我曾尝试发送值作为参数也失败了。 任何帮助,将不胜感激。 当我使用$set每次调用Web服务时更新相同的值,但它确实工作,但不是与$push 。 MongoError:字段'songId'必须是一个数组,但在文档中是objectIdtypes的 {_id: ObjectId('59709590380026118c22dd61')} 我的播放列表模式代码: – var PlaylistSchema = new mongoose.Schema({ name: String, coverphoto: { type: String, default: '' }, userId: { type: ObjectId, ref: 'User' }, songId: [{ type: ObjectId, ref: 'Song' }], updated_at: { type: Date, default: Date.now }, }); 我的宋架构代码 var mongoose = require('mongoose'); var Schema = […]

node.js mongoose chai-http RESTful APItesting(唯一字段)的行为与手动ARC APItesting不同

各位开发者, 我编写了一个基于node.js和mongoose的RESTful API服务器。 在编写相应的testing时,我对使用chai-http进行自动化testing和使用ARC(高级REST客户端,chrome插件)的手动APItesting之间的明显不同之处有着奇怪的体验。 我的用户模型(见下面的定义)有一个唯一的“用户名”字段。 当我使用ARC使用现有的用户名login新用户时,我按照预期从mongodb驱动程序中获取E11000。 当我这样做(至less我这样认为)使用chai-http.request.post,我得到一个statuscode 200,一个SUCCESS结果,我有两个用户在我的testing数据库集合中具有相同的用户名! 用户模式模式,包括/挂钩/导出等。ommited: const userSchema = new Schema({ admin_info: AdminInfo, role: { type: String, required: true}, username: { type: String, required: true, unique: true, index: { unique: true } }, password: { type: String, required: true }, firstname: String, lastname: String, mail: String, }); 用于POST /用户的通用路由处理函数,其中“model”是上述用户模式的一个实例: function addEntry(req, res, […]

节点Mongo连接错误:连接4到xxx集群closures

我正在使用AWS Lambda – Nodejs开发应用程序,并使用Mongodb作为后端。 为了连接Mongodb,我使用了nodejs本地库mongodb 。 问题是,随机,Mongodb抛出错误“连接4到群集closures”。 我在Google上find了一些答案,但没有解决。 像一些要求添加keepalive等。如所build议的那样,当使用AWS Lambda时,保存数据库连接,我坚持这些准则。 这是连接到Mongodb的函数的片段。 var connectToDatabase = function(_callback){ var options ={ server: { socketOptions: {keepAlive: 1} }, poolSize:100, replset: { rs_name: 'voila-cluster-shard-0', socketOptions: {keepAlive: 1} } } MongoClient.connect(url,options,function(error,connection){ if(error){ console.log(error) _callback(error) } else{ client = connection _callback(null,client); } }) } 有人可以帮我在这里。

Node.js:如何获取错误对象中的不正确的字段

我在下面的架构中有独特和必填字段。 当其中的任何一个留空时,将返回一个错误。 如何在Error对象中find有关哪些字段留空的信息? var mongoose = require('mongoose'); var uniqueValidator = require('mongoose-unique-validator'); var Schema = mongoose.Schema; var kullaniciShema = new Schema({ gsm: String, email: String, kullaniciAdi: { type: String, required: true, unique: true }, sifre: { type: String, required: true}, }, { collection: 'kullanici' }); kullaniciShema.plugin(uniqueValidator); var Kullanici = mongoose.model('Kullanici', kullaniciShema); module.exports = Kullanici; 下面是我提交的数据保存到数据库的控制器。 一些传入的数据是空的,所以我得到一个空白字段的错误。 […]

仅在mongoDB Node JS上存在集合时才显示给客户端

我有这个在Mongoose上定义的模式模型: var mongoose = require("mongoose"); var IngredientSchema = new mongoose.Schema({ name:String, number:Number, exist:Boolean, photoName:String }) module.exports = mongoose.model("Ingredient", IngredientSchema); 而且我想在网页上显示不同的结果,这取决于数据库中是否已经创build了一个成分。 这是我到目前为止(但它不起作用): <!– Check if inventory is empty or not, and display accordingly –> <% if ( ! ingredients) { %> <p>Add you first ingredient to the inventory !!</p> <% } else { %> <% ingredients.forEach(function(ingredient) […]

是否有预先阅读的mongoose事件?

每次从模式读取时我都想运行一个方法。 像这样的东西: var Schema = mongoose.Schema; var exampleSchema = new Schema({ views: { type: Number } }); exampleSchema.pre('read', function(next) { this.views++; next(); }); 或者,如果有更好的方法来做到这一点,请让我知道。 谢谢。

访问MongoDB属性不能按预期方式工作

这是我的模式 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var messageSchema = new Schema({ status: {type: String, default: 'Pending'}, latestUpdate: Date, createdAt: {type: Date, default: Date.now} }); module.exports = mongoose.model('Requests', messageSchema); 这是我如何打印到控制台的属性: hosSchemaModel.find(function(err, hosSchema) { if (err) { console.log('inside error') // return res.send(err); } else { console.log(hosSchema) } }); 在上面的代码中, console.log(hosSchema)打印下列内容: console.log()的结果 当我尝试访问hosSchema.status , undefined被打印到terminal。 […]

如何获取schema.pre()方法中的特定字段?

我用mongoose使用nodejs,我想在更新文档之前获取用户密码。 我正在使用schema.pre()方法在更新之前被调用,但是我不知道如何在这个方法中获得用户密码。 这里是我的UserSchema.pre()方法: UserSchema.pre('update', function(next){ var user = this; next(); }); 和我的模式: var UserSchema = new mongoose.Schema({ email: { type: String, required: true, minlength: 1, trim: true, unique: true }, password: { type: String, required: true, minlength: 6 }, firstName: { type: String, required: true, minlength: 1, trim: true }, lastName: { type: String, required: […]

使用mongoose将embedded文档的多个实例保存到我的模型中

我试图在我的模型中保存embedded式文档的多个实例,我期待每当我填写我的表单数据时,embedded式文档的一个新实例被创build并推送到一个数组中。 这是我的预测模式。 const mongoose = require('mongoose'); mongoose.Promise = global.Promise; const slug = require('slugs'); const teamSchema = new mongoose.Schema({ team1: { type: String }, team2: { type: String }, prediction:{ type: String } }); const predictionSchema = new mongoose.Schema({ author:{ type: String }, team: [ teamSchema ] }); module.exports = mongoose.model('Prediction', predictionSchema); 这是我的控制器 const mongoose = […]