mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/docs/drivers/php/laravel-mongodb/
MIT License
7.02k stars 1.43k forks source link

Resulting document after update is larger than 16777216 #2049

Closed dev-erem closed 4 years ago

dev-erem commented 4 years ago

On saving many-to-many relation, my lumen app returns following error Resulting document after update is larger than 16777216

Smolevich commented 4 years ago

Looks like as mongo database error

dev-erem commented 4 years ago

@Smolevich , yes this is mongo database error. There is a many-to-many relationship between categories & posts, and attach operation adds references to both documents. now one document i.e. category has reached max limit of 16MB, so this error occurred. Any work around?

Smolevich commented 4 years ago

You can describe example of data and write actual mongo driver version

dev-erem commented 4 years ago

@Smolevich This is what i get after running command on terminal php --ri mongodb | grep version MongoDB extension version => 1.7.2 libbson bundled version => 1.16.1 libmongoc bundled version => 1.16.1 libmongocrypt bundled version => 1.0.3

dev-erem commented 4 years ago

Example Category document as under:

{
    _id: ObjectId("5ede5420e6fc8c04841cc212"),
    name: "sports",
    post_ids:[
        "5e92a31749cad1b17b430822",
        "5e92a31749cad1b17b430823",
        "5e92a31749cad1b17b430824",
        ....
        ....
        ....
        "5e92a31749cad1b17b430822",
        "5ec130981c51930cbd188fe2"
    ]
}

Example Post document as under:

{
    "_id": "ObjectId("5e92a31f49cad1b17b4a1dee"),
    "title": "A sports related news",
    "summary": "Lorem epsum ....",  
    "category_ids": [
        "5e92a31b49cad1b17b45b35f", 
            "5e92a31b49cad1b17b45b4e0"
    ]
}
dev-erem commented 4 years ago

Now sports category has around 460000 post_ids, and creating a new relation b/w any new post with sports category generate this bug.

divine commented 4 years ago

This has nothing to do with the library.

The document size can't exceed 16MB. Change your logic, it's mongodb overall (you're not forced to do some hard sql relations)

I don't see a reason why you're referring post_ids in Category document? Doesn't make sense at all.

dev-erem commented 4 years ago

Hi @divine ,

I am not referring post_ids in Category document explicitly. I am using following code in my controller to relate a post with a category object: $category->posts()->attach($post);

divine commented 4 years ago

@amjad-eremnews well, I don't remember exactly but probably that's the problem.

In normal laravel mysql/postresql relations attach does use special table to store those references.

In this library mongodb it does work a little bit different, it actually saves in one document.

What I could suggest to solve this problem? Probably referencing attached categories only on posts documents might solve the problem.

Thanks.

dev-erem commented 4 years ago

@divine

I don't think referencing attached categories only on posts documents will the solve the problem. can you check line number 203 on https://github.com/jenssegers/laravel-mongodb/blob/master/src/Jenssegers/Mongodb/Relations/BelongsToMany.php. ?

divine commented 4 years ago

@amjad-eremnews you're right, then the solution would be this:

// Add new category to post
$post->push('category_ids',  $category_id, true);
// Remove category from post
$post->pull('category_ids', $category_id);

This way your main category document wouldn't filled up. However, you'll need probably adjust your relations but this is probably only workaround that is possible currently.

Let me know, if you'll have any more questions.

Thanks.

dev-erem commented 4 years ago

yes i have already applied this for now.

MattJarman commented 4 years ago

Hey,

I'm attempting this fix, but I can't seem to get the relationship right, and as a result, the array of ids isn't being populated. What do I need to do to the relationship to populate the array?

Thanks!