当我尝试查询mongodb缝线时,我会遇到此错误:
错误:$ REGEX需要正则表达式 在客户端:557
这是我的代码:
const searchByName = async (data) => {
let db = await MongoConnect(),
collection_users = db.collection('users'),
users
console.log(data.name)
let regxName = new RegExp(data.name)
console.log(regxName)
try {
let users = await collection_users.find({ name: { $regex: regxName,
$options: 'i' } }).execute()
console.log(users)
} catch (error) {
return error
}
感谢您的帮助,此方法有效。(所有客户端(
import { BSON } from 'mongodb-stitch'
let bsonRegex = new BSON.BSONRegExp('pattern', 'i')
let users = await collection_users.find({ name: bsonRegex }).execute()
从模式返回结果
在mongodb缝合函数中,您必须使用bson.bsonregexp全局函数,该函数将其转换为校正缝制的格式。
这是一个示例:
exports = function(keyword) {
var mongodb = context.services.get("mongodb-atlas");
var coll = mongodb.db("DBNAME").collection("users");
return coll.find({
name: BSON.BSONRegExp(keyword, "i")
}).toArray();
};
并在客户端上称其为:
stitchClientPromise.then(stitchClient =>
stitchClient.executeFunction('FUNC_NAME', 'jhon')
).then(users => console.log('success: ', users))
.catch(e => console.log('error: ', e));