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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:53:39+00:00 2026-06-03T06:53:39+00:00

I’ve two collections posts and authors . A post document contains, beside the post

  • 0

I’ve two collections posts and authors. A post document contains, beside the post data, a DBref link ID to an author _id. My collection looks like this:

posts

  "_id" : ObjectId("4fa12443d3269e98070013b4"),
  "author" : {
    "$ref" : "authors",
    "$id" : ObjectId("4fa1242bd3269e9807000023")
  },
  "post" : " mi eleifend egestas. Sed pharetra, felis eget varius ultrices, mauris ipsum porta elit, a feugiat tellus lorem eu metus. In lorem.",
  "post_title" : "Volutpat. Nulla facilisis. Suspendisse commodo tincidunt nibh. Phasellus nulla. Integer",
  "date" : 1293803276,
  "rating" : 8,
  "comments" : [{
      "name" : "Doris Turner",
      "email" : "Hedda_Herman@.com",
      "upVotes" : 81,
      "downVotes" : 93,
      "comment_date" : 1111395830,
      "comment" : "consectetuer ipsum nunc id enim. Curabitur massa. Vestibulum accumsan neque et nunc. Quisque ornare tortor at"
    }, {
      "name" : "Brenda Tyler",
      "upVotes" : 1,
      "downVotes" : 73,
      "comment_date" : 940325674,
      "comment" : "cursus purus. Nullam scelerisque neque sed sem egestas blandit. Nam Nulla aliquet. Proin velit. Sed malesuada augue ut lacus. Nulla tincidunt, neque vitae semper egestas, urna justo faucibus lectus, a sollicitudin orci sem eget massa."}],
  "tags" : ["tag1", "tag3"]
}  

And my authors collection looks like this:

authors

{
  "_id" : ObjectId("4fa1242bd3269e9807000016"),
  "name" : "Kristina Chung ",
  "email" : "curran_ramos@google.co.id\r\n.dk",
  "age" : 60,
  "city" : "Copenhagen"
}

I’m trying to create a “relational” query to find:
Posts with rating greater than 6 and less than 9 and where the post author age is greater than 19 and less than 25.

I’m trying to aggregate this but can’t seem to get the right data.

The first query is in my posts collection and looks like this:

$query = array('rating' => array('$gte' => 6, '$lt' => 9), 'comments.upVotes' => array('$gt' => 2, '$lt' => 20));

I selects the author.$id field which is the reference to the authors collection.

I then put all the $id’s in a array as such:

while ($cursor->hasNext()): $document = $cursor->getNext();
    $authorID[] = $document['_id'];
endwhile;

and then I try to find the correct number with this query in the authors collection

$query2 = array('age' => array('$gte' => 19, '$lt' =>100), '_id' => array('$in' => $authorID));
$cursor = q('author')->find($query2);

I try to get the total number with this code:
$count = $cursor->count(); but no matter what query I try to run I always get the result 0.

What am I doing wrong and can is it possible at all to create this kind of query or do I have to make it on application level instead of database level?

And I’m well aware of embedded documents, but I want to have these two collections separated and not embed it.

Hope anyone can help me with this.

Sincere
– Mestika

  • 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-03T06:53:40+00:00Added an answer on June 3, 2026 at 6:53 am

    Trying to ‘create a “relational” query’ in MongoDB is going to be an exercise in frustration. Your schema stores some information (the post’s rating) in one collection and other information (the author’s age) in a different collection, but all MongoDB queries operate on single collections. Unless you denormalize your data (which you said you did not want to do), you will need a two-pass method to make this work.

    An approach that should work would be to build an array of author IDs and use it in a query of the posts collection using ‘$in’. Here’s what it might look like in JavaScript using the mongo shell:

    > var authorList = [];
    > var authorCursor = db.authors.find({age:{$gt:19,$lt:25}},{"_id":1});
    > while(authorCursor.hasNext()){authorList.push(authorCursor.next()["_id"])};
    > db.posts.find({"author.$id":{$in:authorList},rating:{$gt:6,$lt:9}});
    

    The first line creates an empty array. The second line creates a cursor that will select the _id fields of all authors in your target age range. The third line uses the cursor to populate an array of author _ids. The fourth line displays all posts that match your target criteria: author _id in the list we just build and rating in the range you specified.

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

Sidebar

Related Questions

Supposing I have two collections, posts and comments. post = { _id: 1, language:
I've got 2 entities in JPA: Entry and Comment. Entry contains two collections of
I want to pass two collections of objects. First is Post , second is
I have two models, Category and Post. Category.rb class Category include Mongoid::Document field :title,
I have two collections as below which hold IDs for Students. The ids are
I got two collections of objects. For example: List<Foo> firstFoos = new List<Foo>(); List<Foo>
I've got a settings object for my app that has two collections in it.
I have two generic collections in my code, EventableCollection and EventableSortedList - which are
according to the official docs there are two options to create parallel collections: 1)
1)Is there any difference between these two keywords for the elements of collections??( Copy

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.