jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
287 stars 65 forks source link

Lookup function not working in RStudio #183

Open Digiapt-Vdrop opened 5 years ago

Digiapt-Vdrop commented 5 years ago

I am trying to merge two collections say 'Student' and 'Finance' using a common column 'Student ID'. Both the collections have enough data and common data for a join to work.

studentCollection = mongo(db="mydb", col="students")
financeCollection = mongo(db="mydb", col="finance")

studentCollection$aggregate('[{"$lookup":{"from": "financeCollection","localField": "Student ID","foreignField": "Student ID","as": "fees_collection"}},{"$project": {"Student Name":1,"fees_collection.Fees Amount":1}},{"$limit":1}]')

Output:

   _id                        Student Name                    fees_collection
1 5d779798147f2bd39f2f6018   XXXXX                                   NULL

The fees_collection is always null.

Note: The same function is working in Mac terminal using Mongo version 4.2.0 and I am able to join.

Command used:

db.projects.aggregate([{$lookup:{from: "students",localField: "Student ID",foreignField: "Student ID",as: "fees_collection"}},{$project:{"Student Name":1,"fees_collection.Fees Amount":1}},{$limit:1}])

Output:

{ "_id" : ObjectId("5d779798147f2bd39f2f6018"), "Student Name" : "XXXXX", "fees_collection" : [ { "Fees Amount" : 5 }, { "Fees Amount" : 24 } ] }

Installed Mongolite Version: 2.1.0 Installed RStudio Version: 1.2.1335