猫鼬通过参考字段查找



我有一个这样的通道模式:

const channelSchema = new mongoose.Schema(
{
name: {
type: String,
unique: true
}
}
);

这是反馈模式:

const feedbackSchema = new mongoose.Schema({
channelId: {
type: mongoose.Schema.Types.ObjectId,
ref: "channel",
require: true
}
});

如何按频道名称查找反馈?

Feedback.find({channelId.name : 'something'})

谢谢

由于您没有任何从通道模式到反馈模式的引用,因此您可以使用猫鼬的填充虚拟功能。

所需的更改如下所示:

1-( 像这样替换您的通道架构以使用虚拟填充:

const mongoose = require("mongoose");
const channelSchema = new mongoose.Schema(
{
name: {
type: String,
unique: true
}
},
{
toJSON: { virtuals: true }
}
);
// Virtual populate
channelSchema.virtual("feedbacks", {
ref: "feedback",
foreignField: "channelId",
localField: "_id"
});
module.exports = mongoose.model("channel", channelSchema);

2-(使用以下查询查找给定频道名称的反馈:

请注意,我在查询中硬编码了通道名称,您可以从请求正文或请求查询或请求参数中读取它。

router.get("/feedback", async (req, res) => {
const result = await Channel.findOne({ name: "Channel 1" }).populate({
path: "feedbacks"
});
res.send(result);
});

响应将是这样的:

[
{
"_id": "5de5509476a9c34048c1d23d",
"name": "Channel 1",
"__v": 0,
"feedbacks": [
{
"_id": "5de5512d7d87de2d4c6b38d2",
"channelId": "5de5509476a9c34048c1d23d",
"__v": 0
},
{
"_id": "5de551357d87de2d4c6b38d3",
"channelId": "5de5509476a9c34048c1d23d",
"__v": 0
}
],
"id": "5de5509476a9c34048c1d23d"
}
]

或者,如果您只对反馈感兴趣,可以通过result.feedbacks访问它们:

router.get("/feedback", async (req, res) => {
const result = await Channel.findOne({ name: "Channel 1" }).populate({
path: "feedbacks"
});
res.send(result.feedbacks);
});

这将为您提供一系列反馈,如下所示:

[
{
"_id": "5de5512d7d87de2d4c6b38d2",
"channelId": "5de5509476a9c34048c1d23d",
"__v": 0
},
{
"_id": "5de551357d87de2d4c6b38d3",
"channelId": "5de5509476a9c34048c1d23d",
"__v": 0
}
]

您无法查询不存在的对象的属性,我建议首先查询通道,获取 id 并从那里进行查找。

const channel = await Channel.findOne({ name });
const feedback = await Feedback.find({ channelId: channel._id })

您可以使用插件猫鼬按引用查找。

步骤1 安装

npm i -S mongoose-find-by-reference

npm 链接 Github链接

步骤2plugin()插件

const mongoose = require("mongoose");
// Require mongoose-find-by-reference
const { MongooseFindByReference } = require('mongoose-find-by-reference');
await mongoose.connect("mongodb://localhost:27017/test")
const channelSchema = new mongoose.Schema(
{
name: {
type: String,
unique: true
}
}
);

const feedbackSchema = new mongoose.Schema({
channelId: {
type: mongoose.Schema.Types.ObjectId,
ref: "channel",
require: true
}
});
// Execute function plugin() on schema with Reference paths
feedbackSchema.plugin(MongooseFindByReference);

const Channel= mongoose.model('channel', channelSchema );
const Feedback= mongoose.model('feedback', feedbackSchema );

步骤3 只需使用它!

const { _id } = await Channel.create({ name: "C" })
await Feedback.create({channelId : _id})
/** Its conditions will be auto automatically replaced with :
{
channelId: {
$in: [
/* ObjectIDs for Eligible channels */
],
},
}
*/
const findResult = await Feedback.find({channelId.name : 'C'})

> 使用populate方法查询

try {
const data = await feedBack.find(...).populate({ path: 'channel', match: { id: 'xxx' }).exec();
} catch (err) {
console.log(err);
}

最新更新