Подтвердить что ты не робот

Как использовать.where() с после заполнения в мангусте

Итак, у меня две схемы

var subcategories = new Schema({
    //the category being populated needs to be the same case ;
    categoryId: [{ type: Schema.ObjectId, ref: 'categories' }],
    name: String,
    description:  String,
    display:  Boolean,
    active: Boolean,
    sortOrder:  Number,
    createDate: Date,
    updateDate: Date,
    type:  String,
    startDate: Date,
    endDate: Date,
    authorId: String
});

А также

var categories = new Schema({
    name: String,
    description:  String,
    display:  Boolean,
    active: Boolean,
    sortOrder:  Number,
    createDate: Number,
    updateDate: Number,
    type:  String,
    startDate: Date,
    endDate: Date,
    authorId: String
});

И я хочу, чтобы запрос возвращался, только если активна/отображается в обеих категориях/подкатегориях. У меня возникают проблемы с тем, как правильно установить фильтр для categoryId после заполнения. Вот что я до сих пор

exports.generateList = function (req, res) {
    subcategories
            .find({})//grabs all subcategoris
            .where('categoryId').ne([])//filter out the ones that don't have a category
            .populate('categoryId')
            .where('active').equals(true)
            .where('display').equals(true)
            .where('categoryId.active').equals(true)
            .where('display').in('categoryId').equals(true)
            .exec(function (err, data) {
            if (err) {
                console.log(err);
                console.log('error returned');
                res.send(500, { error: 'Failed insert' });
            }

            if (!data) {
                res.send(403, { error: 'Authentication Failed' });
            }

            res.send(200, data);
            console.log('success generate List');
        });
    };

Единственная проблема - даже когда у меня есть категория с display = false, она все равно будет возвращена.

4b9b3361

Ответ 1

Чтобы создать условия запроса для заполненных ссылок, существуют специальные способы, на которые можно ссылаться здесь:

Условия запроса и другие параметры

Что, если мы хотим заполнить наш фанерный массив на основе их возраста, выбрать только их имена и вернуться максимум, любые 5 из них?

Story
  .find(...)
  .populate({
    path: 'fans',
    match: { age: { $gte: 21 }},
    select: 'name -_id',
    options: { limit: 5 }
})
.exec()

Поэтому в вашем случае вам нужно сделать что-то похожее на это:

subcategories
  .find({})//grabs all subcategoris
  .where('categoryId').ne([])//filter out the ones that don't have a category
  .where('active').equals(true)
  .where('display').equals(true)
  .populate({
    path: 'categoryId',
    match: {
      active: true,
      display: true,
    }
  })
  .exec()