我的 MongoDB 数据库中有多个集合。我有一个 angularJS 应用程序,它必须根据上下文发布到不同的集合。
如何在 $http 调用中指定集合的名称并拥有通用的 REST API?
$http函数:
$http({
method:"post",
url:"http://localhost:8080/insertIntoTable",
headers:"",
data:"data"
}).success(function(data, status, headers, config) {
$window.alert('Rest API successful');
}).error(function(data, status, headers, config) {
$window.alert('Unsuccessful');
});
后端的post方法:
app.post('/insertIntoTable',function(req,res){
//Establish Connection between client and server
MongoClient.connect(url,function(err,db){
//Connection Status Display
if(err)
console.log('Error while establishing connection with MongoDB',err);
else
console.log('Successfully established connection with MongoDB');
var collection = db.collection(collectionName);
collection.insert({ "name": "abc", "email": "xyz" });
db.close();
console.log('Connection with MongoDB is terminated');
})
});
在上面的代码中,我想在 $http 调用中传递变量的值:collectionName。我该怎么做?