Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb aggregate arguments to $lookup must be strings

        db.absences.insert([
           { "_id" : 1, "student" : "Ann Aardvark", sickdays: [ new Date ("2018-05-01"),new Date ("2018-08-23") ] },
           { "_id" : 2, "student" : "Zoe Zebra", sickdays: [ new Date ("2018-02-01"),new Date ("2018-05-23") ] },
        ])

    db.holidays.insert([
       { "_id" : 1, year: 2018, name: "New Years", date: new Date("2018-01-01") },
       { "_id" : 2, year: 2018, name: "Pi Day", date: new Date("2018-03-14") },
       { "_id" : 3, year: 2018, name: "Ice Cream Day", date: new Date("2018-07-15") },
       { "_id" : 4, year: 2017, name: "New Years", date: new Date("2017-01-01") },
       { "_id" : 5, year: 2017, name: "Ice Cream Day", date: new Date("2017-07-16") }
    ])

db.absences.aggregate([
   {
      $lookup:
         {
           from: "holidays",
           pipeline: [
              { $match: { year: 2018 } },
              { $project: { _id: 0, date: { name: "$name", date: "$date" } } },
              { $replaceRoot: { newRoot: "$date" } }
           ],
           as: "holidays"
         }
    }
])

I'm trying to use pipeline in lookup for aggregation query. Having this as same as from the Mongodb documentation, it still gives an error

Unable to execute the selected commands

Mongo Server error (MongoCommandException): Command failed with error 4570: 'arguments to $lookup must be strings, pipeline: [ { $match: { year: 2018.0 } }, { $project: { _id: 0.0, date: { name: "$name", date: "$date" } } }, { $replaceRoot: { newRoot: "$date" } } ] is type array' on server localhost:27017. 

The full response is:
{ 
    "ok" : 0.0, 
    "errmsg" : "arguments to $lookup must be strings, pipeline: [ { $match: { year: 2018.0 } }, { $project: { _id: 0.0, date: { name: \"$name\", date: \"$date\" } } }, { $replaceRoot: { newRoot: \"$date\" } } ] is type array", 
    "code" : NumberInt(4570), 
    "codeName" : "Location4570"
}

I'm using mongodb v3.4.

like image 323
Nakarmi Avatar asked Dec 06 '17 08:12

Nakarmi


People also ask

What is $lookup in MongoDB?

The $lookup operator is an aggregation operator or an aggregation stage, which is used to join a document from one collection to a document of another collection of the same database based on some queries. Both the collections should belong to the same databases.

Can we join 2 collections in MongoDB?

For performing MongoDB Join two collections, you must use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents. For example, if a user requires all grades from all students, then the below query can be written: Students.

What is aggregate MongoDB?

What is Aggregation in MongoDB? Aggregation is a way of processing a large number of documents in a collection by means of passing them through different stages. The stages make up what is known as a pipeline. The stages in a pipeline can filter, sort, group, reshape and modify documents that pass through the pipeline.


1 Answers

Because you are trying to use the $lookup features (syntax) from MongoDB v3.6 on MongoDB v3.4

The MongoDB v3.4 $lookup syntax:

{
   $lookup:
     {
       from: <collection to join>,
       localField: <field from the input documents>,
       foreignField: <field from the documents of the "from" collection>,
       as: <output array field>
     }
}

The MongoDB v3.6 $lookup syntax:

{
   $lookup:
     {
       from: <collection to join>,
       let: { <var_1>: <expression>, …, <var_n>: <expression> },
       pipeline: [ <pipeline to execute on the collection to join> ],
       as: <output array field>
     }
}

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

like image 146
Neodan Avatar answered Sep 18 '22 21:09

Neodan