bwgjoseph / mongoose-vs-ottoman

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

array operator #96

Open bwgjoseph opened 3 years ago

bwgjoseph commented 3 years ago

Hi,

Something that I've missed out again but is there an existing feature to perform array operation such as $push, $pull, $each, $pop, etc as described in here

I don't think I've seen it mentioned anywhere in the documentation, and it has quite a common use case to manipulate an array within a document.

Thanks

AV25242 commented 3 years ago

Although I understand the need, but there are work arounds like you can select the array update it using native JSON operators and then use the update method to update the document.

bwgjoseph commented 3 years ago

Does op-op mean it will not be considered even post GA? Is it because that the underlying Couchbase database does not support it natively to perform array operations on the document?

I know that we can grab the document and perform the action ourselves, but it's not safe as compared to offloading to the database to do the work. Consider the following scenario where

Request 1 grab the document, make the changes manually to the document, and update it to the database and at the same time Request 2 also grab the document before the changes are made and make changes to the document array.

// req1
.findById('12345');
// req2
.findById('12345');
// req1
.updateById('12345', { comments: [<whatever values before>, 'abcde'] });
// req2
.updateById('12345', { comments: [<whatever values before>, 'defgc'] });
// end state would be
comments: [<whatever values before>, 'defgc']

I understand this might not be the best example since there are also other scenarios where this would occur and developer have to manage themselves via cas or __v (in mongoose), but if there the ability to update the array via $push, $pull, etc, it could be written as such

// req1
.updateById('12345', { comments: { $push: ['abcde'] } });
// req2
.updateById('12345', { comments: { $push: ['defgc'] } });
// end state would be
comments: [<whatever values before>, 'abcde', 'defgc']

As such, this is safe for the developer, and know that there won't be conflict. Without it, there is a lot more work that needs to be done on the developer side to ensure the safety of document conflict.

Ease of array/object manipulation should be something that is provided by the driver that also ensures the safety of the document (as much as possible)

bwgjoseph commented 3 years ago

I think these are the ones I'm looking for (if I understand those correctly)

https://docs.couchbase.com/nodejs-sdk/current/howtos/subdocument-operations.html#array-append-and-prepend https://docs.couchbase.com/nodejs-sdk/current/howtos/subdocument-operations.html#arrays-as-unique-sets https://docs.couchbase.com/nodejs-sdk/current/howtos/subdocument-operations.html#array-insertion

AV25242 commented 3 years ago

I understand, however we dont support sub-document operations yet. We will have to get there first and then apparently think of this. Thats why its listed as no-op atleast for now.

bwgjoseph commented 3 years ago

Alright, it is quite an important feature to have. So long that this feature is planned to be added, I think it's fine to be lacking for now. Thanks