【发布时间】: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 地图集
标签: node.js mongodb aws-lambda graphql graphql-js