dtdfl62844 2012-01-18 02:49
浏览 60
已采纳

使用基于关键AND值的PHP和MongoDB排序结果?

For my case i am using MongoDB to store a whole bunch of products from 50 countries, entry in DB looks something like this:

{
    _id: xxxxxxxxxxxxxxx,
    country: 'au',
    title: 'Equipment X',
    price: 1000,
},
{
    _id: xxxxxxxxxxxxxxy,
    country: 'us',
    title: 'Equipment Y',
    price: 1000,
},
...
...

I would like to know if it is at all possible to query all products and sort by country from au FIRST, then the rest of the products after that. The simple MongoDB sort seems to only sort by key. If it is possible, does it involve map/reduce?

Edit: A more detailed example

Documents in collection:

{ country:'cn', title:'Equipment A', price:'100'},
{ country:'au', title:'Equipment B', price:'100'},
{ country:'za', title:'Equipment C', price:'100'},
{ country:'us', title:'Equipment D', price:'100'},
{ country:'nz', title:'Equipment E', price:'100'},
{ country:'it', title:'Equipment F', price:'100'},
{ country:'nz', title:'Equipment G', price:'100'},
{ country:'au', title:'Equipment H', price:'100'},

I am looking for a sort that will return products from nz to be at the top and then follow by all other products, like this:

{ country:'nz', title:'Equipment E', price:'100'},
{ country:'nz', title:'Equipment G', price:'100'},
{ country:'au', title:'Equipment B', price:'100'},
{ country:'au', title:'Equipment H', price:'100'},
{ country:'cn', title:'Equipment A', price:'100'},
{ country:'it', title:'Equipment F', price:'100'},
{ country:'us', title:'Equipment D', price:'100'},
{ country:'za', title:'Equipment C', price:'100'},
  • 写回答

2条回答 默认 最新

  • duanchui1251 2012-01-18 03:07
    关注

    By sorting on a key, it means sorting on the values in a key. This is what you want, I think. (Left off the beginning part to connect to the db and pick a collection).

    $cursor = $collection->find();
    $cursor->sort(array('country' => 1))
    

    update So is this what you want to do?

    $cursor = $collection->find(array('country' => 'nz'));
    // process your nz products...
    
    $cursor = $collection->find(array('country' => array( '$neq' => 'nz' )));
    // process the rest of your products...
    

    update2 Well, I think you may need to resort to doing something like this, if you really need this functionality.

    // create a new field on the records that don't have it yet
    $collection->update(
       array('country' => 'nz', array('first' => array('$exists' => false))), 
       array('$set' => array('first'=>1)), 
       array('multiple' => true));
    // ensure index on that field
    $collection->ensureIndex(array('first' => 1));
    // find everything
    $cursor = $collection->find();
    // sort on that field
    $cursor->sort(array('first':-1));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退