Node.js 如何使用GraphQL和Mongodb编写lambda函数?
我是新来的GraphQL,我读了一篇关于GraphQL的文章,它和我预期的类似,但我不明白,如何在aws lambda函数中使用它。我有两个收藏1)用户帖子2)用户档案。查找以下收集数据以供参考 1) 用户发布集合Node.js 如何使用GraphQL和Mongodb编写lambda函数?,node.js,mongodb,aws-lambda,graphql,graphql-js,Node.js,Mongodb,Aws Lambda,Graphql,Graphql Js,我是新来的GraphQL,我读了一篇关于GraphQL的文章,它和我预期的类似,但我不明白,如何在aws lambda函数中使用它。我有两个收藏1)用户帖子2)用户档案。查找以下收集数据以供参考 1) 用户发布集合 _id :ObjectId("5d519f861c9d4400005ebd1b") userid : ObjectId("5d518caed55bc00001d235c1") media : "hello.jpg" type : "jpg" created : " " modifie
_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"
_id : ObjectId("5d518caed55bc00001d235c1")
username : "ramesh",
photo : " ",
created : " ",
modified : " "
_id : ObjectId("5d518da6d55bc00001d235c2")
username : "shekar",
photo : " ",
created : " ",
modified : " "
2) 用户配置文件集合
_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"
_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 : " "
您需要mongo min 3.6版 根据下面的
user\u posts
returnforget查询更新您的模式
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 })
});
图形ql
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您的mongodb版本是什么?为什么不用mongoose?@Ashok我用的是mongodb Atlasi如果你不是我的,你能帮我解决以下问题吗