0

I am trying aggregate query in mongo database.

I could find the command for aggregate in command line as well in robomongo.

Please find query below

db.portfolio.aggregate([
    { $match: {id: "c-4fbfd8ed-8b4a-4cf6-b3e0-2f5f3200d6d4"}},
    { $project: {
        medias: {$filter: {
            input: '$medias',
            as: 'category',
            cond: {$eq: ['$$category.categoryId', "j_664e2bba-fa0e-4d31-aa26-732b92c1b872"]}
        }}
    }}
])

But when I tried to convert into BSONDocument and run it, it throws an error during runtime. The equivalent BSONDocument that I have added in my code is

 val commandDoc = BSONDocument(
      "aggregate" -> "portfolio", // we aggregate on collection `portfolio`
      "pipeline" -> BSONArray(
        BSONDocument("$match" -> BSONDocument("id" -> portfolioId)),
        BSONDocument(
          "$project" -> BSONDocument(
            "medias" -> BSONDocument(
                "$filter" -> BSONDocument(
                    "input" -> "$medias",
                    "as" -> "category",
                    "cond" -> BSONDocument(
                        "$eq" -> Json.arr("$$category.categoryId", categoryId)
                    )
                )
            )
          )
        )
      )
    )

the error which gives when I am debugging the code during runtime is

The 'cursor' option is required, except for aggregate with the explain argument

What should I required to resolve this issue. I am using mongo version 3.6.2, is it can be issue with version because when I tried with mongo 3.2.7 it was working perfectly well.

  • [`BsonArray`](http://mongodb.github.io/mongo-scala-driver/2.0/scaladoc/org/mongodb/scala/bson/BsonArray$.html) AFIAK. You either do "everything" as as JSON and convert, or use the Bson types. Techically it's just the Java type anyway [`BsonArray`](http://api.mongodb.com/java/current/index.html?org/bson/BsonArray.html) or really any list of string. – Neil Lunn May 21 '18 at 07:07
  • checked by changing to BSON Array but it still throws me the same error The 'cursor' option is required, except for aggregate with the explain argument))), Success((code,BSONInteger(9))), Success((codeName,BSONString(FailedToParse))) – Manoj S Ramaswamy May 21 '18 at 07:21
  • Sorry did not look at that part of the question. Don't use `command` use the `aggregate()` method on the collection. – Neil Lunn May 21 '18 at 07:22
  • You should first have a look at the documentation http://reactivemongo.org/releases/0.1x/documentation/advanced-topics/aggregation.html – cchantep May 21 '18 at 11:20

0 Answers0