node.js - 猫鼬查询嵌入文档 (Mongoose-graphQL)
问题描述
我需要查询嵌入式文档,但它返回空数组。我正在使用猫鼬、阿波罗服务器和 graphQL。
我需要的是获取所有具有名为父过滤的字段的文档,其中包含“名称”字段。
这是模型:
const elfoSchema = new Schema
(
{
name:
{
type:String,
unique:true
},
parent:
{
type: Schema.ObjectId,
ref:'Elfo'
},
}
)
这是 graphql 类型:
type Elfo
{
id:ID
name:String
parent:Elfo
}
这是端点:
async function newElfo(name,parent)
{
const newElfo = new Elfo()
newElfo.name = name
if(parent)
{
newElfo.parent = parent
}
const storedElfo = await newElfo.save()
await storedElfo.populate('parent').execPopulate()
try {
if(storedElfo)
{
return storedElfo
}
} catch (error) {
throw new Error(error.message)
}
}
async function getElfoChildren(elfo)
{
const storedElfo = await Elfo.findById(elfo)
try {
if(!storedElfo) throw new Error('elfo not exist')
const elfoChildren = await Elfo.find({'parent.name':storedElfo.name}).populate('parent')
try {
if(elfoChildren)
{
return elfoChildren
}
} catch (error) {
throw new Error(error.message)
}
} catch (error) {
throw new Error(error.message)
}
}
但结果是这样的:
{
"data": {
"getElfoChildren": []
}
}
但是通过这个查询,我得到了所有结果:
const elfoChildren = await Elfo.find({'parent':storedElfo._id}).populate('parent')
结果:
{
"data": {
"getElfoChildren": [
{
"id": "6106c57fb080f1080e60b584",
"name": "elfo3",
"parent": {
"id": "6106c569b080f1080e60b583",
"name": "elfo2"
}
},
{
"id": "6106c586b080f1080e60b585",
"name": "elfo4",
"parent": {
"id": "6106c569b080f1080e60b583",
"name": "elfo2"
}
},
{
"id": "6106c58ab080f1080e60b586",
"name": "elfo5",
"parent": {
"id": "6106c569b080f1080e60b583",
"name": "elfo2"
}
},
{
"id": "6106c58eb080f1080e60b587",
"name": "elfo6",
"parent": {
"id": "6106c569b080f1080e60b583",
"name": "elfo2"
}
}
]
}
}
有谁能够帮我?
解决方案
推荐阅读
- python - 模块没有属性 __file__
- laravel-lighthouse - 将当前模型发送到 can 指令
- javascript - HTML5 Canvas 的图像输出尺寸和 DPI 大于输入尺寸和 DPI
- sql - 将表中所有记录的外键更新为一列
- python - 数组不会在 while 循环中用新值更新
- c# - 接口实现可以限制在程序集中吗?
- r - 计算R中的股票双周回报标准偏差
- javascript - 如何在节点 js 和 mongodb 中添加包含所有页面详细信息的分页
- python - 在没有创建表的情况下缺少每小时值 sql(无法更改数据库)
- javascript - 在 Vue3 中接收事件 - 在渲染期间访问了属性,但未在实例上定义