首页 > 解决方案 > MongoDB查询最相似的数组

问题描述

我在一个 MongoDB 集合中有一堆文档,这些文档有一个指纹,它由 8 个布尔值组成。我想构建一个查询,该查询将为我提供具有 7 到 8 个布尔值的文档,这些布尔值与我的查询以相同的顺序排列。

所以我的查询将是以下内容:查找所有具有

fingerPrint = ["true", "false", "true", "true", "true", "true", "false", "true"   ] 

该查询将返回两个文档,因为第一个文档在序列中的所有布尔值都是正确的,而第二个文档在序列中的 8 个布尔值中有 7 个是正确的。

{ 
    "_id" : ObjectId("5538e75c3cea103b25ff94a3"),
    "name" : "document1",
    "fingerPrint" : [
        "true",
        "false",
        "true",
        "true",
        "true",
        "true",
        "false",
        "true"
    ]
},
{ 
    "_id" : ObjectId("5538e75c3cea103b25ff94a4"),
    "name" : "document2",
    "fingerPrint" : [
        "true",
        "false",
        "true",
        "true",
        "false",
        "true",
        "false",
        "true"
    ]
}

我该怎么做呢?

或者:是否有更好的方法来存储位数组并能够更优化地查询集合?

标签: mongodbmongodb-query

解决方案


推荐阅读