如何从以特定文本开头且仅以 mongodb 中的数字结尾的条目中获取数字数组?
How to get numbers array from entries that begins with certain text and ends only with numbers in mongodb?
我在 mongodb:
中存储了这样的文档
{
name: "1234book"
}
{
name: "book8"
}
{
name: "book2"
}
{
name: "book2some6text"
}
{
name: "smallbook2some6text"
}
{
name: "smallbook22"
}
如何获取以特定文本开头且仅以数字结尾的条目?
例如:
海峡:"book"
预期结果:
{
numbers: [8, 2]
}
您可以通过触发此查询来实现:
db.collection.find({name: {$regex: /^book(.*)[\d]$/}}).map(function(record){
return NumberInt(record.name.match(/[\d]+$/)[0]);
});
我在 mongodb:
中存储了这样的文档{
name: "1234book"
}
{
name: "book8"
}
{
name: "book2"
}
{
name: "book2some6text"
}
{
name: "smallbook2some6text"
}
{
name: "smallbook22"
}
如何获取以特定文本开头且仅以数字结尾的条目?
例如:
海峡:"book"
预期结果:
{
numbers: [8, 2]
}
您可以通过触发此查询来实现:
db.collection.find({name: {$regex: /^book(.*)[\d]$/}}).map(function(record){
return NumberInt(record.name.match(/[\d]+$/)[0]);
});