首页 > 解决方案 > 如何通过每个子文档中的两个字段过滤子文档数组

问题描述

我正在尝试添加一个帮助请求系统,该系统允许请求者仅向专家提出每个主题的帮助请求。如果专家列出了他们可以帮助的多个主题,我想将每个请求者限制为每个专家每个主题的一个帮助请求。

我正在将 node.js 和 mongoose.js 与自托管的 mongodb 实例一起使用,只要他们没有来自同一请求者就同一主题的现有请求,我就尝试使用$and运算符来查找专家的。._id它适用于一次更新,但在专家文档插入了一个子文档后,应用了topic_idrequestor_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...
    });

标签: javascriptnode.jsmongodbmongoose

解决方案


您没有得到任何专家的原因是您的查询中的条件。

如果您在查询中的条件得到满足,结果总是根据您的查询条件返回,您将获得如此简单的结果。

您的查询

{'helpRequests.requestor_id': {$ne: req.body.requestor_id}},
{'helpRequests.topic_id': {$ne: req.body.topic_id}}

你将得到你expert 唯一的ifrequestor_id并且topic_id存在helpRequests数组中。那就是你要查询的。

解决方案

根据您的架构,如果helpRequests仅包含requestor_idtopic_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 });

推荐阅读