Php 如何使用Jenssers/laravel mongodb库在mongodb集合的子文档中实现搜索查询
我正在一个项目中使用jenssegers/laravel mongodb库。我有一个集合,其中创建了包含其他详细信息的子文档。现在我必须实现一个搜索查询来搜索集合的子文档。子文档是我想在其索引6中搜索的product_数据。我想在里面搜索国家名称。我该怎么做呢?集合的结构如下:Php 如何使用Jenssers/laravel mongodb库在mongodb集合的子文档中实现搜索查询,php,mongodb,laravel-5,jenssegers-mongodb,Php,Mongodb,Laravel 5,Jenssegers Mongodb,我正在一个项目中使用jenssegers/laravel mongodb库。我有一个集合,其中创建了包含其他详细信息的子文档。现在我必须实现一个搜索查询来搜索集合的子文档。子文档是我想在其索引6中搜索的product_数据。我想在里面搜索国家名称。我该怎么做呢?集合的结构如下: { "_id": ObjectId("564d32f191c1cb1f087d7c71"), "userid": "55c487a1083f11aa1b8b4567", "shopid": "anoth
{
"_id": ObjectId("564d32f191c1cb1f087d7c71"),
"userid": "55c487a1083f11aa1b8b4567",
"shopid": "anotherstore",
"postedby": null,
"purpose": "sell",
"cat_ids": [
"TDC00-001-001",
],
"postdate": "2015-11-19",
"desc": "T-SHIRT",
"thumb": "http://test.local/uploads/product_img/prod-8yKsMHC2-1447895652.jpg",
"product_data"▼: {
"1": "2015-11-19",
"2": "anotherstore",
"3": "T-SHIRT",
"4": "1245",
"6": "Styling features include twin needle stitching for neatness and strength on the collar, sleeve and h",
"5": "US",
"6": "US"
}
}
我尝试了一些查询搜索,但没有成功。这些是:
$country=Array(
[0] => Australia
)
$country = Products::whereIn('product_data[6]', $country)->get();
or
$country = Products::where('books', 'elemMatch', array([6] => $country))->get();
如果有人知道,请提供帮助。因为在mongo中对子文档的查询如下所示:
db.products.find({'product_data.6':'US'});
您只需要在代码中重新创建该结构:
$country = 'US';
$queryFilter = ['product_data.6'=>$country];
Products::where($queryFilter)->first(); //or get whatever.
如果你想搜索一系列国家,你必须使用where方法
$countries = ['US','SPAIN'];
$results = Products::whereIn('product_data.6',$countries)->get();
是否有一种方法可以搜索$country是否为数组。意思是这样的$country=>Array([0]=>Abkhazia[1]=>Afghanistan)如果您查看下面的API,肯定会看到正确的格式设置
$countries=['US','SPAIN']$结果=产品::其中('product_data.6',$countries)->get()代码>