我有以下收藏。
[{ _id: ObjectId("5b17ab5489264c2df61ed2d6"), amount: 12514545 }]
125 并获取以下文档。
125
我试过这个,但不起作用:
db.collection.find({ amount: { $regex: 125 } })
如果可以通过聚合查询来完成就更好了。
Test.aggregate([ { "$match": { "$or": [ { "amount": { parseInt(searchString) }}, { "firstName": { "$regex": searchString }} ] } } ])
你可以用 $where
$where
db.test.find({$where: 'this.amount.toString().match("125")'})
但你不能用 总的来说 $match .
$match
amount 在用于正则表达式搜索的每个文档中。
amount
{ _id: ObjectId("5b17ab5489264c2df61ed2d6"), amount: 12514545, amountString: '12514545' }
然后您可以像搜索任何其他字符串字段一样搜索它:
Test.aggregate([ { "$match": { "$or": [ { "amountString": { "$regex": searchString }}, { "firstName": { "$regex": searchString }} ] } } ])