javascript - 如何通过每个子文档中的两个字段过滤子文档数组
问题描述
我正在尝试添加一个帮助请求系统,该系统允许请求者仅向专家提出每个主题的帮助请求。如果专家列出了他们可以帮助的多个主题,我想将每个请求者限制为每个专家每个主题的一个帮助请求。
我正在将 node.js 和 mongoose.js 与自托管的 mongodb 实例一起使用,只要他们没有来自同一请求者就同一主题的现有请求,我就尝试使用$and
运算符来查找专家的。._id
它适用于一次更新,但在专家文档插入了一个子文档后,应用了topic_id
或requestor_id
过滤器并且没有返回专家。
// Schema
ExpertSchema = new mongoose.Schema({
expert_id: String,
helpRequests: [
requestor_id: String,
topic_id: String
]
});
//query
const query = {
$and:[
{expert_id: req.body.expert_id},
{'helpRequests.requestor_id': {$ne: req.body.requestor_id}},
{'helpRequests.topic_id': {$ne: req.body.topic_id}}
]
};
// desired update
const update = {
$push: {
helpRequests: {
requestor_id: req.body.requestor_id,
topic_id: req.body.topic_id
}
}
Expert.findOneAndUpdate(query, update, {new: true}, (err, expert) =>{
// handle return or error...
});
解决方案
您没有得到任何专家的原因是您的查询中的条件。
如果您在查询中的条件得到满足,结果总是根据您的查询条件返回,您将获得如此简单的结果。
您的查询
{'helpRequests.requestor_id': {$ne: req.body.requestor_id}},
{'helpRequests.topic_id': {$ne: req.body.topic_id}}
你将得到你expert
唯一的ifrequestor_id
并且topic_id
不存在于helpRequests
数组中。那就是你要查询的。
解决方案
根据您的架构,如果helpRequests
仅包含requestor_id
,topic_id
然后您可以通过以下查询实现您想要的。
Expert.findOneAndUpdate(
{
expert_id: req.body.expert_id,
}, {
$addToSet: {
helpRequests: {
requestor_id: req.body.requestor_id,
topic_id: req.body.topic_id
}
}
}, { returnNewDocument: true });
推荐阅读
- android - 如何从 DAO 获取 OnConflictStrategy 返回值
- wordpress - Custom Nav Walker 检查菜单子项是否有子项
- python - 在 python 中解析 Json 文件并在对话框流中打印响应
- c# - 如何使用 AutoMapper 将 3 个对象映射到视图模型
- amazon-web-services - 从 s3a 存储桶中检索最新的对象版本
- python - 如何从 python 中的多个光栅文件创建 netcdf 文件?
- javascript - 总是得到未定义的值
- android - 如何根据选定的段制作范围?
- node.js - Else 语句不会停止循环 NodeJS
- php - 如何使用多个数据集操作 laravel 获取请求