简体   繁体   中英

MongoDB Stitch SDK returns “aggregation stage ”$lookup“ is not supported”

Is the latest Stitch not supporting $lookup? I'm using mongodb-stitch-server-sdk@4.3.2 and my server is on version 4.0.6. I have a query like the following:

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)
})

However, I am getting an error of UnhandledPromiseRejectionWarning: StitchServiceError: aggregation stage "$lookup" is not supported

If you're already using Stitch, take a look at Service Webhooks . Webhooks provide HTTP access to Stitch functions, and Stitch functions support $lookup .

Webhooks may prove simpler than queries with the SDK because managing pipelines in JS gets a bit convoluted. The SDK can still be used for authentication, but data accessed with webhooks might make life easier.

MongoDB promises 10-minute API setup . It took me slightly longer, but not much.

Your Webhook function might look something like:

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;
};

After your webhook is configured you just have to call it. If you're using React, that would look like this:

  async componentDidMount() {
    const response = await fetch(MY_WEBHOOK);
    const json = await response.json();
    this.setState({ docs: json });
  }

You can create a Stitch function to do this, and call it directly from your stitch client var (which you have named client):

See: https://docs.mongodb.com/stitch/functions/define-a-function/

to create a function. If you run your function as a system user , then you have unrestricted access to MongoDB CRUD and aggregation operations:

See: https://docs.mongodb.com/stitch/authentication/#system-users

Then to call the function:

See: https://docs.mongodb.com/stitch/functions/call-a-function/

In your app it would be like:

client.callFunction('functionName', args).then(response => {...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM