如何在php中检索MongoDB多个数组值?

时间:2016-12-28 16:49:01

标签: php mongodb mongodb-query

我想检索字段颜色的所有值。 php如何处理这个值?

def menu(self):
    self.savefig=QtGui.QAction('&Save', self) 
    self.savefig.setShortcut('Ctrl+S')
    self.savefig.triggered.connect(self.save_figure)

    undo=QtGui.QAction('&Undo', self)
    undo.setShortcut('Ctrl+Z')
    undo.triggered.connect(self.back)

    redo=QtGui.QAction('&Redo', self)
    redo.setShortcut('Ctrl+Y')
    redo.triggered.connect(self.forward)

    figoptions=QtGui.QAction('&Figure Options', self)
    figoptions.setShortcut('Ctrl+F')
    figoptions.setStatusTip('Edit curves lines and axes parameters')
    figoptions.triggered.connect(self.edit_parameters)



def back(self):
    self.toolbar.back()
def forward(self):
    self.toolbar.forward()
def save_figure(self):
    self.toolbar.save_figure()
def edit_parameters(self):
    self.toolbar.edit_parameters()

PHP代码:

{
    _id : 1,

    color : ["red","blue","yellow"]
},
{
    _id : 2,

    color : ["green","black","grey"]
}

因此它不会返回任何值。如何获取数组值?

1 个答案:

答案 0 :(得分:1)

使用distinct获取数组的不同值:

$cursor = $collection->distinct('color');

一个完整的例子:

<?php

    $m = new MongoClient();
    $db = $m->selectDB("testDB");
    $cursor = $db->users->distinct('color');
    var_dump($cursor);

?>