Mongodb php查询,在数组中搜索? [英] Mongodb php query, search in array?

查看:67
本文介绍了Mongodb php查询,在数组中搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在具有以下结构的文档answers"数组中搜索关键字(例如Henrik")的方法

I am looking for a way to search for a keyword (ex. "Henrik") in the "answers" array of documents with the following structure

Array
(
[_id] => MongoId Object
    (
        [$id] => 4eeedd9545c717620a000007
    )

[formId] => 6
[respId] => 4eeedd95c93228
[dateCreated] => 2011-10-14 07:45
[answers] => Array
    (
        [field1] => Henrik
        [field6] => myemail@domain.com
        [field7] => my city address
    )

)

我在这个项目中使用 PHP,当然像这样查询:

I am working in PHP on this project, and quering like this works of course:

$answers = $collection->find( array('formId' => 6, 'answers.field1'=> 'Henrik' ) );

我想要做的是在没有答案数组的特定键的情况下进行搜索,就像这样

What I want to do is search without a specific key of the answers array, like this

$answers = $collection->find( array('formId' => 6, 'answers'=> 'Henrik' ) );

是否可以进行此类查询?如果这是转帖,我很抱歉.我无法在此处或 Google 上找到任何关于此的示例.

Is it possible to do this type of query? I am sorry if this is a repost. I was not able to find any examples about this here or on Google.

推荐答案

我认为这样做是不可能的.

I don't think it's possible to do that.

answers"是一个文档数组(如您所说),需要知道它包含在文档中的哪个位置以查找您指定的值.

"answers", being an array of documents (as you say), needs to know where in the documents it contains to look for the value you specify.

我认为您想要做的是在答案"中文档的任何字段上查找Henrik".在这种情况下,我认为最好的办法是使用 $or:

I presume that what you want to do is to look for 'Henrik' on any of the fields of the documents in "answers". In this case I think your best bet is to use $or:

$answers = $collection->find( array('formId' => 6, '$or' => array('answers.field1'=> 'Henrik', 'answers.field6' => 'Henrik', 'answers.field7' => 'Henrik')));

这篇关于Mongodb php查询,在数组中搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆