[英]Nodejs mongodb dynamic collection name
I have several mongodb collections, 我有几个mongodb集合,
Fruits: [ {}, {}, {} ...],
Bread: [{}, {}, {} ...]
I want to use dynamic collection name in my server like 我想在我的服务器中使用动态集合名称
// :collection will be "Fruits" or "Bread"
app.get('/fetch/:collection', function (req, res){
[req.params.collection].find({}, function(err, docs){
res.json(docs)
})
})
But as you know, [req.params.collection].find()...
isn't working. 但是如你所知,
[req.params.collection].find()...
无效。 How can I use dynamic collection name? 我如何使用动态集合名称?
You can use the different collections using db.collection('name').find({}) 您可以使用db.collection('name')来使用不同的集合.find({})
Here is the code i have tried 这是我试过的代码
App.js App.js
const express = require('express');
const bodyParser = require('body-parser');
const MongoClient = require("mongodb").MongoClient;
const assert = require('assert');
const url = 'mongodb://localhost:27017';
var db;
MongoClient.connect(url, { useNewUrlParser: true }, function (err, client) {
assert.equal(null, err);
console.log("Connected successfully to DB");
db = client.db('name of the db');
});
var app=express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/test/:collection', function(req,res){
let collection = req.params.collection;
console.log(collection);
db.collection(collection).find({}).toArray( function (err, result) {
res.send(result);
});
});
var port = 8000
app.listen(port, '0.0.0.0', () => {
console.log('Service running on port ' + port);
});
Hope this helps 希望这可以帮助
This is not working as the collection name you are passing is just a variable with no DB cursor. 这不起作用,因为您传递的集合名称只是一个没有DB光标的变量。
Here's what you can do here: 这是你在这里可以做的:
Create an index.js file, import all the models in that file with proper names as keys. 创建一个index.js文件,导入该文件中的所有模型,并使用适当的名称作为键。
eg: 例如:
const model = {
order: require("./order"),
product: require("./product"),
token: require("./token"),
user: require("./user")
}
module.exports = model;
Let's assume your request URL is now. 我们假设你的请求URL现在是。
/fetch/user
Now import this index.js as model in your controller file. 现在将此index.js导入为控制器文件中的模型。
eg: 例如:
const model = require("./index");
Now you can pass the key as params in your request like and use like it is stated below: 现在您可以将密钥作为params传递给您的请求,并使用如下所述:
app.get('/fetch/:collection', function (req, res){
model[req.params.collection].find({}, function(err, docs){
res.json(docs) // All user data.
})
})
Hope this solves your query! 希望这能解决您的疑问!
I will give solution for this but I don't know is this as per developer standards 我会为此提供解决方案,但我不知道这是否符合开发人员标准
let your model names, fruit.js and bread.js in model folder 让你的模型名称,fruit.js和bread.js在模型文件夹中
app.get('/fetch/:collection', function (req, res){
let collectionName = require(`./models/${req.params.collection}`)
collectionName.find({}, function(err, docs){
res.json(docs)
})
})
Hope this will work 希望这会奏效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.