// TODO: Write doc to Mongo public MongoResponse update(MongoRequest request) { // I expect the updated documents here BsonDocument result = new BsonDocument { { "result", "Not Implemented" } }; return(new MongoResponse(result)); }
// TODO: Delete doc from Mongo public MongoResponse delete(MongoRequest request) { // I expect the IDs of documents to delete here BsonDocument result = new BsonDocument { { "result", "Not Implemented" } }; return(new MongoResponse(result)); }
// TODO: Get doc from Mongo public MongoResponse get(MongoRequest request) { // I expect an ID or position here BsonDocument result = new BsonDocument { { "result", "Not Implemented" } }; return(new MongoResponse(result)); }
// Connect to Mongo public void connect(MongoRequest request) { // If any of the parameters are empty, check if the appropriate environment variable is there LambdaLogger.Log("Contacting mongo"); if (dbClient == null) { dbClient = new MongoDB.Driver.MongoClient(request.connectionString); } }
public async Task <bool> updateDocuments(MongoRequest request) { if (request.documents != null) { return(await write(operation.updateDocuments, request)); } else { return(false); } }
async Task <bool> write(operation op, MongoRequest request) { LambdaLogger.Log(request.ToString()); request = this.checkRequest(request); connect(request); LambdaLogger.Log(dbClient.Cluster.Description.ToString()); IMongoCollection <BsonDocument> collection = dbClient.GetDatabase(request.database).GetCollection <BsonDocument>(request.collection); BsonDocument document = new BsonDocument(); List <BsonDocument> documents = new List <BsonDocument>(); BsonDocument filter = new BsonDocument(); switch (op) { case operation.createDocument: document = request.documents[0].ToBsonDocument(); await collection.InsertOneAsync(document); return(true); case operation.createDocuments: await collection.InsertManyAsync(request.documents); return(true); case operation.updateDocuments: filter = MongoDB.Bson.Serialization.BsonSerializer.Deserialize <BsonDocument>(request.query); BsonDocument update = request.documents[0]; var result = await collection.UpdateManyAsync(filter, update); if (result.ModifiedCount > 0) { return(true); } else { return(false); } // case operation.deleteDocuments: // filter = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(request.query); // DeleteResult deleteResult = await collection.DeleteManyAsync(filter); // if(deleteResult != null) { // } } return(false); }
public MongoRequest checkRequest(MongoRequest request) { LambdaLogger.Log($"dbClient is created: {dbClient != null}"); LambdaLogger.Log("checking connection settings"); LambdaLogger.Log($"database is empty: {(string.IsNullOrEmpty(request.database) && string.IsNullOrEmpty(this.database)).ToString()}"); if (string.IsNullOrEmpty(request.database)) { if (this.database != null) { request.database = this.database; } else { request.database = Environment.GetEnvironmentVariable("mongoDB"); } } LambdaLogger.Log($"database: {request.database}"); // If we're already connected, we keep that connection and don't need to check that part of the input if (dbClient == null) { // if no database is provided, use env var LambdaLogger.Log($"and connstring is empty: {string.IsNullOrEmpty(request.connectionString).ToString()}"); // if no connectionstring is provided, use env var if (string.IsNullOrEmpty(request.connectionString)) { if (this.connectionString != null) { request.connectionString = this.connectionString; } else { request.connectionString = $"{Environment.GetEnvironmentVariable("mongoConnectionString")}"; } } LambdaLogger.Log($"connstring: {request.connectionString}"); } // I realised the issue was because of no outbound traffic from lambda. //I modified it to be in the VPC that allowed outbound traffic and/ the connection to Mongo started working. return(request); }
// TODO: Get all docs from Mongo public async Task <object> getAll(APIGatewayProxyRequest request, ILambdaContext context = null) { // I expect nothing in here LambdaLogger.Log(request.ToJson().ToString()); NeARandFARBackEnd.RequestUtil requestUtil = new NeARandFARBackEnd.RequestUtil(); MongoRequest mongoRequest = new MongoRequest(requestUtil.checkRequest(request, new string[1] { "collection" })); JsonDocument docs = await client.getDocuments(mongoRequest); // return new {context = context}; return(new MongoResponse(new BsonDocument { { mongoRequest.collection, docs.ToString() } })); }
public async Task <JsonDocument> getDocuments(MongoRequest request) { LambdaLogger.Log(request.ToString()); request = this.checkRequest(request); connect(request); LambdaLogger.Log(dbClient.Cluster.Description.ToString()); IMongoCollection <BsonDocument> collection = dbClient.GetDatabase(request.database).GetCollection <BsonDocument>(request.collection); // Handle filters BsonDocument query = new BsonDocument(); if (request.query != null) { query = MongoDB.Bson.Serialization.BsonSerializer.Deserialize <BsonDocument>(request.query); } List <BsonDocument> docs = await collection.Find(query).Project("{_id: 0}").ToListAsync(); JsonDocument result = JsonDocument.Parse(docs.ToJson <List <BsonDocument> >()); return(result); }