Unable to use $lookup stage in aggregation pipeline in mongodb4.4

Hi,

When I am trying to use lookup stage in my aggregation pipeline with Java for mongodb 4.4 (I am using free tier) , I am getting following error :

com.mongodb.MongoCommandException: Command failed with error 8000 (AtlasError): ‘pipeline is not allowed in this atlas tier’ on server mflix-shard-00-02.7jsv0.mongodb.net:27017. The full response is { “ok” : 0, “errmsg” : “pipeline is not allowed in this atlas tier”, “code” : 8000, “codeName” : “AtlasError” }

I am using the following java code :

List<? extends Bson> lookupStage = Arrays.asList(new Document(
“pipeline”,Arrays.asList(
new Document(“$match”,
new Document(“$expr”,
new Document(“$eq”,Arrays.asList(movieId,“movie_id”)))),
new Document(“$sort”,new Document(“date”,-1L)))));

    Bson lookup = Aggregates.lookup("comments",lookupStage,"comments");

Hi @Prakhar_Khanna and welcome in the MongoDB Community :muscle: !

There is no such limitation on M0 Free Tier in Atlas.

Here is a piece of code that proves it:

package com.mongodb.quickstart;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.json.JsonWriterSettings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

import static com.mongodb.client.model.Aggregates.lookup;
import static com.mongodb.client.model.Aggregates.match;
import static com.mongodb.client.model.Filters.eq;
import static java.util.Arrays.asList;

public class AggregationFrameworkCommunity {

    public static void main(String[] args) {
        String connectionString = System.getProperty("mongodb.uri");
        try (MongoClient mongoClient = MongoClients.create(connectionString)) {
            MongoCollection<Document> persons = mongoClient.getDatabase("test").getCollection("persons");
            System.out.println("Dropping collection 'test.persons'");
            persons.drop();
            System.out.println("Insert 2 sample docs...");
            insertSampleDocs(persons);
            aggregationWithLookup(persons);
        }
    }

    private static void insertSampleDocs(MongoCollection<Document> persons) {
        List<Integer> maxFriends = Collections.singletonList(2);
        Document maxime = new Document("_id", 1).append("name", "Maxime").append("friends", maxFriends);
        Document prakhar = new Document("_id", 2).append("name", "Prakhar");
        persons.insertMany(asList(maxime, prakhar));
    }

    private static void aggregationWithLookup(MongoCollection<Document> persons) {
        Bson match = match(eq("name", "Maxime"));
        Bson lookup = lookup("persons", "friends", "_id", "friends");
        List<Document> results = persons.aggregate(asList(match, lookup)).into(new ArrayList<>());
        System.out.println("==> Print result of Lookup");
        results.forEach(printDocuments());
    }

    private static Consumer<Document> printDocuments() {
        return doc -> System.out.println(doc.toJson(JsonWriterSettings.builder().indent(true).build()));
    }
}

Result in my test.persons collection:

Atlas Free-shard-0 [primary] test> db.persons.find()
[
  { _id: 1, name: 'Maxime', friends: [ 2 ] },
  { _id: 2, name: 'Prakhar' }
]

Java program output:

Dropping collection 'test.persons'
Insert 2 sample docs...
==> Print result of Lookup
{
  "_id": 1,
  "name": "Maxime",
  "friends": [
    {
      "_id": 2,
      "name": "Prakhar"
    }
  ]
}

Everything seem to work as intended for me.

Cheers,
Maxime.

1 Like

Hi @MaBeuLux88 , thanks for such an explanatory response.

Maybe I should have framed my question in a better way. I am trying to use following method of Aggregation class in Java :

lookup(String from, List<? extends Bson> pipeline, String as);

I am unable to use the above mentioned method, as I am getting following error while using a pipeline:

com.mongodb.MongoCommandException: Command failed with error 8000 (AtlasError): ‘pipeline is not allowed in this atlas tier’ on server mflix-shard-00-02.7jsv0.mongodb.net:27017. The full response is { “ok” : 0, “errmsg” : “pipeline is not allowed in this atlas tier”, “code” : 8000, “codeName” : “AtlasError” }

I hope now I am able to explain the problem (in a better way ), that I am facing.

Regards,
Prakhar

Just like the field name from and the field name as is implied by the method, I suspect that the field pipeline is also implied and should not be specified.

Just pass Arrays.asList( new Document( $match… )) rather than Arrays.asList(new Document(pipeline,…).

2 Likes

Hi @steevej ,

Thanks for the help! It worked after I removed keyword pipeline.

Cheers,
Prakhar

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.