如何使用 GraphQL 和 Mongodb 编写 lambda 函数?
Posted
技术标签:
【中文标题】如何使用 GraphQL 和 Mongodb 编写 lambda 函数?【英文标题】:How to write lambda function using GraphQL and Mongodb? 【发布时间】:2019-12-24 18:33:51 【问题描述】:我是新的 GraphQL,我阅读了有关 GraphQL 的文章,它与我的预期相似,但我不明白如何在 aws lambda 函数中使用它。我有两个集合 1)user_posts 2)user_profile。查找以下收集数据供您参考。
1) user_posts 集合
_id :ObjectId("5d519f861c9d4400005ebd1b")
userid : ObjectId("5d518caed55bc00001d235c1")
media : "hello.jpg"
type : "jpg"
created : " "
modified : " "
like : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "like"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "happy"
comment : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
comment : "hello"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
comment : "welcome"
share : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "shared"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "shared"
2) User_profile 集合
_id : ObjectId("5d518caed55bc00001d235c1")
username : "ramesh",
photo : " ",
created : " ",
modified : " "
_id : ObjectId("5d518da6d55bc00001d235c2")
username : "shekar",
photo : " ",
created : " ",
modified : " "
通常我使用 lambda 函数来获得这样的输出。但它没有得到我预期的输出。
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
var databasename = "trans_db";
var db;
exports.handler = (event, context, callback) =>
var Userid = event['userid'];
var uid = ObjectId(Userid);
MongoClient.connect(uri, useNewUrlParser: true , (error, client) =>
if (error) return 1; // Checking the connection
console.log('Connection Successful');
db = client.db(databasename);
db.collection("user_posts").find("userid" : uid).toArray(function(err,
res)
if (err) throw err;
context.succeed(res);
);
);
;
我需要下面这样的输出。
_id :ObjectId("5d519f861c9d4400005ebd1b")
userid : ObjectId("5d518caed55bc00001d235c1")
username : "ramesh"
photo : " ",
media : "hello.jpg"
type : "jpg"
created : " "
modified : " "
like : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "like"
username : "ramesh"
photo : " "
1 : Object
username : "shekar"
photo : " "
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "happy"
username : "shekar"
photo : " "
comment : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
comment : "hello"
username : "ramesh"
photo : " "
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
comment : "welocme"
username : "shekar"
photo : " "
share : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "shared"
username : "ramesh"
photo : " "
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "shared"
username : "shekar"
photo : " "
【问题讨论】:
您的 MongoDB 版本是多少?为什么不使用猫鼬? @Ashok 我正在使用 mongodb 地图集 【参考方案1】:您需要 mongo min 3.6 版。
根据user_posts
更新您的架构基础以获取下面的查询。
MongoDB查询
MongoClient.connect(uri, useNewUrlParser: true , (error, client) =>
if (error) return 1; // Checking the connection
console.log('Connection Successful');
db = client.db(databasename);
db.collection("user_posts").aggregate(
$match: "userid" : uid,
$unwind: '$like' ,
$lookup: from: "users", localField: "like.userid", foreignField: "_id", as: "users" ,
$group:
_id: "$_id",
like: $push: $mergeObjects: ['$like', $arrayElemAt: [ "$users", 0 ] ],
data: $first: "$$ROOT"
,
$replaceRoot: newRoot: $mergeObjects: ['$data', like: "$like" ]
$unwind: '$comment' ,
$lookup: from: "users", localField: "comment.userid", foreignField: "_id", as: "users" ,
$group:
_id: "$_id",
comment: $push: $mergeObjects: ['$comment', $arrayElemAt: [ "$users", 0 ] ],
data: $first: "$$ROOT"
,
$replaceRoot: newRoot: $mergeObjects: ['$data', comment: "$comment" ]
$unwind: '$share' ,
$lookup: from: "users", localField: "share.userid", foreignField: "_id", as: "users" ,
$group:
_id: "$_id",
share: $push: $mergeObjects: ['$share', $arrayElemAt: [ "$users", 0 ] ],
data: $first: "$$ROOT"
,
$replaceRoot: newRoot: $mergeObjects: ['$data', share: "$share" ] ,
$project: users: 0
).then(res =>
context.succeed(res);
).catch(error =>
console.log( error )
);
GraphQL
user_posts
_id
userid
username
photo
media
type
created
modified
like
userid
username
status
photo
comment
userid
username
status
photo
share
userid
username
status
photo
graphql 中解析器的变化基于 graphql 中的查询,我们可以简化 mongodb 查询,但现在我们必须专注于 Graphql
【讨论】:
如果你不是我的,请你帮忙解决以下问题***.com/questions/57629210/…以上是关于如何使用 GraphQL 和 Mongodb 编写 lambda 函数?的主要内容,如果未能解决你的问题,请参考以下文章
GraphQL、Relay 和 Mongodb (mongoose) 如何获取数组
如何在不创建 Mongoose 模型的情况下将 GraphQL 与 Mongoose 和 MongoDB 一起使用
如何在使用 graphql 和 mongodb 的查询中使用“where”参数?
GraphQL 如何使用 Nestjs 或 type-graphql 设置查询 MongoDB ref 集合