猫鼬:在.populate({match})中使用$ or

时间:2019-10-13 18:41:45

标签: mongodb mongoose mongoose-populate

我已经建立了这样的数据库:


const listSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        trim: true
    },
    additionalInfo: {
      type: String,
      required: false
    },
    author: {
        required: true,
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }
  })

  listSchema.virtual('vocs', {
    ref: 'Voc',
    localField: '_id',
    foreignField: 'list'
  })

  var List = mongoose.model('List', listSchema);

  module.exports = List;


const vocSchema = new mongoose.Schema({
    foreignLanguage: {
        type: String,
        required: true,
        trim: true
    },
    translationGerman: {
      type: String,
      required: true,
      trim: true
    },
    translationForeignLanguage: {
        type: String,
        required: true
    },
    difficulty: {
        type: Number,
        required: true
    },
    list: {
        required: true,
        type: mongoose.Schema.Types.ObjectId,
        ref: 'List'
    }
  })

  var Voc = mongoose.model('Voc', vocSchema);

  module.exports = Voc;

现在,我要做的是:我想选择一个特定作者的所有列表,然后联系vocs并通过translationGerman和translationForeignLanguage过滤这些vocs:我想在所有列表中选择所有vocs具有特定值(例如,存储在“ translationGerman”或“ translationForeignLanguage”中的单词“ apple”。我尝试了以下代码:

router.get('/api/filterAllWords/:wordname', auth, async (req, res) => {
    try {

        // const words = await Voc.find({$or:[{translationForeignLanguage: req.params.wordname},{translationGerman: req.params.wordname}]});
        // console.log(words);
        // res.send(words)

        const lists = await List.find({author: req.user._id});
        let allVocs = [];

        for (let i = 0; i < lists.length; i++) {
            const singleListVocs = await lists[i].populate(
                {path: "vocs",
                match: {
                    {$or:[{translationForeignLanguage: req.params.wordname},{translationGerman: req.params.wordname}]}
                }}).execPopulate();
            for(let i = 0; i < singleListVocs.vocs.length; i++) {
                allVocs.push(singleListVocs.vocs[i])
            }
        }

        res.send(allVocs)
    }

    catch {
        res.status(404).send({status: 'No vocs found'});
    }
})

当我尝试测试代码时,它向我发送错误“意外令牌”。我在populate(match)内部使用$ or错误吗?当我尝试仅将“ translationForeignLanguage:req.params.wordname”作为match-Parameter重复此代码时,一切都很好并且可以正常运行!

感谢您的帮助!

朱莉娅

1 个答案:

答案 0 :(得分:0)

只需失去match中(在$or语句周围)的多余花括号即可,它应该可以工作:

lists[i]
  .populate({
    path: 'vocs',
    match: {
      $or: [
        { translationForeignLanguage: req.params.wordname },
        { translationGerman: req.params.wordname },
      ],
    },
  })
  .execPopulate();