0

当我尝试查询 Mongodb Stitch 时出现此错误:

错误:$regex 需要 client.js: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
}
4

2 回答 2

1

感谢您的帮助,此方法有效。(所有客户端)

import { BSON } from 'mongodb-stitch'

let bsonRegex = new BSON.BSONRegExp('pattern', 'i')

let users = await collection_users.find({ name: bsonRegex }).execute()

从模式返回结果

于 2018-04-04T03:56:56.200 回答
0

在 MongoDB Stitch Functions 中,您必须使用BSON.BSONRegExp全局函数将其转换为 Stitch 的正确格式。

这是一个例子:

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));
于 2018-04-03T08:19:08.053 回答