bwgjoseph / mongoose-vs-ottoman

feature comparison between mongoose and ottoman
0 stars 1 forks source link

support for lean #43

Closed bwgjoseph closed 3 years ago

bwgjoseph commented 3 years ago

See https://mongoosejs.com/docs/tutorials/lean.html#using-lean

I'm not sure if ottoman is the same as mongoose that by default has alot more internal state to track, and hence uses more memory. If so, then could it also provide the lean option to return just the actual document object

AV25242 commented 3 years ago

Created Issue https://github.com/couchbaselabs/node-ottoman/issues/374

httpJunkie commented 3 years ago

This was released with alpha.17

bwgjoseph commented 3 years ago

Thank you! Will test this soon

bwgjoseph commented 3 years ago

As per request, moving the discussion of https://github.com/bwgjoseph/mongoose-vs-ottoman/issues/40#issuecomment-782603693 to here

bwgjoseph commented 3 years ago

Written a test for this but the outcome is quite unexpected, take a look at this test case

// in mongoose
const find = await Airplane.findById(created.id).lean().exec();
const find2 = await Airplane.findById(created.id).exec();
const checkFind = sizeof(find); // 926
const checkFind2 = sizeof(find2); // 64690

// in ottoman
const find = await Airplane.findById(created.id, {lean: true});
const find2 = await Airplane.findById(created.id);
const checkFind = sizeof(find); // 670
const checkFind2 = sizeof(find2); // 574

Somehow, lean document is bigger than the normal (non-lean) document size

AV25242 commented 3 years ago

Will let our team take a look at it.

AV25242 commented 3 years ago

A fix for this is in place and should be available with next release

AV25242 commented 3 years ago

Checkout the new alpha.21 release and let us know.

bwgjoseph commented 3 years ago

I've tested this, and it is better, but I'm not exactly sure how to verify this.

This is what happens now with alpha.21

const find = await Airplane.findById(created.id, {lean: true});
const find2 = await Airplane.findById(created.id);
const checkFind = sizeof(find); // 574
const checkFind2 = sizeof(find2); // 574

This has no difference between using lean or not. For mongoose, by default, it has much bigger size without lean, does this means that ottoman document is all the same whether lean or not? If not, then how do I test for this?

ariamF11 commented 3 years ago

This is a way

    const UserModel = model('User', schema);
    const document = await UserModel.findById(id, { lean: true });
    const document1 = await UserModel.findById(id);

    // with lean:true
    console.log(document instanceof UserModel); // false
    console.log(document instanceof Model); // false
    console.log(document instanceof Document); // false
    console.log(document instanceof Object); // true
    console.log(document.constructor.name === 'Object'); // true

    // with lean:false
    console.log(document1 instanceof UserModel); // true
    console.log(document1 instanceof Model); // true
    console.log(document1 instanceof Document); // true
    console.log(document1 instanceof Object); // true
    console.log(document1.constructor.name === '_Model'); // true
bwgjoseph commented 3 years ago

Hi @gsi-ariam,

Thanks for that!

With my test stating that both lean and non-lean document has the exact same size, does that mean that ottoman does not have internal fields/methods/etc that is similar to mongoose (in the Document class?)? As mentioned in the first post, and example (few post down), it shows that mongoose document size reduced by quite a factor with lean

gsi-chao commented 3 years ago

@bwgjoseph in this example you can see why the result size of lean is the same(using object-sizeof library) that the Model Object. Note: We use the spread operator to convert the class object to object in Ottoman

bwgjoseph commented 3 years ago

Thanks!

Thank you for being patient. I would like to understand more about the difference so that I know what's going behind the scene.

In mongoose, we lose a number of feature (as describe the their docs), such as

// from the docs
The downside of enabling lean is that lean docs don't have:

Change tracking
Casting and validation
Getters and setters
Virtuals
save()

Does something like this happen to ottoman when using lean as well? Such as missing certain feature?

gsi-chao commented 3 years ago

Yes, in ottoman when using lean we return an object using a spread operator, all methods, hooks,..., etc are removed. In the example I sent you, I wanted to prove to you roughly, that for the object-sizeof library, a class like the one we have defined, has the same size as an object that contains the same properties.

If you run the test in debug mode you will see that an object after applying the "lean" method, loses all the methods and only keeps the properties of the class.

bwgjoseph commented 3 years ago

Great! Thank you so much for the explanation.

I hope these can find their way into the documentation in the near future. It would help a lot.

Thank you!

gsi-chao commented 3 years ago

@bwgjoseph, You can be sure that we will take it into account, thank you very much for all your feedbacks

gsi-chao commented 3 years ago

@bwgjoseph Do you have any suggestions to add to the documentation for this feature?

Can we close this issue?

bwgjoseph commented 3 years ago

I think some sort of the example you gave in https://github.com/bwgjoseph/mongoose-vs-ottoman/issues/43#issuecomment-813046405 plus an example of when the size would be different, and also explaining what would be the trade-off of using lean vs non-lean.

Can we close this issue?

We sure can! Do you still have anything for me?

gsi-chao commented 3 years ago

Thanks, nothing more to add on my part.