Javascript GraphQL:获取部分字符串匹配
我正在通过graphQL访问mongoDB集合。这是数据库数据: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" :
{
"_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()
。所以我必须使用正则表达式?是的,正则表达式是Mongofind
方法的输入。Mongo有责任做你感兴趣的工作。你能将粘贴到Mongo上吗?查找方法代码吗?我们需要知道您是如何构造查询的,因为为了支持所需内容,必须对其进行调整。@cbartosiak在发布的架构中,您可以看到resolve()代码。在这里您可以看到ToDoMongo.find()
。所以我必须使用正则表达式?是的,正则表达式是Mongofind
方法的输入。做你感兴趣的工作是Mongo的责任。