找到与嵌套数组完全匹配的顺序并不重要

时间:2017-06-23 16:58:17

标签: javascript arrays node.js mongodb mongodb-query

假设这个数据:

Dim i As Long
Dim wb As Workbook

Do

Application.Wait Now + TimeValue("00:00:01")

    For i = 1 To Workbooks.Count

        If Workbooks(i).Name <> ThisWorkbook.Name And Workbooks(i).Name <> "PERSONAL.XLSB" Then
            Set wb = Workbooks(i)
            Exit Do
        End If

    Next

Loop

我正在尝试在&#34; hashSets&#34;上查询完全匹配。但是&#34; hashSets&#34;中对象的顺序。数组可能不同,数据数组中的哈希顺序可能不同。

我发现这篇文章有点类似:MongoDB Find Exact Array Match but order doesn't matter

虽然使用$ size和$ all是有意义的,但我不太确定如何将它应用于嵌套数组/对象。

我使用的是这个简单的查询,但似乎订单必须相同:

{
    "_id" : ObjectId("594d43d1da7887921c701575"),
    "hashSets" : [ 
        {
            "data1" : [ 
                "2bf2cededea424f47d64d33c511dd431", 
                "40499da7ee1456a2f79dc39a718aaf9b"
            ],
            "data2" : [ 
                "b068931cc450442b63f5b3d276ea4297",
                "16ebd6af0cadece819da7468a9fd79f4", 
                "8717cfca734e8987971f63b20eeb8024"
            ]
        },
        {
            "data1" : [ 
                "098F6BCD4621D373CADE4E832627B4F6", 
                "5A105E8B9D40E1329780D62EA2265D8A", 
                "AD0234829205B9033196BA818F7A872B"
            ],
            "data2" : [ 
                "8AD8757BAA8564DC136C1E07507F4A98"
            ]
        }
    ],
    "__v" : 0
}

编辑: 为清楚起见:

db.analysis.find({hashSets:input.hashSets})

请注意,数据的内容相同,但各种数组中的数据顺序已更改。希望更有意义。

1 个答案:

答案 0 :(得分:2)

您似乎在谈论$elemMatch$all的需求。

例如,如果我改变样本的结构以反转几个元素的顺序:

[
        {
                "data2" : [
                        "8AD8757BAA8564DC136C1E07507F4A98"
                ],
                "data1" : [
                        "098F6BCD4621D373CADE4E832627B4F6",
                        "5A105E8B9D40E1329780D62EA2265D8A",
                        "AD0234829205B9033196BA818F7A872B"
                ]
        },
        {
                "data2" : [
                        "b068931cc450442b63f5b3d276ea4297",
                        "16ebd6af0cadece819da7468a9fd79f4",
                        "8717cfca734e8987971f63b20eeb8024"
                ],
                "data1" : [
                        "2bf2cededea424f47d64d33c511dd431",
                        "40499da7ee1456a2f79dc39a718aaf9b"
                ]
        }
]

然后我可以在$elemMatch条件中使用$all来匹配数组的元素,两者的顺序相反,“keys”的顺序不同:

db.hashsets.find({
  "hashSets": { 
    "$all": input.hashSets.map( e => ({ "$elemMatch": e  }) ) 
  }
})

哪会产生“查询”,如:

{ 
  "hashSets": { "$all": 
      [
        {
                "$elemMatch" : {
                        "data2" : [
                                "8AD8757BAA8564DC136C1E07507F4A98"
                        ],
                        "data1" : [
                                "098F6BCD4621D373CADE4E832627B4F6",
                                "5A105E8B9D40E1329780D62EA2265D8A",
                                "AD0234829205B9033196BA818F7A872B"
                        ]
                }
        },
        {
                "$elemMatch" : {
                        "data2" : [
                                "b068931cc450442b63f5b3d276ea4297",
                                "16ebd6af0cadece819da7468a9fd79f4",
                                "8717cfca734e8987971f63b20eeb8024"
                        ],
                        "data1" : [
                                "2bf2cededea424f47d64d33c511dd431",
                                "40499da7ee1456a2f79dc39a718aaf9b"
                        ]
                }
        }
      ]
  }
}

如果您需要进一步了解实际的“键”,请更改映射或输入以执行此操作:

db.hashsets.find({ 
  "hashSets": { 
    "$all": input.hashSets.map( e => 
      ({ "$elemMatch": { "data2": { "$all": e.data2  }, "data1": { "$all": e.data1 } }}) )
  }
})

生成如下查询:

{ 
  "hashSets": { "$all": 
      [
        {
                "$elemMatch" : {
                        "data2" : { "$all": [
                                "8AD8757BAA8564DC136C1E07507F4A98"
                        ]},
                        "data1" : { "$all": [
                                "098F6BCD4621D373CADE4E832627B4F6",
                                "5A105E8B9D40E1329780D62EA2265D8A",
                                "AD0234829205B9033196BA818F7A872B"
                        ]}
                }
        },
        {
                "$elemMatch" : {
                        "data2" : { "$all": [
                                "b068931cc450442b63f5b3d276ea4297",
                                "16ebd6af0cadece819da7468a9fd79f4",
                                "8717cfca734e8987971f63b20eeb8024"
                        ]},
                        "data1" : { "$all": [
                                "2bf2cededea424f47d64d33c511dd431",
                                "40499da7ee1456a2f79dc39a718aaf9b"
                        ]}
                }
        }
      ]
  }
}

或最灵活,包括尺寸:

  db.hashsets.find({ 
    "hashSets": {
      "$all": input.hashSets.map( e => {
        var obj = {};
        Object.keys(e).forEach(k => {
          obj[k] = { "$all": e[k], "$size": e[k].length }
        });

        return { "$elemMatch": obj };
      }),
      "$size": input.hashSets.length
    }
  })

“完整”当然是这样的:

{
        "hashSets" : {
                "$all" : [
                        {
                                "$elemMatch" : {
                                        "data2" : {
                                                "$all" : [
                                                        "8AD8757BAA8564DC136C1E07507F4A98"
                                                ],
                                                "$size" : 1
                                        },
                                        "data1" : {
                                                "$all" : [
                                                        "098F6BCD4621D373CADE4E832627B4F6",
                                                        "5A105E8B9D40E1329780D62EA2265D8A",
                                                        "AD0234829205B9033196BA818F7A872B"
                                                ],
                                                "$size" : 3
                                        }
                                }
                        },
                        {
                                "$elemMatch" : {
                                        "data2" : {
                                                "$all" : [
                                                        "b068931cc450442b63f5b3d276ea4297",
                                                        "16ebd6af0cadece819da7468a9fd79f4",
                                                        "8717cfca734e8987971f63b20eeb8024"
                                                ],
                                                "$size" : 3
                                        },
                                        "data1" : {
                                                "$all" : [
                                                        "2bf2cededea424f47d64d33c511dd431",
                                                        "40499da7ee1456a2f79dc39a718aaf9b"
                                                ],
                                                "$size" : 2
                                        }
                                }
                        }
                ],
                "$size" : 2
        }
}