A
A
Alexey Yarkov2017-03-12 00:04:06
MongoDB
Alexey Yarkov, 2017-03-12 00:04:06

How to correctly set a condition in Mongoose?

There is a category model:

const CategorySchema = new mongoose.Schema( {
    /**
     * Название категории
     */
    title: {
        type: String,
        required: true,
        unique: true,
        validate: {
            validator: v => v.length,
            message: 'Название категории не должно быть пустым!',
        }
    },
    /**
     * Флаг, означающий то, что категория стандартная
     */
    root: {
        type: Boolean,
        required: true,
        default: true
    },
    /**
     * Дата и время создания
     */
    createdate: {
        type: Date,
        default: Date.now,
    },
    /**
     * Дата и время последнего изменения
     */
    lastupdate: {
        type: Date,
        default: Date.now,
    },
    /**
     * ID автора
     */
    userID: {
        type: mongoose.Schema.ObjectId,
        ref: 'User'
    },
    /**
     * Описание полей и заголовков для построения формы
     */
    metaInfo: {
        type: mongoose.Schema.Types.Mixed,
        required: true
    }
} );

As you can see from the diagram, the title field is unique. BUT! Now it was necessary to allow users to create their own categories. From this question:
How to make it so that if root==false or userID is not empty, then title would allow matches?
I do not know how to say))) I hope I explained clearly.
Standard categories are created when the application is deployed using the script 1 time.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nick8, 2017-03-12
@Nick8

You can do findOne with the projection { root: 1 } and then, depending on the existence and value of root, change / not change the title. I hope I understood the question correctly.

A
Alexander Krupnov, 2017-12-17
@worlddev

$('form').submit(function(){
//обработка полей
//возвращаем true при правильной заполненности полей формы и false в случае ошибки валидации
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question