7得票1回答
基于其他字段的Mongoose验证

考虑以下用于在Mongoose中保存时间间隔的模式: let dateIntervalSchema = new mongoose.Schema({ begin: { type: Date, required: true }, end: { type: Date, required:...

45得票4回答
Mongoose - 一次函数调用中删除多个文档

文档中有一个deleteMany()方法Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }, function (err) {}); 我想删除多个具有一个共同属性和其他属性不同的文档。就像这样: Site.deleteMany({...

10得票9回答
(node:71307) [DEP0079] DeprecationWarning

尝试更新MongoDB文档时,收到以下废弃警告: (node:71307) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated Node...

8得票2回答
如何创建包含对象数组的mongoose模式

我有这个JSON: { "data": [ "id": "1", "name": "Sample test", "description": "this is a sample test", "category": "t...

7得票1回答
Mongoose - ObjectID作为键?

我希望在我的mongoose模型中有一个对象(例如“ingredients”),其中键是ObjectIDs,它们的值是数字。这是否可能?我应该如何定义我的mongoose模式?您可以在下面找到一个示例。 示例JSON: { "_id": ""5a2539b41c574006c4...

10得票7回答
MongoDB / Mongoose时间戳不更新

模式:var schema = new Schema({...}, { timestamps: true, id: false, toJSON: { virtuals: true, }, toObject: { virtu...

7得票3回答
如何在Mongoose Schema中存储URL值?

我正在将IOS应用程序中的图像上传到Firebase,Firebase返回包括类型为URL的URL在内的元数据。 我应该将它存储为以下代码中的String类型,还是有特定的类型适用于URL? var schema = new Schema({ downloadURL: { type: ...

17得票5回答
如何在Mongoose.js的pre/post更新钩子中检查修改字段

我需要知道在Mongoose模式中,哪些字段被修改或者一个特定的字段是否在更新前或更新后的钩子函数中被修改。我尝试了以下方法,但仍然无法解决:schema.post('update', function (doc) { //check modified fields or if the...

27得票4回答
在定义Mongoose模式时,我应该使用Schema.Types.ObjectId还是Schema.ObjectId?

看起来我可以这样定义我的模式:var PossessionSchema = new mongoose.Schema({ thing: {type: mongoose.Schema.Types.ObjectId, ref:"Thing"} }); 或者这样:var PossessionSch...

15得票4回答
在mongoose中,如何要求一个字符串字段不为null或undefined(允许0长度字符串)?

我已经尝试了这个,它允许保存null、undefined和完全省略键:{ myField: { type: String, validate: value => typeof value === 'string', }, } 并且这个不允许保存 '' (空字符串)...