gpt4 book ai didi

php - 如何使用 jenssegers/laravel-mongodb 库在 mongodb 集合的子文档中实现搜索查询

转载 作者:可可西里 更新时间:2023-11-01 09:35:30 25 4
gpt4 key购买 nike

我在项目中使用 jenssegers/laravel-mongodb 库。我有一个集合,其中创建了一个包含其他详细信息的子文档。现在我必须实现一个搜索查询来搜索集合的子文档。子文档是 product_data,我想在其中搜索它的索引 6。我想在其中搜索国家名称。我该怎么做。集合的结构如下:

{
"_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.

如果你想在一组国家中搜索你必须使用 whereIn 方法

$countries = ['US','SPAIN']; 
$results = Products::whereIn('product_data.6',$countries)->get();

关于php - 如何使用 jenssegers/laravel-mongodb 库在 mongodb 集合的子文档中实现搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34157451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com