Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update push and pull docs #2685

Merged
merged 13 commits into from
Dec 4, 2023
147 changes: 122 additions & 25 deletions docs/query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,64 +448,161 @@ DB::collection('items')

**Push**

Add items to an array.
Add one or multiple values to the items array.
hans-thomas marked this conversation as resolved.
Show resolved Hide resolved

```php
// Push the value to the matched documents
DB::collection('users')
->where('name', 'John')
->push('items', 'boots');
->where('name', 'John')
// Push a single value to the items array
->push('items', 'boots');
// Result:
// items: ['boots']

DB::collection('users')
->where('name', 'John')
// Push multiple values to the items array
->push('items', ['hat', 'jeans']);
// Result:
// items: ['boots', 'hat', 'jeans']

// Or

// Push the value to an exact document
$user->push('items', 'boots');
$user->push('items', ['hat', 'jeans']);
hans-thomas marked this conversation as resolved.
Show resolved Hide resolved
```

To add an array as a value to the messages array. value arrays must be nested into a list array, otherwise they are
interpreted as a list of scalar values.
hans-thomas marked this conversation as resolved.
Show resolved Hide resolved

```php
DB::collection('users')
->where('name', 'John')
->push('messages', [
'from' => 'Jane Doe',
'message' => 'Hi John',
]);
->where('name', 'John')
// Push an array as a value to the items array
hans-thomas marked this conversation as resolved.
Show resolved Hide resolved
->push('messages', [
[
'from' => 'Jane Doe',
'message' => 'Hi John',
],
]);
// Result:
// messages: [
// { 'from' => 'Jane Doe', 'message' => 'Hi John' }
hans-thomas marked this conversation as resolved.
Show resolved Hide resolved
// ]

// Or

$user->push('messages', [
'from' => 'Jane Doe',
'message' => 'Hi John',
[
'from' => 'Jane Doe',
'message' => 'Hi John',
],
]);
```

If you **DON'T** want duplicate items, set the third parameter to `true`:
If you **DON'T** want duplicate values, set the third parameter to `true`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked the code, but this suggests that the boolean causes $addToSet to be used internally instead of $push. Is that correct?

Having a boolean argument control that rubs me the wrong way (I'll add it to the growing list), but I assume there's an explanation for this behavior. Is the push() method here actually derived from something else in Eloquent, or was it originally created in this library specifically for the MQL operators?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked the code, but this suggests that the boolean causes $addToSet to be used internally instead of $push. Is that correct?

Yes. It's the logged queries of pushing normally and pushing unique values.

array:2 [
  0 => array:3 [
    "query" => "users.updateMany({"_id":"65672f58551357588a07c993"},{"$push":{"items":{"$each":["boots"]}}},{"multiple":true})"
    "bindings" => []
    "time" => 1.82
  ]
  1 => array:3 [
    "query" => "users.updateMany({"_id":"65672f58551357588a07c993"},{"$addToSet":{"items":{"$each":["hat","jeans"]}}},{"multiple":true})"
    "bindings" => []
    "time" => 0.6
  ]
]

Having a boolean argument control that rubs me the wrong way (I'll add it to the growing list), but I assume there's an explanation for this behavior. Is the push() method here actually derived from something else in Eloquent, or was it originally created in this library specifically for the MQL operators?

The push method is originally from Eloquent and it's used for saving a model instance and its relationships to the database. this library overrides the push method and accepts three args. If no arg is passed, the original push method will be called.

Copy link
Member

@jmikola jmikola Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Eloquent's push method documented anywhere beyond Recursively Saving Models & Relationships? That doesn't appear to take any arguments and the method seems entirely different in purpose (this doesn't have to do with relationships).

@GromNaN: A boolean flag to determine whether $push or $addToSet is used seems like a poor design as it makes the code less self-documenting. I think this is compounded by the fact that the method on the model class uses func_get_args() and doesn't define a named parameter that users could specify if they so desired.

It may be worth making a ticket to revisit this down the line. Perhaps introduce a new addToSet() method and then deprecate the original variation. This might also be a moot point when/if the query builder is integrated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 agree that the double behavior of this method is a bad design.

If deprecate the custom behavior of Model::push with parameters, we have to do deprecate the pull method too.

Let's improve the documentation for now.


```php
DB::collection('users')
->where('name', 'John')
->push('items', 'boots', true);
->where('name', 'John')
->push('items', 'boots');
// Result:
// items: ['boots']

DB::collection('users')
->where('name', 'John')
->push('items', ['hat', 'boots', 'jeans'], true);
// Result:
// items: ['boots', 'hat', 'jeans']

// Or

$user->push('messages', [
[
'from' => 'Jane Doe',
'message' => 'Hi John',
],
]);
// Result:
// messages: [
// { 'from' => 'Jane Doe', 'message' => 'Hi John' }
// ]

$user->push('items', 'boots', true);
$user->push('messages', [
[
'from' => 'Jess Doe',
'message' => 'Hi',
],
[
'from' => 'Jane Doe',
'message' => 'Hi John',
],
], true);
hans-thomas marked this conversation as resolved.
Show resolved Hide resolved
// Result:
// messages: [
// { 'from' => 'Jane Doe', 'message' => 'Hi John' }
// { 'from' => 'Jess Doe', 'message' => 'Hi' }
// ]
```

**Pull**

Remove an item from an array.
Remove one or multiple values form items array.
hans-thomas marked this conversation as resolved.
Show resolved Hide resolved

```php
// items: ['boots', 'hat', 'jeans']

DB::collection('users')
->where('name', 'John')
->pull('items', 'boots');
->where('name', 'John')
->pull('items', 'boots'); // Pull a single value
// Result:
// items: ['hat', 'jeans']

// Or pull multiple values

$user->pull('items', 'boots');
$user->pull('items', ['boots', 'jeans']);
// Result:
// items: ['hat']
```

Also, you can pull an array value from the messages array.
hans-thomas marked this conversation as resolved.
Show resolved Hide resolved

```php
// Latest state:
// messages: [
// { 'from' => 'Jane Doe', 'message' => 'Hi John' }
// { 'from' => 'Jess Doe', 'message' => 'Hi' }
// ]

DB::collection('users')
->where('name', 'John')
->pull('messages', [
'from' => 'Jane Doe',
'message' => 'Hi John',
]);
->where('name', 'John')
// It will search for the exact array and pulls it
hans-thomas marked this conversation as resolved.
Show resolved Hide resolved
->pull('messages', [
[
'from' => 'Jane Doe',
'message' => 'Hi John',
]
hans-thomas marked this conversation as resolved.
Show resolved Hide resolved
]);
// Result:
// messages: [
// { 'from' => 'Jess Doe', 'message' => 'Hi' }
// ]

// Or pull multiple array
hans-thomas marked this conversation as resolved.
Show resolved Hide resolved

$user->pull('messages', [
'from' => 'Jane Doe',
'message' => 'Hi John',
[
'from' => 'Jane Doe',
'message' => 'Hi John',
],
[
'from' => 'Jess Doe',
'message' => 'Hi',
]
]);
// Result:
// messages: [ ]
```

**Unset**
Expand Down