Tag: mongoose schema

TypeError:不能使用'in'运算符在中search'_id'

我不断地收到错误消息“TypeError:不能在'运算符'中search”+“mongoose”,“body-parser”,“./config”中的_id“…显然任何依赖。 我努力了: 确保我的模型/模式是正确的,如下所示: TypeError:不能使用'in'运算符来search男性中的'_id' 完全注释掉req.body的任何提及,以避免导致错误的任何可能性,如下所述: TypeError:不能使用'in'运算符在[{}]和这里search'_id' : Mongoose TypeError :不能使用'in'运算符来search[object Object]中的'_id' 读这个,但不知道这里发生了什么: Mongoose TypeError:不能使用'in'运算符来search'_id'in 我尝试了一些我认为可能导致问题的config.js文件。 (见下文) 我甚至尝试完全注释掉这些违规的依赖项(比如var config = require('./config') ),这显然不是一个可行的长期解决scheme,但也没有摆脱我的错误。 相反,这只是将错误转移到了不同​​的依赖关系上(例如,现在这与mongoose本身有关)。 所以现在我很难过 这是我的用户模型: "use strict"; var mongoose = require('mongoose'); var userSchema = new mongoose.Schema({ username: { type: String, required: false}, password: { type: String, required: false} }); var user = mongoose.model('user', userSchema); module.exports = […]

基于其他领域的mongoosevalidation

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

是否有预先阅读的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: […]

Mongoosevalidation架构没有模型

我想在对应于较大模式的模型上发布该对象之前,针对作为较大模式的成员的模式来validation对象的一些内部元素。 有人已经问过这个问题, 这个答案在第二个选项上有类似的地方: var p = new People({ name: 'you', age: 3 }); p.validate(function(err) { if (err) console.log(err); else console.log('pass validate'); }); 问题是,在进行validation之前,必须使用var People = mongoose.model('People', peopleSchema);创build一个新模型var People = mongoose.model('People', peopleSchema); 。 这是否意味着在这种情况下,新的集合'People'将被存储在MongoDB数据库中? 是叫mongoose.model('People', peopleSchema); 以任何方式改变数据库? 如果是这样,有没有办法做到这一点,而不是创build一个新的集合,在这种情况下, 'People' ? 我想要validation的模式将作为另一个文档的一个组件存储在数据库中,我不想用未使用的集合来污染数据库。

mongooseOR操作符在架构定义types

Moneoose是否支持一个类似JSON模式的模式? 以下是我想要实现的 new Schema({ components: [{ prop1: String, prop2: Number } | { prop3: Number }] }); 我排除了MIXED,因为我不想让任何东西,只允许2个定义之一,但不是任何东西。

在db.create模型中使用Mongoose.Schema.Types.Decimal时发生ValidationError

版本: mongoose@4.11.5 mongodb服务器版本:3.4.7 模型: var mongoose = require('mongoose'), Schema = mongoose.Schema; var CheckSchema = new Schema({ providerName: String, number: String, date: { type: Date }, dateReceived: { type: Date }, amount: Schema.Types.Decimal, paymentType: String, comment: String, status: String, images: [Image], transactions: [Transaction], rejectedTransactions: [Transaction], payerName: String }); 服务器代码: Check.create(newCheck, function (err, check) { if (err) […]

将mongoose文档转换为其他类似的模型

所以我有我的应用程序的以下布局: UserSchema(包含用户名和密码) var UserSchema = new mongoose.Schema({ firstName: String, lastName: String, email: String, username: String, password: String }, options); UserSchema.plugin(passportLocalMongoose); module.exports = mongoose.model("User", UserSchema); StudentSchema(鉴别者()在用户)与额外的领域 var StudentSchema = new mongoose.Schema({ indexNumber: String, courses: [{ id: { type: mongoose.Schema.Types.ObjectId, ref: "Course" }, entryDate: { type: Date, default: Date.now } }] }, options); module.exports = User.discriminator("Student", StudentSchema); […]

CastError:投射到ObjectId失败的值“types:”缓冲区“”

试图创build一个新的用户在我的Web应用程序没有意识到已经有一个用户在我的数据库用户名相同。 出于某种原因,这个边界案例打破了我的应用程序,现在每次我尝试去主页,我得到: (节点:14649)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):CastError:强制转换为ObjectId失败的值为“{type:'Buffer',data:[89,243,30,231,66,69,45, (节点:14649)[DEP0018]弃用警告:不处理的承诺拒绝已被弃用。 将来,未处理的承诺拒绝将使用非零退出代码来终止Node.js进程。 是什么使这个问题独特的从这里的其他问题在stackoverflow是事实,types是'缓冲区'。 没有其他问题有这个具体问题。 我不知道如何创build一个缓冲区,当我没有在我的数据库或节点服务器中使用它们。 这是在缓冲区错误之前发生的重复错误消息: {MongoError:E11000重复键错误集合:crud2.users索引:username_1 dup键:{:“lightbeam”}在Function.MongoError.create(/ Users / * /桌面/编码/应用程序/ thequoteapp / node_modules / mongodb核心/在/ Users / * / Desktop / coding / lib目录下的toError(/Users/*/Desktop/coding/apps/thequoteapp/node_modules/mongodb/lib/utils.js:139:22)中的lib / error.js:31:11) apps / thequoteapp / node_modules / mongodb / lib / collection.js:668:23 at handleCallback(/Users/*/Desktop/coding/apps/thequoteapp/node_modules/mongodb/lib/utils.js:120:56)at / Users /*/Desktop/coding/apps/thequoteapp/node_modules/mongodb/lib/bulk/unordered.js:465:9 at handleCallback(/ Users / * / Desktop / […]