我正在尝试使用Laravel/Jenssegers MongoDB为我的应用设置一些简单的收藏
我有两个型号:
class Item {
public function favorites()
{
return $this->hasMany('App\Models\User', 'favorites');
}
}
class User {
public function favorites()
{
return $this->hasMany('App\Models\Item', 'favorites');
}
}
Run Code Online (Sandbox Code Playgroud)
因此,项目可以包含许多用户,并且用户可以拥有许多项目或"收藏夹".
当我尝试运行一个简单的查询,如:
Item::with('favorites')->get();
Run Code Online (Sandbox Code Playgroud)
收藏夹的关系是一个空数组.
该favorites表很简单,只有三列:
_id, item_id, user_id
Run Code Online (Sandbox Code Playgroud)
我也尝试过embedsMany没有运气:
return $this->embedsMany('App\Models\User', 'favorites');
Run Code Online (Sandbox Code Playgroud)
我也尝试过各种参数组合.
return $this->embedsMany('App\Models\User', 'favorites', 'building_id', 'user_id');
return $this->embedsMany('App\Models\User', 'favorites', 'user_id', 'building_id');
return $this->hasMany('App\Models\User', 'favorites', 'building_id', 'user_id');
return $this->hasMany('App\Models\User', 'favorites', 'user_id', 'building_id');
Run Code Online (Sandbox Code Playgroud)
使用时产生奇怪的结果:
return $this->hasMany('App\Models\User', 'favorites', 'building_id', 'user_id');
Run Code Online (Sandbox Code Playgroud)
只有在这种情况下它才会返回所有用户,即使收藏夹只有一条记录.
尝试使用调试getQueryLog只是给我一个空数组.
print_r(\DB::connection('mongodb')->getQueryLog());
Run Code Online (Sandbox Code Playgroud)
我已经完成了这项工作mysql并且从未遇到过问题.所以不确定问题的来源.
我是 Laravel 的新手。我想使用 Laravel 10 连接到 MongoDB,需要安装 jenssegers/mongodb。当我在终端中运行命令时composer require jenssegers/mongodb 3.8.0 --ignore-platform-reqs,出现如下错误:
PHP Warning: PHP Startup: Unable to load dynamic library 'php_mongodb.dll' (tried: D:\ProgramFiles\xampp\php\ext\php_mongodb.dll (The specified module could not be found), D:\ProgramFiles\xampp\php\ext\php_php_mongodb.dll.dll (The specified module could not be found)) in Unknown on line 0
Warning: PHP Startup: Unable to load dynamic library 'php_mongodb.dll' (tried: D:\ProgramFiles\xampp\php\ext\php_mongodb.dll (The specified module could not be found), D:\ProgramFiles\xampp\php\ext\php_php_mongodb.dll.dll (The specified module could not be found)) in Unknown on line 0
Info from https://repo.packagist.org: …Run Code Online (Sandbox Code Playgroud) 我有一个称为更改的 MongoDB集合,其中包含以下数据
{
"date" : ISODate("2014-06-09T00:00:00.000Z"),
"field" : "ip",
"from" : "157.11.209.123",
"to" : "107.21.109.254"
}
{
"date" : ISODate("2014-05-15T00:00:00.000Z"),
"field" : "ip",
"from" : "107.21.109.254",
"to" : "157.11.209.123"
}
{
"date" : ISODate("2014-06-09T00:00:00.000Z"),
"field" : "registration",
"from" : "Old service",
"to" : "Some new service"
}
Run Code Online (Sandbox Code Playgroud)
然后我想制作典型的SQL查询来计算出现次数并将其分组field.所以,我在MongoDB中创建了查询
db.changes.group({
"key": {
"field": true
},
"initial": {
"count": 0,
},
"reduce": function(obj, prev) {
prev.count++;
},
});
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我如何将其转换为与Laravel 4一起使用?我正在使用jenssegers/laravel-mongodb与mongo服务器进行通信.
另外,我在查询中有更多条件,我已删除它以使我的问题看起来更清楚,所以我正在寻找将该查询完全转换为laravel的解决方案,而不是其他可能的解决方案:).
php mongodb laravel aggregation-framework jenssegers-mongodb
我正在使用jenssegers / laravel-mongodb与MongoDB数据库一起使用Laravel API 。
我正在尝试使用正则表达式进行过滤以获取一些特定数据。在该插件的教程中,我发现了这一点:
User::where('name', 'regex', new MongoRegex("/.*doe/i"))->get();
Run Code Online (Sandbox Code Playgroud)
所以我的代码看起来像这样:
School::where('name', 'regex', new MongoRegex("/haags/i"))->get();
Run Code Online (Sandbox Code Playgroud)
但是结果是空的。当我输出查询时,它看起来像这样:
db.schools.find({"name":{"$regex":{"regex":"haags","flags":"i"}}})
Run Code Online (Sandbox Code Playgroud)
当我在控制台中使用该查询时,它说:
error: {
"$err" : "Can't canonicalize query: BadValue $regex has to be a string",
"code" : 17287
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
School::where('name', 'regexp', "/haags/i")->get();
Run Code Online (Sandbox Code Playgroud)
但这给了我这个查询:
db.schools.find({"name":{"$regex":"\/haag\/i"}})
Run Code Online (Sandbox Code Playgroud)
这显然转义了正斜线并使正则表达式无效。除此之外,正则表达式不应该在引号之间,或者应该是这样(在MongoDB手册中找到):
db.products.find( { description: { $regex: /^S/, $options: 'm' } } )
Run Code Online (Sandbox Code Playgroud)
因此,转换为MongoDB查询时出现问题,或者我做错了事。有人可以告诉我这是什么吗?
我正在使用 laravel 5.1 并使用 jessenger mongodb 包。我正在使用原始查询来获取数据,但我很困惑如何使用日期,因为目前它返回空结果。
$resultSet = DB::connection('mongodb')->collection('wc_mycollection')->raw(function ($collection){
return $collection->aggregate([
[
'$match'=>[
'created_at'=>[
'$gte' => Previous day midnight,
'$lt' => Current Time
]
]
],
[
'$group' => [
'_id' => '$some_id',
]
]
]);
});
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
我正在使用 mongodb 使用 laravel 5.1。对于 mongodb,我使用 jenssegers mongo 配置。
现在我必须创建 mongodb 副本集,并且我只想从主服务器的从属和写操作进行读取操作。
在 laravel 的文档中,我读到我们可以在读写中进行分离,如下所示:
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
Run Code Online (Sandbox Code Playgroud)
这里是链接了解如何在laravel设置配置。
在 jenssegers 文档中,这里是复制配置:
'mongodb' => [
'driver' => 'mongodb',
'host' => ['server1', 'server2'],
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => …Run Code Online (Sandbox Code Playgroud) 我现在主要在两个模型上工作,Formand 和Notification,并且建立了多对多关系并适用于大多数 Eloquent 命令,除了whereHasand has。两者都只返回一个空数组,[]。
似乎开发人员过去在让它工作时遇到了麻烦,但似乎在这里已经解决了。
这是我到目前为止所拥有的以及我尝试过的示例:
表单.php
class Form extends Eloquent {
protected $connection = 'mongodb';
public function notifications(){
return $this->belongsToMany('App\Api\Forms\Notification', null, 'form_ids', 'notification_ids');
}
}
Run Code Online (Sandbox Code Playgroud)
通知.php
class Notification extends Eloquent {
protected $connection = 'mongodb';
public function forms()
{
return $this->belongsToMany('App\Api\Forms\Form', null, 'notification_ids', 'form_ids');
}
}
Run Code Online (Sandbox Code Playgroud)
通知控制器.php
<?php
namespace App\Http\Controllers;
use App\Api\Forms\Notification;
use App\Api\Forms\Form;
class NotificationController extends Controller
{
public function …Run Code Online (Sandbox Code Playgroud) 我有两个模型,一个是扩展 Jenssegers\Model 的 mongo 模型,另一个是扩展 Illuminate\Model 的 sql 模型。此 SQL 模型没有定义连接名称,因为我们有多个数据库连接,每个数据库中都有相同的表。
Mongo模型注释.php
<?php
namespace App\Models\Mongo;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Comment extends Eloquent
{
/** @var string Mongo Connection Name */
protected $connection = 'mongodb';
/** @var string Mongo Collection Name */
protected $collection = 'comments';
/** @var bool Enable/Disable Timestamp */
public $timestamps = true;
/** @var date Date format */
protected $dateFormat = 'Y-m-d H:i:s';
public function userProfile()
{
return $this->belongsTo('\\App\\Models\\Sql\\UserDefaultProfile', 'created_by', 'user_code');
}
}
Run Code Online (Sandbox Code Playgroud)
Sql 模型 UserProfile.php …
文档说“belongsToMany 关系不会使用数据透视表”,而是将 id 推送到 related_ids 属性。这使得belongsToMany 方法的第二个参数无用。如果您想为您的关系定义自定义键,请设置它为空”
那么如何在 related_ids 属性上设置其他字段?
我有产品和购物车,
这是关于 related_ids 的 cart 子文档
产品ID:['600ed36d5e2720122c633172','600ed3e792a0e87c86741eb2','600ed3f1bc85983ebc06fc53','600edcdfccfc896fe81fc262','600edd71bc665d23a 03558c2'],
但我需要为每种产品设置数量
has-and-belongs-to-many mongodb laravel jenssegers-mongodb moloquent
因此,我设法使用 WSL2 在 Windows 中全新安装了 Laravel。我的项目要求之一是有两个数据库同时运行mysql和mongodb。
我在我的 docker-compose.yml 中添加了 MongoDB 的图像,因为dock-hub 告诉我,但是当我尝试添加 jenssegers/laravel-mongodb 包时,sail 告诉我我没有正确的要求。
如何正确连接两个容器,以便我可以毫无问题地安装软件包?
日志:
~/example-app$ sail composer require jenssegers/mongodb:"^3.8"
./composer.json has been updated
Running composer update jenssegers/mongodb
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.
Problem 1
- jenssegers/mongodb[v3.8.0, ..., v3.8.1] require mongodb/mongodb ^1.6 -> satisfiable by mongodb/mongodb[1.6.0, ..., 1.9.x-dev (alias of dev-master)].
- mongodb/mongodb 1.9.x-dev is an alias of mongodb/mongodb dev-master and thus requires …Run Code Online (Sandbox Code Playgroud)