mongodb - 在查询中访问 MongoDB 子文档字段(Typescript 和 Mongoose)
问题描述
我使用猫鼬作为 ODM。假设我有这种方案结构。
const Child = new mongoose.Schema({
field1: {
type:String
},
field2: {
type:String
}
});
const Parent = new mongoose.Schema({
children: {
type: [Child]
},
});
interface IChildDoc extends mongoose.Document {
field1: string
field2: string
}
interface IParentDoc extends mongoose.Document {
children: [IChildDoc]
}
const ParentModel = mongoose.model<IParentDoc>("Parent", Parent)
我想通过field1查询,但是像这样:
const parent = await ParentModel.findOne({children.field1 : "string"});
但我得到:
No value exists in scope for the shorthand property 'children'. Either declare one or provide an initializer.
我也尝试children.field1
输入一个字符串,但这只会导致返回 null 而我并不是真正的强类型
解决方案
您必须像这样查询:{"children.field1" : "string"}
推荐阅读
- powershell - 使用 powershell 监视文件夹中的新文件,但忽略新文件夹
- c# - 对多个子域使用相同的代码
- c++ - Valgrind 失败不会失败 CTest
- java - 如何在 android 中向 Firebase 数据库添加新数据?
- javascript - jQuery推送到多维数组只返回最后一个值
- statistics - 使用 PomeGranate 拟合多分量 GammaDistribution
- javascript - AngularJS + Scrollify 禁用后无法正常滚动
- c# - 额外字节 3F 收到串口 c#
- android - 如何制作像 ProgressDialog 这样的 Fragment 模态
- android - 开始删除文本时如何启用我的搜索方法