On Tue, Mar 30, 2010 at 12:48 PM, Dan Yamins <dya...@gmail.com> wrote:
> Two related questions:
>
> 1) Is there any efficient way to find those records whose keys intersect
> with a list of key names? E.g. if I have an arbitrary list of 50 key names,
> and I want to find all records that have at least one of those keys, is
> there anything better then taking the union of the list of results for
> {$exists:key} for each key alone?
There may be a better way to model this so that you don't have to use
$exist, etc. Can you share more about your use case? I'm thinking
you'd have
a key pointing to an array of objects, which would be much more
queryable since you could then use $in.
> 2) Is there any efficient way to find those records which have more a given
> number of keys? Perhaps a $where query? But what javascript express would
> I use? The following two attempts did not work:
>
> {'$where':'function(){return this.Keys.length == 10};'}
> {'$where':'function(){return this.length == 10};'}
JavaScript objects don't have a keys method. You have to iterate over
the properties in each object and count them using for(x in obj).
Alternatively,
if you store your objects in an array as suggested above, then you can
cache the size or vote on this issue
(http://jira.mongodb.org/browse/SERVER-478) and
hope it becomes available soon.
>
> Thanks,
> Dan
>
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.
>
There may be a better way to model this so that you don't have to use
$exist, etc. Can you share more about your use case? I'm thinking
you'd have
a key pointing to an array of objects, which would be much more
queryable since you could then use $in.
Have you considered storing one data point per document?
{'series_id': x, year: '1951.1', value: y}
If you index it properly, you should be able to perform range queries
and $in queries as needed.
There is an $or operator in the works: http://jira.mongodb.org/browse/SERVER-205
Have you considered storing one data point per document?
{'series_id': x, year: '1951.1', value: y}
After racking my brain on a similar issue I got a javascript intersect
to work by basically using the $where condition in find (or group by)
and then writing a simple function. I also found that if using in a
group by you can do it as a finalize.
Anyway, here is my intersection routine. Might help you:
// sample res object
{
"_id.tags.channel" : "Yahoo",
"_id.tags.browser" : "chrome",
"_id.tags.landing_page" : "C",
"csum" : 64.9,
},
// find intersections
var eligibleArray = new Array();
if (res) {
for(var i = 0; i < res.length; i++) {
var intersections = 0;
list = { channel: 'Yahoo', browser: 'chrome' };
for(prop in list) if( res[i].hasOwnProperty("_id.tags."+prop) &&
res[i]["_id.tags."+prop] == list[prop]) intersections++;
res[i].intersections = intersections;
}
}
// output
{
"_id.tags.channel" : "Yahoo",
"_id.tags.browser" : "chrome",
"_id.tags.landing_page" : "C",
"csum" : 64.9,
"intersections" : 2,
},
On Apr 2, 8:35 pm, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> we haven't started $or. not that much work, just a matter of getting
> it into the priority queue.
>
>
>
> On Fri, Apr 2, 2010 at 7:48 PM, Dan Yamins <dyam...@gmail.com> wrote:
>
> > On Thu, Apr 1, 2010 at 4:23 PM, Kyle Banker <k...@10gen.com> wrote:
>
> >> There is an $or operator in the works:
> >>http://jira.mongodb.org/browse/SERVER-205
>
> > Excellent. Is it still being actively developed? I would love to
> > understand what some of the problematic programming issues are with respect
> > to it -- as is hinted at here:
> > http://groups.google.com/group/mongodb-user/browse_thread/thread/1323....
Dan -
After racking my brain on a similar issue I got a javascript intersect
to work by basically using the $where condition in find (or group by)
and then writing a simple function. I also found that if using in a
group by you can do it as a finalize.