【问题标题】:simple join SQL like with mongoose像猫鼬一样的简单连接 SQL
【发布时间】:2020-04-26 03:56:50
【问题描述】:

我有文档和参数,我正在尝试构建一个查询来选择与参数匹配的文档。

document {
_id : ...,
status : ...,
type : ...,
...
}
parameter {
_id : ...,
parameter : ...,
status : ...,
type : ...,
}

我确实认为 SQL 会害死我。 我该怎么做:

select document.* from document,parameter 
  where document.type = parameter.type and document.status = parameter.status
         and parameter.parameter="example"

我可能不认为是正确的方法 ex :我不使用两个对象之间的任何引用链接,我可能会,但它会是一个 N 到 N 链接,我不觉得这很容易维护作为参数或文档将被更新。

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema mongoose-populate


    【解决方案1】:

    请尝试以下查询:

    db.document.aggregate([
       {
          $lookup:
             {
               from: "parameter",
               let: { type: "$type", status: "$status" },
               pipeline: [
                  { $match:
                     { $expr:
                        { $and:
                           [
                             { $eq: [ "$parameter",  "example" ] },
                             { $eq: [ "$type",  "$$type" ] },
                             { $eq: [ "$status", "$$status" ] }
                           ]
                        }
                     }
                  }
               ],
               as: "documentParameterJoined"
             }
        }
    ])
    

    文档收集数据:

    /* 1 */
    {
        "_id" : ObjectId("5e160929627ef782360f69aa"),
        "status" : "mystatus",
        "type" : "whattype"
    }
    
    /* 2 */
    {
        "_id" : ObjectId("5e160931627ef782360f6abb"),
        "status" : "mystatus1",
        "type" : "whattype1"
    }
    

    参数收集数据:

    /* 1 */
    {
        "_id" : ObjectId("5e16095f627ef782360f6e1d"),
        "parameter" : "example",
        "status" : "mystatus",
        "type" : "whattype"
    }
    
    /* 2 */
    {
        "_id" : ObjectId("5e16097e627ef782360f70b5"),
        "parameter" : "example",
        "status" : "mystatus1",
        "type" : "whattype1"
    }
    
    /* 3 */
    {
        "_id" : ObjectId("5e160985627ef782360f7152"),
        "parameter" : "example1",
        "status" : "mystatus",
        "type" : "whatType"
    }
    
    /* 4 */
    {
        "_id" : ObjectId("5e160a39627ef782360f8027"),
        "parameter" : "example",
        "status" : "mystatus1",
        "type" : "whatType"
    }
    
    /* 5 */
    {
        "_id" : ObjectId("5e160a42627ef782360f80ec"),
        "parameter" : "example",
        "status" : "mystatus",
        "type" : "whatType1"
    }
    

    结果:

     // You don't see docs 3 to 5 as they don't match any filter criteria.
    /* 1 */
    {
        "_id" : ObjectId("5e160929627ef782360f69aa"),
        "status" : "mystatus",
        "type" : "whattype",
        "documentParameterJoined" : [ 
            {
                "_id" : ObjectId("5e16095f627ef782360f6e1d"),
                "parameter" : "example",
                "status" : "mystatus",
                "type" : "whattype"
            }
        ]
    }
    
    /* 2 */
    {
        "_id" : ObjectId("5e160931627ef782360f6abb"),
        "status" : "mystatus1",
        "type" : "whattype1",
        "documentParameterJoined" : [ 
            {
                "_id" : ObjectId("5e16097e627ef782360f70b5"),
                "parameter" : "example",
                "status" : "mystatus1",
                "type" : "whattype1"
            }
        ]
    }
    

    您不需要进行两次数据库调用,您可以使用$lookup(mongoDB 原生)和表达式来实现所需。您也可以尝试使用mongoose populate 进行类似操作(这是对 mongoDB $lookup 的一种包装器)

    参考: $lookup , mongoose-populate

    【讨论】:

      【解决方案2】:

      我想出了这个问题,但我仍然愿意接受建议。

      parameter.find({parameter :"example"}, function(err, parameters){
        var parameters_status;
        var parameters_type;
      
        if (err) {
          // do error handling
          return;
        }
      
        parameters_status= parameters.map(function (parameters) { return parameters.status; });
        parameters_type= parameters.map(function (parameters) { return parameters.type; });
      
        document.find({type: {$in: parameters_type},status : {$in: parameters_status} }, function (err, documents) {
          if (err) {
            // do error handling
            return;
          }
      
          console.log(documents);
          // do something with the documents
        });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-13
        • 1970-01-01
        • 1970-01-01
        • 2015-12-30
        • 1970-01-01
        • 1970-01-01
        • 2017-02-07
        相关资源
        最近更新 更多