Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9002683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:36:13+00:00 2026-06-16T00:36:13+00:00

I have a document that has an id of another document from a different

  • 0

I have a document that has an id of another document from a different collection embedded in it.

My desired result is to return (I’m using python and pymongo) the all the fields of the first collection, and all of the friends from the document that was embedded.

I understand mongo doesn’t do joins and I understand I’ll need to make two queries. I also don’t want to duplicate my data.

My question is how to piece the two queries together in python/pymongo so I have one results with all the fields from both documents in it.

Here is what my data looks like:

db.employees

{_id: ObjectId("4d85c7039ab0fd70a117d733"), name: 'Joe Smith', title: 'junior',
manager: ObjectId("4d85c7039ab0fd70a117d730") }

db.managers

{_id: ObjectId("ObjectId("4d85c7039ab0fd70a117d730"), name: 'Jane Doe', title: 'senior manager'}

desired result

x = {_id: ObjectId("4d85c7039ab0fd70a117d733"), name: 'Joe Smith', title: 'junior',
    manager: 'Jane Doe' }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:36:14+00:00Added an answer on June 16, 2026 at 12:36 am

    Your basically doing something that Mongo does not support out of the box and would actually be more painful than using the two records separately.

    Basically (in pseudo/JS code since I am not a Python programmer):

    var emp = db.employees.find({ name: 'Joe Smith' });
    var mang = db.managers.find({ _id: emp._id });
    

    And there you have it you have the two records separately. You cannot chain as @slownage shows and get a merged result, or any result infact since MongoDB, from one qauery, will actually return a dictionary not the value of the field even when you specify only one field to return.

    So this is really the only solution, to get the the two separately and then work on them.

    Edit

    You could use a DBRef here but it is pretty much the same as doing it this way, only difference is that it is a helper to put it into your own model instead of doing it youself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity Course that has a key to another entity (Document) inside.
I have an object called Document that has a one-to-many relationship with DocumentPersonCc. I
I have a Delphi application that has a document browser as the main form.
I have XML tag that has the content which is HTML document. <xml-tag> <!--
I have an aspx document that returns a list of incoming flights from a
I have a <div> element that has a click event attached to it using
There is a simple entity that contains collection of another Document entity. class Client{
I have a new Joomla (2.5) site that has been developed on a different
I have a html document that has this structure: https://i.stack.imgur.com/zrOq1.png <- sorry for link,
I have a php page that also has another php page inside it, this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.