最新的缝合是不是不支持$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错误
发布于 2019-03-27 00:25:10
如果你已经在使用Stitch了,看看Service Webhooks吧。Webhooks提供了对缝合函数的超文本传输协议访问,并且缝合函数支持$lookup。
Webhooks可能会被证明比使用SDK进行查询更简单,因为在JS中管理管道变得有点复杂。SDK仍然可以用于身份验证,但通过webhook访问的数据可能会使工作变得更容易。
MongoDB承诺10-minute API setup。我花了稍微多一点的时间,但不是很多。
您的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 });
}发布于 2019-05-27 10:45:43
您可以创建一个Stitch函数来执行此操作,并直接从您的缝合客户端变量(已命名为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 => {...https://stackoverflow.com/questions/55214322
复制相似问题