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 6882831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:18:15+00:00 2026-05-27T05:18:15+00:00

I’m wondering the best way to query mongo db for many objects, where each

  • 0

I’m wondering the best way to query mongo db for many objects, where each one has an array of _id’s that are attached to it. I want to grab the referenced objects as well. The objects’ schemas looks like this:

var headlineSchema = new Schema({
    title : String, 
    source : String,
    edits : Array       // list of edits, stored as an array of _id's
    ...
});

…and the one that’s referenced, if needed:

var messageSchema = new Schema({
    message : String,
    user : String,
    headlineID : ObjectId   // also contains a ref. back to headline it's incl. in
    ...
});

One part of the problem (well, depending if I want to keep going this route) is that pushing the message id’s is not working (edits remains an empty array [] afterwards) :

db.headline.update({_id : headlineid}, {$push: {edits : messageid} }, true);

When I do my query, I need to grab about 30 ‘headlines’ at a time, and each one could contain references to up to 20 or 30 ‘messages’. My question is, what is the best way to fetch all of these things? I know mongo isn’t a relational db, so what I’m intending is to first grab the headlines that I need, and then loop through all 30 of them to grab any attached messages.

db.headline.find({'date': {$gte: start, $lt: end} }, function (err, docs) {
    if(err) { console.log(err.message); }
    if(docs) { 
        docs.forEach(function(doc){
            doc.edits.forEach(function(ed){
                db.messages.find({_id:ed}, function (err, msg) {
                    // save stuff
                });
            });
        });
    }
});

This just seems wrong, but I’m unsure how else to proceed. Should I even bother with keeping an array of attached messages? I’m not married to the way I’ve set up my schema, either. If there is a better way to track relationships between them, or a better query to achieve this, please let me know.
Thanks

  • 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-05-27T05:18:15+00:00Added an answer on May 27, 2026 at 5:18 am

    Does each message belong to only one headline? If so, you can store the headline id as part of each message. Then for each headline, do:

    db.messages.find({headline_id: current-headline-id-here})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I need a function that will clean a strings' special characters. I do NOT

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.