Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript GraphQL:获取部分字符串匹配_Javascript_Mongodb_Graphql - Fatal编程技术网

Javascript GraphQL:获取部分字符串匹配

Javascript GraphQL:获取部分字符串匹配,javascript,mongodb,graphql,Javascript,Mongodb,Graphql,我正在通过graphQL访问mongoDB集合。这是数据库数据: { "_id" : ObjectId("59ee1be762494b1df1dfe30c"), "itemId" : 1, "item" : "texture", "__v" : 0 } { "_id" : ObjectId("59ee1bee62494b1df1dfe30d"), "itemId" : 1, "item" : "pictures", "__v" :

我正在通过graphQL访问mongoDB集合。这是数据库数据:

{
    "_id" : ObjectId("59ee1be762494b1df1dfe30c"),
    "itemId" : 1,
    "item" : "texture",
    "__v" : 0
}
{
    "_id" : ObjectId("59ee1bee62494b1df1dfe30d"),
    "itemId" : 1,
    "item" : "pictures",
    "__v" : 0
}
查询
{todo(item:“texture”){itemId,item}
生成

{
  "data": {
    "todo": [
      {
        "itemId": 1,
        "item": "texture"
      }
    ]
  }
}
但我需要找到部分匹配给定字符串的数据集。 因此字符串
tur
应产生两个数据集:texture,pictures

我的graphQL模式如下所示:

var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      todo: {
        type: new GraphQLList(todoType),
        args: {
          item: {
            name: 'item',
            type: new GraphQLNonNull(GraphQLString)
          }
        },
        resolve: (root, {item}, source, fieldASTs) => {
          var projections = getProjection(fieldASTs)
          var foundItems = new Promise((resolve, reject) => {
            ToDoMongo.find({item}, projections, (err, todos) => {
              err ? reject(err) : resolve(todos)
            })
          })
          return foundItems
        }
      }
    }
  })
})

通常,您必须按照以下方式构造(mongo)查询:

{ "item": /tur/ }

这是
$regex
运算符的简短语法。您应该在
ToDoMongo.find
方法代码中构造此查询。

通常,您必须按照以下方式构造(mongo)查询:

{ "item": /tur/ }

这是
$regex
运算符的简短语法。您应该在
ToDoMongo.find
方法代码中构造此查询。

您可以粘贴
ToDoMongo.find
方法代码吗?我们需要知道您是如何构造查询的,因为为了支持所需内容,必须对其进行调整。@cbartosiak在发布的架构中,您可以看到resolve()代码。在这里您可以看到
ToDoMongo.find()
。所以我必须使用正则表达式?是的,正则表达式是Mongo
find
方法的输入。Mongo有责任做你感兴趣的工作。你能将
粘贴到Mongo上吗?查找
方法代码吗?我们需要知道您是如何构造查询的,因为为了支持所需内容,必须对其进行调整。@cbartosiak在发布的架构中,您可以看到resolve()代码。在这里您可以看到
ToDoMongo.find()
。所以我必须使用正则表达式?是的,正则表达式是Mongo
find
方法的输入。做你感兴趣的工作是Mongo的责任。