1

What is the mongoDB query for that mysql query how use join in mongoDB there is the problem i am facing to write in mysql?

SELECT t.uid, count(j.job_id) AS x 
FROM verify AS t 
LEFT JOIN jobs AS j 
ON t.uid = j.job_id
kovpack
  • 4,905
  • 8
  • 38
  • 55
  • You might achieve something close with aggregation pipeline and its `$lookup` operator. But, generally speaking, joins and complex reporting are not mongodb's forte. – Sergio Tulentsev Jul 27 '17 at 09:34
  • question is incorrect if `uid` = `job_id` and it `one-to-one` relation so why to do join and get count? why not simply have a count? – num8er Jul 27 '17 at 09:58
  • 1
    in no-sql world there is no join's, cuz most of time data kept in same document (json, bson object). and even if they are kept separately - depending on db ODMs there are `populate` (or alike) methods – num8er Jul 27 '17 at 10:01
  • Which basically goes to say, please do not ask "how to convert my SQL". Because what you should instead be doing is "restructuring" the data you actually store. If you are just going to copy the exact same table structures, then moving from an RDBMS to MongoDB is pointless. – Neil Lunn Jul 27 '17 at 10:14
  • Show [this link](https://stackoverflow.com/questions/12563762/join-two-collections-in-mongodb/62988605#62988605) and see a scenario in SQL code and mongoDB code – Levik Nazarians Jul 20 '20 at 12:46

1 Answers1

1

This would be like below by using aggregation framework with $project & $lookup pipeline :

db.collection1.aggregation[{
    $project: {

        'uid': 1,
        "x": {
            "$size": "job_id"
        }
    },

    $lookup: {
        from: 'collection2',
        localField: 'uid',
        foreignField: 'job_id',
        as: 'j'
    }
}]
Talha Awan
  • 4,573
  • 4
  • 25
  • 40
Abhay
  • 6,410
  • 4
  • 26
  • 34