Last active
September 19, 2018 19:31
-
-
Save ep4sh/a382a7d32f1a4339e3074af7963c2a1e to your computer and use it in GitHub Desktop.
MongoDB SQL join analog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use app; | |
| db.createCollection('rent') | |
| db.createCollection('guest') | |
| db.guest.insert({ "_id" : ObjectId("5ba24958103b0ac162148f6f"), "name" : "Иванов Иван Иваныч", "floor" : 3, "org" : 2, "cardNo" : 34443, "dateIn" : 20180919, "dateOut" : 20190920 }) | |
| db.rent.insert({ "_id" : ObjectId("5ba24a53103b0ac162148f71"), "company" : "Sun", "floor" : 3, "org" : 2 }) | |
| db.rent.insert({ "_id" : ObjectId("5ba24a44103b0ac162148f70"), "company" : "Microsoft", "floor" : 2, "org" : 1 }) | |
| db.guest.aggregate([ | |
| { | |
| $lookup: { | |
| from: "rent", | |
| localField: "org", | |
| foreignField: "org", | |
| as: "company" | |
| } | |
| }, | |
| { | |
| $project: { | |
| name: 1, | |
| floor: 1, | |
| cardNo: 1, | |
| org: "$company.company", | |
| dateIn: 1, | |
| dateout: 1 | |
| } | |
| }, | |
| { | |
| $unwind: "$org" | |
| } | |
| ]).pretty(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment