我通过将字符串转换为 BSON 来进行 MongoDB 查找。在进行转换之前,我有没有办法确定我拥有的字符串是否是 Mongo 的有效 ObjectID?
这是我当前的 findByID 函数的咖啡脚本。它工作得很好,但如果我确定字符串不是 ID,我想通过不同的属性进行查找。
db.collection "pages", (err, collection) ->
collection.findOne
_id: new BSON.ObjectID(id)
, (err, item) ->
if item
res.send item
else
res.send 404
You can use a regular expression to test for that:
CoffeeScript
JavaScript