我想知道如何在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"})
答案 0 :(得分:2)
取决于您是指"单词" 或"的最后三个字母,''& #34; 那么你通常需要一个$regex
关于这些数据:
{ "_id" : ObjectId("570462448c0fd5187b53985a"), "name" : "Bounces" }
{ "_id" : ObjectId("5704624d8c0fd5187b53985b"), "name" : "Something" }
{ "_id" : ObjectId("5704625b8c0fd5187b53985c"), "name" : "Bounces Something" }
然后查询"字",其中\b
表示"字边界",并在"字符串&#中由\
转义34;表达式:
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" }
答案 1 :(得分:0)
尝试以下任何一种方法
: db.mycollection.find({name: /ces$/})
db.hotels.find({name: {$regex: "ces$"}})
db.hotels.find({name: {$regex: /ces$/}})