【发布时间】:2015-05-25 08:46:53
【问题描述】:
MongoDB/Mongoose 的 Express.js 新手,我不知道如何查询它并获取我需要的 JSON。
我有一份乐器清单。有些乐器是一种子类型,所以我将它们与一种乐器联系起来。所以,我有一个看起来像这样的模式:
var InstrumentSchema = new mongoose.Schema({
name: String,
description: String,
parent: String
});
module.exports = mongoose.model('Instrument', InstrumentSchema);
它们都是乐器,所以都这样存储。我所说的“父”工具只会有一个空白的父值(即,它们没有父值)。但是,子类型仪器在该字段中具有“父”仪器的 _id。
所以,我最终可能会得到 ($ db.instruments.find()):
{
_id: ObjectId("5508ae0e81a30b766fb8b679"),
name: "Mandolin",
description: "blah blah blah",
parent: ""
},
{
_id: ObjectId("5508ae0e81a30b766fb8b80"),
name: "Mandola",
description: "blah blah blah",
parent: "5508ae0e81a30b766fb8b679"
},
{
_id: ObjectId("5508ae0e81a30b766fb8b681"),
name: "Octave Mandolin",
description: "blah blah blah",
parent: "5508ae0e81a30b766fb8b679"
}
所以后两个与前两个相关 - 通过“父”键。
我想请求第一个乐器,还有后两个(作为一个数组),最终得到如下内容:
{
_id: ObjectId("5508ae0e81a30b766fb8b679"),
name: "Mandolin",
description: "blah blah blah",
parent: ""
sub-instruments: [
{
_id: ObjectId("5508ae0e81a30b766fb8b80"),
name: "Mandola",
description: "blah blah blah",
parent: "5508ae0e81a30b766fb8b679"
},
{
_id: ObjectId("5508ae0e81a30b766fb8b681"),
name: "Octave Mandolin",
description: "blah blah blah",
parent: "5508ae0e81a30b766fb8b679"
}
]
}
所以,我得到了主要仪器(这将是 IU 中的主显示器),以及一组子仪器(可以在下面列出 - 在列表或表格中)。
我已经扫描了 MongoDB 和 Mongoose 文档并查看了一些 SO 问题,但似乎没有任何答案 - 我当然希望这是可能的。现在我有一个主要仪器的查询,例如:
var instRoute = router.route('/instruments/:instrument_id');
instRoute.get(function(req, res) {
Instrument.findById(req.params.insturment_id, function(err, instrument) {
if (err) {
return res.send(err);
}
res.render('instrument', {
instrument: instrument,
pageTitle: 'Instrument' // for the <title> tag
});
});
});
如何在其中插入子查询并将其添加到数组中返回的 JSON 中?好像我需要另一个 .find() 或者 MongoDB 文档中有关于 elemMatch 的内容(或者只是在哪里?)。我尝试过使用几种不同的东西,但没有任何效果。我只需要更新查询吗?或者 Instruments 的 Schema 是否也需要更改?
【问题讨论】:
-
你看过猫鼬对population的支持吗?
-
谢谢。当我扫描文档时,人群并没有引起我的注意——我不知道我在寻找什么。我会检查一下。
标签: mongoose