Laravel-mongodb: How to use allowDiskUse: true?

Created on 11 Jul 2015  路  15Comments  路  Source: jenssegers/laravel-mongodb

Help me,

How can I use 'allowDiskUse: true' in my model query?

Most helpful comment

@Puggers Can you please change AllowDiskUse to allowDiskUse?

All 15 comments

Dupe of #491. Not supported, but would be easy to add as an option in \Jenssegers\Mongodb\Query\Builder::getFresh.

Having the same issue! @hackel do you mean it'd be an easy fix on jenssegers end or would anyone be able to add code into their app for it to work? If the latter, could you explain how?

@chloealee You would have to fork it and modify the getFresh method. This is where MongoCollection::aggregate() is called, and allowDiskUse needs to be passed as an option there. If you don't want to bother with forking, you can just create a raw query with the raw() method.

@hackel great, thanks!

@hackel still having some trouble - tried to insert it into my raw query (below) but getting this error:

[MongoResultException] localhost:27017: exception: Unrecognized pipeline stage name: 'allowDiskUse'

        $duplicates = User::raw(function($collection) {
            return $collection->aggregate(
                [
                    '$group' => [
                        '_id' => ['email' => '$email'
                        ],
                        'uniqueIds' => [
                            '$addToSet' => '$_id'
                        ],
                        'count' => [
                            '$sum' => 1
                        ]
                    ]
                ],
                [
                    '$match' => [
                        'count' => [
                            '$gt' => 1
                        ]
                    ]
                ],
                [
                    'allowDiskUse' => true,
                    // 'cursor' => {}
                ]
            );
        });

do you know what I'm doing wrong?

I see what I did wrong now! If anyone is having this issue too and needs an example, got this to work:

        $duplicates = User::raw(function($collection) {
            return $collection->aggregate(
                [
                    [
                        '$group' => [
                            '_id' => ['email' => '$email'
                            ],
                            'uniqueIds' => [
                                '$addToSet' => '$_id'
                            ],
                            'count' => [
                                '$sum' => 1
                            ]
                        ]
                    ],
                    [
                        '$match' => [
                            'count' => [
                                '$gt' => 1
                            ]
                        ]
                    ]
                ],
                [
                    'allowDiskUse' => true,
                ]
            );
        });

The issue was that it thought 'allowDiskUse' was a pipeline stage name so I had to separate it out even further from the query.

How would you go about passing in values from outside $duplicates, such as an array to compare.

I think you'd want to do that after you find all of the $duplicates instead of passing through - just compare with the results in the array that gets returned.

Yeah, but the index filtering results before they are returned would be a lot quicker

I'm not quite sure - sorry @ScottSpittle !

@chloealee No worries, Thanks anyway.

@sparrowbear @chloealee
This issue is now fixed on Moloquent Branch

Thanks to @hackel :)

Not sure if I am in the right place for this, I am attempting to add the AllowDiskUse option invoking the options function, it doesn't seem to be handling it. Is AllowDiskUse usable outside of a raw query?

$tweets = Tweet::whereBetween('created_at.sec', dateToInt($request->get('startDate'), $request->get('endDate')))
            ->where(getTweets($request))
            ->orderBy('created_at.sec', 'desc')
            ->options(['AllowDiskUse' => true])
            ->paginate(10);

@Puggers Can you please change AllowDiskUse to allowDiskUse?

@amjad-eremnews The power of cases... yup that did it. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

geofflancaster picture geofflancaster  路  3Comments

kschethan picture kschethan  路  3Comments

BlakeGardner picture BlakeGardner  路  3Comments

tomartailored picture tomartailored  路  3Comments

Vasiliy-Bondarenko picture Vasiliy-Bondarenko  路  3Comments