是的,是的。你可以做到的。
你可以创建一个函数:
exports.joinRetailCollection = (collectionName, collectionJoinName, foreignFieldName, asFieldName) => state.db.collection(collectionName).aggregate([
{
$lookup:
{
from: collectionName,
localField: collectionJoinName,
foreignField: foreignFieldName,
as: asFieldName
}
}
]);
from: 'user',
localField: 'products',
foreignField: 'sku',
as: 'inventory'
并导入
enter code here
const {db} = require('../route');
并创建一个变量
const userLink = db.joinRetailCollection('user', .....) //函数的参数。
将此添加到您的 mongo-connector.js 文件中
const MongoClient = require('mongodb').MongoClient
const state = {
db: null,
}
exports.connect = async (url, done) => {
if (state.db) return done()
MongoClient.connect(url, {
connectTimeoutMS: 200,
retryWrites: true,
useNewUrlParser: true,
useUnifiedTopology: true }, (err, client) => {
if (err) return done(err)
state.db = client.db('wp_test')
done()
})
}`
exports.getCollection = (collectionName) => state.db.collection(collectionName);
然后是函数
您还可以在同一文件中添加一个仅用于收集
希望这对你有用。
亲切的问候