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 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度