Mongodb Mongoose是否确实验证了对象Id的存在?

Mongodb Mongoose是否确实验证了对象Id的存在?,mongodb,mongoose,Mongodb,Mongoose,我喜欢猫鼬带来的验证。我们正试图弄清楚是否要使用它,并忍受开销。是否有人知道在创建mongoose架构时是否提供对父集合的引用(在子架构中,将父对象的对象id指定为字段),这是否意味着每次尝试保存文档时,它都会检查父集合是否存在引用的对象id?否,架构中定义为对另一个集合的引用的ObjectId字段在保存时未被检查为存在于引用的集合中。如果需要,您可以在Mongoose中间件中执行此操作。我正在使用中间件执行此操作,在验证时搜索元素: ExampleSchema = new mongoose.S

我喜欢猫鼬带来的验证。我们正试图弄清楚是否要使用它,并忍受开销。是否有人知道在创建mongoose架构时是否提供对父集合的引用(在子架构中,将父对象的对象id指定为字段),这是否意味着每次尝试保存文档时,它都会检查父集合是否存在引用的对象id?

否,架构中定义为对另一个集合的引用的
ObjectId
字段在保存时未被检查为存在于引用的集合中。如果需要,您可以在Mongoose中间件中执行此操作。

我正在使用中间件执行此操作,在验证时搜索元素:

ExampleSchema = new mongoose.Schema({

    parentId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Example'
    }

});

ExampleModel = mongoose.model('Example', ExampleSchema);

ExampleSchema.path('parentId').validate(function (value, respond) {

    ExampleModel.findOne({_id: value}, function (err, doc) {
        if (err || !doc) {
            respond(false);
        } else {
            respond(true);
        }
    });

}, 'Example non existent');
我正在使用。很好用

var mongoose = require('mongoose');
var idValidator = require('mongoose-id-validator');

var ReferencedModel = new mongoose.Schema({name: String});

var MySchema = new mongoose.Schema({
  referencedObj : { type: mongoose.Schema.Types.ObjectId, ref: 'ReferencedModel'},
  referencedObjArray: [{ type: mongoose.Schema.Types.ObjectId, ref: 'ReferencedModel' }]
});

MySchema.plugin(idValidator);
你可以试试(我是开发者)

如果引用用于其他文档,它还可以防止删除

var mongooseRefValidator = require('lackey-mongoose-ref-validator');
mongoSchema.plugin(mongooseRefValidator, {
    onDeleteRestrict: ['tags']
});

这是一个早期版本,因此预计会出现一些bug。如果您发现任何问题,只需填写一张罚单。

我发现这个帖子非常有用,这就是我想到的:

我编写的这个中间件(我认为它是一个中间件,如果不是,请告诉我)检查引用模型中字段中提供的id

const mongoose = require('mongoose');

module.exports = (value, respond, modelName) => {
    return modelName
        .countDocuments({ _id: value })
        .exec()
        .then(function(count) {
            return count > 0;
        })
        .catch(function(err) {
            throw err;
        });
}; 
示例模型:

const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const Schema = mongoose.Schema;
const User = require('./User');
const Cart = require('./Cart');
const refIsValid = require('../middleware/refIsValid');

const orderSchema = new Schema({
    name: { type: String, default: Date.now, unique: true },
    customerRef: { type: Schema.Types.ObjectId, required: true },
    cartRef: { type: Schema.Types.ObjectId, ref: 'Cart', required: true },
    total: { type: Number, default: 0 },
    city: { type: String, required: true },
    street: { type: String, required: true },
    deliveryDate: { type: Date, required: true },
    dateCreated: { type: Date, default: Date.now() },
    ccLastDigits: { type: String, required: true },
});

orderSchema.path('customerRef').validate((value, respond) => {
    return refIsValid(value, respond, User);
}, 'Invalid customerRef.');

orderSchema.path('cartRef').validate((value, respond) => {
    return refIsValid(value, respond, Cart);
}, 'Invalid cartRef.');

orderSchema.path('ccLastDigits').validate(function(field) {
    return field && field.length === 4;
}, 'Invalid ccLastDigits: must be 4 characters');

orderSchema.plugin(uniqueValidator);

module.exports = mongoose.model('order', orderSchema);

我是一个非常新的开发人员,所以任何反馈都是非常宝贵的

有道理。我们要考虑性能,假设我们编码正确,不会输入不存在的对象id。这当然有它自己的风险,但我们正在从java重写到节点,以匹配我们架构的其余部分,并希望解决一些严重的性能问题。我喜欢这样。很好,很简单,上一次出版是16天前。直到今天仍然更新,好的一个!什么是回应?我在文档中没有看到,而且“回复”对我来说不起作用。当我尝试它时,我得到了“响应不是一个函数”。医生:太好了!这对我来说非常有效。