[英]MongoDB Stitch SDK returns “aggregation stage ”$lookup“ is not supported”
最新的Stitch不支持$ lookup吗? 我正在使用mongodb-stitch-server-sdk@4.3.2,我的服务器版本为4.0.6。 我有一个类似以下的查询:
const {
Stitch,
UserPasswordCredential,
RemoteMongoClient
} = require('mongodb-stitch-server-sdk');
const client = Stitch.initializeDefaultAppClient('<APP ID>');
client.auth.loginWithCredential(new UserPasswordCredential("<username>","<password>")).then(user => {
client.close();
}).catch(err => {
console.log(err);
client.close();
})
mongodb = client.getServiceClient(
RemoteMongoClient.factory,
"fleet-home")
testQuery =
[{
$match: {
_id: "c1ba5c3f-263b-5748-9492-e50e0a39cb7a"
}
},
{
$lookup: {
from: "aircraft",
localField: "aircraft_id",
foreignField: "_id",
as: "aircraft"
}
}]
test = mongodb
.db("FleetDatabase")
.collection("fleet")
.aggregate(testQuery)
.asArray().then((success) => {
console.log(success)
})
但是,我收到UnhandledPromiseRejectionWarning: StitchServiceError: aggregation stage "$lookup" is not supported
错误UnhandledPromiseRejectionWarning: StitchServiceError: aggregation stage "$lookup" is not supported
如果您已经在使用Stitch,请查看Service Webhooks 。 Webhooks提供对Stitch函数的HTTP访问,Stitch函数支持$lookup
。
与使用SDK进行查询相比,Webhooks可能会更简单,因为在JS中管理管道会有些麻烦。 该SDK仍可以用于身份验证,但是通过Webhooks访问的数据可能会使生活更轻松。
MongoDB承诺10分钟的API设置 。 我花了一点时间,但不多。
您的Webhook函数可能类似于:
exports = function() {
var collection = context.services
.get("mongodb-atlas")
.db("FleetDatabase")
.collection("fleet");
var doc = collection
.aggregate([
{
$match: {
_id: "c1ba5c3f-263b-5748-9492-e50e0a39cb7a"
}
},
{
$lookup: {
from: "aircraft",
localField: "aircraft_id",
foreignField: "_id",
as: "aircraft"
}
}
])
.toArray();
return doc;
};
配置了Webhook之后,只需调用它即可。 如果您使用的是React,则如下所示:
async componentDidMount() {
const response = await fetch(MY_WEBHOOK);
const json = await response.json();
this.setState({ docs: json });
}
您可以创建一个Stitch函数来执行此操作,然后直接从您的itch客户var(已将其命名为client)调用它:
参见: https : //docs.mongodb.com/stitch/functions/define-a-function/
创建一个功能。 如果您以system user
身份运行功能,那么您对MongoDB CRUD和聚合操作的访问权限不受限制:
请参阅: https : //docs.mongodb.com/stitch/authentication/#system-users
然后调用该函数:
参见: https : //docs.mongodb.com/stitch/functions/call-a-function/
在您的应用中,它就像:
client.callFunction('functionName', args).then(response => {...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.