0
"bookName": "I've been discovered",
    "bookGenre": {
         "0": "Classics",
         "1": "Fantasy",
         "2": "Romance"
    }

"bookName": "Doctor Who",
"bookGenre": {
     "0": "Classics",
     "1": "Biography"
}

"bookName": "I don't want to tread carefully",
"bookGenre": {
     "0": "Classics",
     "1": "Fantasy",
     "2": "History"
}

我想获得所有经典类型的书籍,但不包括幻想和历史类型。

$genre = array("Classics");
$genreNot = array("Fantasy", "History");

$q = find(array("bookGenre" => array('$in' => $genre), "bookGenre" => array('$nin' => $genreNot)));

在这个查询中,我得到了我所有的书,但没有带来类型幻想和历史不起作用的例外。如何向排除的类型作品提出请求?

4

1 回答 1

0

嗯,你的数组有一个基本问题:

$q = find(array("bookGenre" => array('$in' => $genre), 
"bookGenre" => array('$nin' => $genreNot)));

您对同一个对象有重复的键,所以实际上这个查询中唯一通过的子句是"bookGenre" => array('$nin' => $genreNot). 我相信您可以将 and 结合$in起来$nin

$q = find(array(
             "bookGenre" => array('$in' => $genre, '$nin' => $genreNot)
));

像这样。

于 2013-01-09T14:07:19.903 回答