include relation in mongo query

db.user.aggregate([
    { $project: { "userId": 1, "userKeyEncrypted": 1, "uuid":1, "roles": 1, "passwordHash": 1, "disabled": 1, companyRefs: { $let: { vars: { refParts: { $objectToArray: "$$ROOT.company" }}, in: "$$refParts.v" } } } },
    { $match: { "companyRefs": { $exists: true } } },
    { $project: { "userId": 1, "userKeyEncrypted": 1, "uuid":1, "roles": 1, "passwordHash": 1, "disabled": 1, "companyRef": { $arrayElemAt: [ "$companyRefs", 1 ] } } },
    { $lookup: { from: "company", localField: "companyRef", foreignField: "_id", as: "company_data" } },
    { $project: { "userId": 1, "userKeyEncrypted": 1, "uuid":1, "roles": 1, "passwordHash": 1, "disabled": 1,  "companyUuid": { $arrayElemAt: [ "$company_data.uuid", 0 ] } } }
])

Are there any code examples left?
New code examples in category Go
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
mongodb query relation mongo db relation mongodb command find with relationships mongo link collections how to create connection many to many between a data collection anda a file colection mongodb how to create connection between collection mongodb mongodb reference model mongodb document relationships linking mongodb objects link collections in mongob how to maintain a relation among collections in pymongo relation in mongodb mongo db how to link a document to a user mongodb relation between collections mongodb keep references right after update mongodb keep references right mongodb reference relationship mongodb reference document example two references in same collection mongodb mongodb cross reference mongodb collection double relation mongodb linking documents example mongoDB exmaple relation how to make relation to mongodb document refrerence key mongodb how to get data from another table by referencing another table in mongo db mongodb relationships map mongodb relationships between collections c# mongodb collection relationship relationship mongo db mongodb realtions mongodb how to add a new relation mongodb database associations how to create relation between two collections in mongodb make relation between two collections in mongo mongodb relations example deleting a user properly with linking id and relations mongodb mongodb collection relation query mongodb document collection relationships linking documents in mongodb mongodb linking collections mongodb relatiosn mongodb use relation or one document relationships in mongodb one for many reference model mongodb doing referencing model with mongodb mongdb many to one relations using references How can I relationate a user in Mongodb relationships query in mongodb mongo relationship mongo relationships mongodb links between documents relationship in mongodb link one model to another in mongodb relation mongodb driver relation mongo driver refereced relationship mongodb mongodb best practice relationship just id or entire dopcument how to align your model to mongodb collection mongodb collections relations relationships id in mongodb relation id in mongodb how to make relations in mongodb mongodb creating relationships mongodb no relation how to implement relationship in mongodb How to create relationship Between two collections in MongoDB from relations to mongodb best practices save in mongodb linking mongodb connect documents from other collection mongodb connect documents mongodb connect document to other document best way to store linked files mongodb relations mongodb link document to another relationships mongodb best relationship on mongodb creating relationships between collections with ids with MongoDB mongodb query relationships mongodb find by relation id mongodb associations mongodb relations mongodb relations update mongodb relation relations in mongodb mongodb reference document relation document mongodb relationship mongodb how to connect collections in mongodb relating collections mongodb mongodb relationships mongodb relationship example how to create relationship in mongodb how to create a relationships in mongodb how to create a relation in mongodb example relationship between mongo mongodb belongs-to relationship tip mongodb relationship mongodb document reference example
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source