[英]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.