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

问题描述 投票:0回答:1
我已经建立了这样的数据库:

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”作为匹配参数重复此代码时,一切都很好,并且可以正常运行!

感谢您的帮助!

朱莉娅

我已经建立了这样的数据库:const listSchema = new mongoose.Schema({name:{type:String,required:true,trim:true},AdditionalInfo:{type:...

mongodb mongoose mongoose-populate
1个回答
0
投票
只需丢失match中的多余花括号(在$or语句周围,它就可以工作:
© www.soinside.com 2019 - 2024. All rights reserved.