gpt4 book ai didi

regex - 最后三个字母匹配字符串 "ces"

转载 作者:可可西里 更新时间:2023-11-01 09:35:12 25 4
gpt4 key购买 nike

我想知道如何在 mongodb 中仅使用其名称的最后三个字母来查找具有结构 db.collection.find() 的内容,谢谢!

  {  
"address": {
"building": "1007",
"coord": [ -73.856077, 40.848447 ],
"street": "Morris Park Ave",
"zipcode": "10462"
},
"borough": "Bronx",
"cuisine": "Bakery",
"grades": [
{ "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
{ "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
{ "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
{ "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
{ "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
],
"name": "Morris Park Bake Shop",
"restaurant_id": "30075445"
}

我目前的尝试:

db.mycollection.find({},{name:"ces"})

最佳答案

根据您的意思是“单词的最后三个字母” 还是“‘字段’的最后三个字母” 那么您通常需要 $regex

关于这个数据:

{ "_id" : ObjectId("570462448c0fd5187b53985a"), "name" : "Bounces" }
{ "_id" : ObjectId("5704624d8c0fd5187b53985b"), "name" : "Something" }
{ "_id" : ObjectId("5704625b8c0fd5187b53985c"), "name" : "Bounces Something" }

然后查询“word”,其中\b表示“word boundary”,在“string”表达式中通过\转义:

db.collection.find({ "name": { "$regex": "ces\\b" } })

哪些匹配:

{ "_id" : ObjectId("570462448c0fd5187b53985a"), "name" : "Bounces" }
{ "_id" : ObjectId("5704625b8c0fd5187b53985c"), "name" : "Bounces Something" }

或者查询“字段”,其中 $ 表示“从头开始”:

db.collection.find({ "name": { "$regex": "ces$" } })

哪些匹配:

{ "_id" : ObjectId("570462448c0fd5187b53985a"), "name" : "Bounces" }

关于regex - 最后三个字母匹配字符串 "ces",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36439225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com