Skip to content

Duplicate items in array when $push #91

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

Closed
brunocascio opened this issue Dec 15, 2013 · 4 comments
Closed

Duplicate items in array when $push #91

brunocascio opened this issue Dec 15, 2013 · 4 comments

Comments

@brunocascio
Copy link

Hi jens!

$data = array('user_id' => Auth::user()->_id,   'username' => Auth::user()->username    );
$post->push('stars', $data);
$post->push('stars', $data);

Any option to prevent duplicate objects in array?

In mongo is $addToSet..

http://docs.mongodb.org/manual/reference/operator/update/addToSet/

Thanks!

@jenssegers
Copy link
Contributor

Not at this moment. But you could use a raw expression: https://github.com/jenssegers/Laravel-MongoDB#raw-expressions

@brunocascio
Copy link
Author

I have sent a pull request with that functionality.
I checked with same operation, and work fine.

@jenssegers
Copy link
Contributor

The problem I have with these methods is that it deviates a lot from the original Eloquent functionality. MongoDB specific functionality is always available through raw expressions. I'll leave this open for opinions from other users for now.

I would rather add a third $unique parameter to the push method than introduce a new non-eloquent method.

@brunocascio
Copy link
Author

Yes! Good idea.

Anyway..

Raw update:

$data = array('foo' => 'bar', 'foo2' => 'bar2');
$id = '52ae0bf538d34983168b4571';

Post::raw(function($collection) use ($data, $id)
{
  return $collection->update(array('_id' => new MongoID($id) ), array('$addToSet' => array( 'stars' =>  $data  ) ) );
});

mnphpexpert added a commit to mnphpexpert/laravel-mongodb that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants