"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"
}
I want to get all the books in the genre Classics, but excluding the genre Fantasy and History.
$genre = array("Classics");
$genreNot = array("Fantasy", "History");
$q = find(array("bookGenre" => array('$in' => $genre), "bookGenre" => array('$nin' => $genreNot)));
In this query, I get all my books, and the exception is not bringing the genre Fantasy and History does not work. How to make a request to the excluded genre work?
Hmm you have got a fundamental problem with your arrays here:
$q = find(array("bookGenre" => array('$in' => $genre),
"bookGenre" => array('$nin' => $genreNot)));
You have duplicate keys for the same object so actually the only clause in this query that goes through is "bookGenre" => array('$nin' => $genreNot)
. I believe instead you can combine the $in
and $nin
:
$q = find(array(
"bookGenre" => array('$in' => $genre, '$nin' => $genreNot)
));
Like so.