我在检索与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”是一个有序且无单位的字符串元素数组: 排序($搜索); $ search = array_unique($ search);
$new->setProperty('search', $search);
我对Mongo很新,但我不明白为什么,如果我的文档的语法不正确,那么返回一个而不是一个。
谢谢。
答案 0 :(得分: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" ] }