簡體   English   中英

如何匹配mongoquery中的浮點值?

[英]How to match float value in mongoquery?

這是我的 MongoDB 示例文檔集。 我需要一個預期的結果,以便任何人指導我解決我的問題

/* 1 */
{
  "_id" : 1.0,
  "user_id" : "5c592f6716209a24f4125001",
  "tu_code" : "5"
}
/* 2 */
{
  "_id" : 2.0,
  "user_id" : "5c592f6716209a24f4125002",
  "tu_code" : "50"
}
/* 3 */
{
  "_id" : 3.0,
  "user_id" : "5c592f6716209a24f4125003",
  "tu_code" : "50.21"
}
/* 4 */
{
  "_id" : 4.0,
  "user_id" : "5c592f6716209a24f4125004",
  "tu_code" : "50.22"
}
/* 5 */
{
  "_id" : 5.0,
  "user_id" : "5c592f6716209a24f4125005",
  "tu_code" : "52"
}
/* 6 */
{
  "_id" : 3.0,
  "user_id" : "5c592f6716209a24f4125006",
  "tu_code" : "5.1"
}
/* 7 */
{
  "_id" : 6.0,
  "user_id" : "5c592f6716209a24f4125007",
  "tu_code" : "5.1.1"
}
/* 8 */
{
  "_id" : 7.0,
  "user_id" : "5c592f6716209a24f4125008",
  "tu_code" : "5.2"
}
/* 9 */
{
  "_id" : 8.0,
  "user_id" : "5c592f6716209a24f4125009",
  "tu_code" : "5.2.1"
}

在這里,我在下面提到了我的 mongo聚合查詢

[ 
    { "$project": { 
        "tu_code": { "$toLower": "$tu_code" }, 
    }}, 
    { "$match": { "tu_code": {"$regex": "^5.*"}}}
]

上述查詢產生以下結果

    | _id| tu_code |

    | ---| --------|

    | 1  | 5     |
    | 2  | 50    |
    | 3  | 50.21 |
    | 4  | 50.22 |
    | 5  | 52    |
    | 6  | 53    |
    | 7  | 5.1   |
    | 8  | 5.1.1 |
    | 9  | 5.2   |
    | 10 | 5.2.1 |

但我想要下面給出的預期結果

    | _id| tu_code |

    | ---| --------|

    | 1  | 5     |
    | 7  | 5.1   |
    | 8  | 5.1.1 |
    | 9  | 5.2   |
    | 10 | 5.2.1 |

任何人都可以幫我解決這個問題,我不知道如何得到我的預期結果。

  • 您可以在搜索表達式的末尾使用\b (單詞邊界)運算符,
[
  {
    "$match": {
      "tu_code": {
        "$regex": "^5\\b"
      }
    }
  }
]

操場

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM