0

我无法检索与 MongoDB 上的查询匹配的所有文档。我正在使用 PHP。

这是一个快速测试:

## Document 1   

{
   "_id": ObjectId("4ea80a1eb73e26ef1500cc9e"),
   "search": {
   "0": "clothing",
   "1": "golden",
    ....etc

## Document 2
{
   "_id": ObjectId("4ea81e78b73e26ef15339c65"),
   "search": {
     "0": "and",
     "1": "belt",
     "2": "brown",
     "3": "golden",
    ...etc

## query
$search = array('golden');
$products = Products::all( array('search'=>array('$in'=> $search ))) // only document 1 is returned.
//this is a quick test. in the end I'd like to have:
$search = array('golden', 'belt'); //document 2 is returned

文档是从 PHP 数组创建的,“search”是字符串元素的排序和唯一数组: sort($search); $search = array_unique($search);

$new->setProperty('search', $search);

我对 Mongo 很陌生,但我不明白为什么,如果我的文档的语法不正确,那么会返回一个而不是返回一个。

谢谢你。

4

1 回答 1

0

我不知道 PHP 中有什么,但这有效:

> db.items.find({})
{ "_id" : ObjectId("4ea98d0fa287fedc0281feee"), "name" : "first", "keywords" : [ "one", "two", "three", "four" ] }
{ "_id" : ObjectId("4ea98d1aa287fedc0281feef"), "name" : "first", "keywords" : [ "two", "three", "four", "five" ] }
{ "_id" : ObjectId("4ea98d23a287fedc0281fef0"), "name" : "first", "keywords" : [ "four", "five", "six" ] }
{ "_id" : ObjectId("4ea99243a287fedc0281fef1"), "name" : "first", "keywords" : [ "five", "six" ] }
{ "_id" : ObjectId("4ea99247a287fedc0281fef2"), "name" : "first", "keywords" : [ "five", "six", "seven" ] }
{ "_id" : ObjectId("4ea99273a287fedc0281fef3"), "name" : "first", "keywords" : [ "one", "two", "seven" ] }
{ "_id" : ObjectId("4ea9927aa287fedc0281fef4"), "name" : "first", "keywords" : [ "one", "two", "seven" ] }
> db.items.find({$or:[{keywords:"four"},{keywords:"five"}]})
{ "_id" : ObjectId("4ea98d0fa287fedc0281feee"), "name" : "first", "keywords" : [ "one", "two", "three", "four" ] }
{ "_id" : ObjectId("4ea98d1aa287fedc0281feef"), "name" : "first", "keywords" : [ "two", "three", "four", "five" ] }
{ "_id" : ObjectId("4ea98d23a287fedc0281fef0"), "name" : "first", "keywords" : [ "four", "five", "six" ] }
{ "_id" : ObjectId("4ea99243a287fedc0281fef1"), "name" : "first", "keywords" : [ "five", "six" ] }
{ "_id" : ObjectId("4ea99247a287fedc0281fef2"), "name" : "first", "keywords" : [ "five", "six", "seven" ] }
> db.items.find({keywords:{$in:["four","five"]}})
{ "_id" : ObjectId("4ea98d0fa287fedc0281feee"), "name" : "first", "keywords" : [ "one", "two", "three", "four" ] }
{ "_id" : ObjectId("4ea98d1aa287fedc0281feef"), "name" : "first", "keywords" : [ "two", "three", "four", "five" ] }
{ "_id" : ObjectId("4ea98d23a287fedc0281fef0"), "name" : "first", "keywords" : [ "four", "five", "six" ] }
{ "_id" : ObjectId("4ea99243a287fedc0281fef1"), "name" : "first", "keywords" : [ "five", "six" ] }
{ "_id" : ObjectId("4ea99247a287fedc0281fef2"), "name" : "first", "keywords" : [ "five", "six", "seven" ] }
于 2011-10-27T17:22:41.527 回答