[SERVER-7095] Sample of three or more collections Created: 21/Sep/12  Updated: 15/Feb/13  Resolved: 21/Sep/12

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: Evgeny Kalibrov Assignee: sam.helman@10gen.com
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

PHP driver


Issue Links:
Duplicate
Participants:

 Description   

Hello!

There are collections

city:

{"_id", "name"}

company:

{"_id", "name", "cityID"}

comments:

{"_id", "text", "companyID"}

You must select the last 10 comments on companies of a certain city.

Now I select _id first of all companies in the city, and then the 10 comments received on _id
Here's the code:

$ db-> execute ('function () {

var result = {};

var company = [];
result.company = [];
db.company.find (

{"city": "msk"}

,

{"title": 1, "_id": 1}

). forEach (function (a) {
result.company [a._id] = Object.deepExtend (a, result [a._id]);
company.push (a._id);
});

var comments = [];
result.comments = [];
db.comments.find ({"company": {"$ in": company}}). sort (

{"createTime": -1}

). limit (10). forEach (function (a)

{ result.comments [a._id] = Object.deepExtend (a, result [a._id]); comments.push (a._id); }); return result; }

');

Esti's a better option to do what I need?
Thanks in advance for your advice!



 Comments   
Comment by Jeremy Mikola [ 21/Sep/12 ]

I resolved this because the SERVER project in JIRA is intended to be used for recording issues with the MongoDB server. Your question was a usage question, and a 10gen employee was already responding to your duplicate post of StackOverflow. Please follow up on the question there.

Comment by Evgeny Kalibrov [ 21/Sep/12 ]

This is the right way to do two queries, where the first we select _id array, and the second looking at the array using "$in"?

Comment by Jeremy Mikola [ 21/Sep/12 ]

This is already being answered on SO: http://stackoverflow.com/questions/12529663/mongodb-query-from-three-collections

Generated at Thu Feb 08 03:13:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.