开发者

Referencing Relative Objects in MongoDB

开发者 https://www.devze.com 2023-01-30 21:45 出处:网络
Is it possible, given a sorted query, to reference a relative object during mapReduce, or even with find($where)?

Is it possible, given a sorted query, to reference a relative object during mapReduce, or even with find($where)?

e.g. Given

[
 { 'name' : 'bill',
   'age' : 20 }
,
 { 'name' : 'bill',
   'age' : 25 }
]

find({$where: 'this.age > previo开发者_Go百科usItem.age'})

will just return:

 { 'name' : 'bill',
   'age' : 25 }


You don't have access to the cursor in a map-reduce context, so there's no way you can 'memorize' what the previous document was. In map-reduce, each document is treated in isolation.

The reason for this is that map-reduce can run in parallel on multiple shards, resulting in better performance. In the future, multithreaded map-reduce jobs may also be supported. In these scenarios there isn't a true 'previous item', as you're treating multiple parts of the collection simultaneously. It can either be the previous item in the current thread, the previous item on the current shard or the previous item across all shards.

Regarding your example, if you have sorted your query on age, then all the previously processed documents have an age less than or equal to the current item's age. All unprocessed items will have an age greater than or equal to the current item's age. You can use find() to retrieve these items, but I wouldn't recommend it, as it may impair performance. Also, you can only access the current item, not the previous item as you're trying to do in your example.

I think you're misunderstanding the concept of map-reduce. What is it you're trying to achieve?

0

精彩评论

暂无评论...
验证码 换一张
取 消