首页 文章

Mongoose:在数组中查找标签并返回匹配的文档

提问于
浏览
6

在我目前的Mongo DB中,我有一个简单的Parts集合,具有Parent Children关系,如下所示:

{"partcode": "Parent1","children": ["Child1","Child2","Child3"]}

{"partcode": "Child1","label": ["label1"]}
{"partcode": "Child2","label": ["label1"]}
{"partcode": "Child3","label": ["label1"]}

为了返回partcode的子代,我使用以下Mongoose函数:

PartSchema.static('getChildren', function(query, callback) {
  var self = this,
  self.findOne(query, {children: 1, _id: 0})
    .exec(function(err, doc) {
      return (self.find({
        "partcode": {
          "$in": doc.children
        }
      }, {_id: 0}, callback));
    });
});

这将返回以下数组:

[{"partcode": "Child1","label": ["label1"]},
{"partcode": "Child2","label": ["label1"]},
{"partcode": "Child3","label": ["label1"]}]

我想实现一个标签系统,我可以将标签指定为元子,并使代码返回与该标签匹配的所有子标签 .

{"partcode": "Parent1","children": ["*label1"]}

会回来:

[{"partcode": "Child1","label": ["label1"]},
{"partcode": "Child2","label": ["label1"]},
{"partcode": "Child3","label": ["label1"]}]

我将在父文档的子字段中指定一个标签,以特殊字符开头(目前,我正在使用'*',但如果需要,我很乐意将其更改为其他内容) .

伪代码:

  • 找到父母

  • 获取Parent的 children 数组

  • 在数组中,如果子项以标签字符开头

  • 获取与label和 . 匹配的所有零件代码的数组

  • 替换 children 数组中标签的零件代码

  • 返回 children 数组

不应以标签字符开头的儿童也应退回 .

2 回答

  • 0

    我按照以下方式工作:

    PartSchema.static('getChildren', function(query, callback) {
      var self = this,
        children = [],
        labels = [];
      self.findOne(query, {children: 1, _id: 0})
        .exec(function(err, doc) {
          //find labels
          labels = _.filter(doc.children, obj => /^\*/.test(obj));
          //remove labels from children array
          children = _.difference(doc.children, labels);
          //remove label identifier '*'
          labels = _.map(labels, label => label.substring(1));
          self.find({
            vendor: vendor,
            $or: [{
              "partcode": {
                "$in": children
              }
            }, {
              "label": {
                "$in": labels
              }
            }]
          }, {_id: 0}, callback);
        });
    });
    

    我对评论很感兴趣 . 特别是在优雅,结构,惯例等方面 . 任何形状或丑陋的东西都会让你眼前一亮?

  • 1

    跟进OP's self answer

    这里不需要特殊字符( * )来区分标签和子字段 . 这也将减少您使用的阵列操作( filterdifferencemap ) .

    因此,假设您没有标签的特殊字符,您的收藏将如下所示:

    {"partcode": "Parent1","children": ["label1"]}
    

    [{"partcode": "Child1","label": ["label1"]},
    {"partcode": "Child2","label": ["label1"]},
    {"partcode": "Child3","label": ["label1"]}]
    

    因此,下面的代码将为您提供所需的结果:

    PartSchema.static('getChildren', function(query, callback) {
      var self = this,
        children = [],
        labels = [];
      self.findOne(query, {children: 1, _id: 0})
        .exec(function(err, doc) {
    // simply find for children in both `label` and `partcode` fields,
    // using whole children array
              self.find({
                vendor: vendor,
                $or: [{
                  "partcode": {
                    "$in": doc.children
                  }
                }, {
                  "label": {
                    "$in": doc.children
                  }
                }]
              }, {_id: 0}, callback);
            });
        });
    

    Note :如果 doc 中的 child 字段不是另一个 doc 中的 label 字段,则此解决方案适用,反之亦然 .

相关问题