node.js - 在 mongoose 的查询中引用另一个模型的字段
问题描述
我有以下两个方案
const postSchema = new Schema({
service: {
type: Schema.Types.ObjectId,
ref: 'Service',
required: true
}
});
const serviceSchema = new Schema({
title: {
type: String,
required: [true, 'required'],
unique: true
}
});
我需要找到与搜索词匹配但引用 Service 模型的标题字段的 Post 模型的文档。我正在使用以下查询
Post.find().populate({path:'service', match:{title:{$regex: term, $options:"i"}}}).exec();
是的,它执行查询,但与它不匹配的文档将它们返回为空。我应该如何进行查询?
我正在使用猫鼬 5.13.2
解决方案
推荐阅读
- python - 如何处理 Django 中未应用的迁移?
- asterisk - 在 CDR 数据库 Asterisk 中插入值“答案”时出错
- objective-c - TimePicker 时间列反转
- javascript - 从 MDN 了解对象解构 {foo: bar} = {baz: true}
- azure-devops - AzureDevops:如何使用看板任务板冲刺上的任务管理测试活动
- typescript - 即使使用 async/await 方法,Promise 也未决
- python - 我正在尝试将数据框插入本地 MySQL 数据库
- excel - 如何将文件名与 Excel 中的单元格值连接并将其粘贴到 Word 中
- ionic-framework - Ionic 3 - 可点击卡片 - 重定向
- node.js - 无法推送 mongo 数组中的项目