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

The SetStage() in AggregationPipelineBuilder().addStage(SetStage()) isn't working with setting the field to the null value. #322

Open
SittiphanSittisak opened this issue Mar 13, 2023 · 3 comments
Labels

Comments

@SittiphanSittisak
Copy link

I can't set the value from string to null.
basic example code:

Future<Map<String, dynamic>?> exanple(
  {
  required ObjectId id,
  required String? oldValue,
  required String? newValue,
}) async {
  return await mongodb.dbCollection.findAndModify(
    query: where.id(id).eq('value', oldValue),
    update: (AggregationPipelineBuilder()..addStage(SetStage({'value': newValue}))).build(),
    returnNew: true,
  );
}

print(await example(id: ObjectId(), oldValue: 'oldValue', newValue: null)); //got the map with the `value` field is 'oldValue'. (should be null)

but this function will work successfully when I set any string for newValue: property.
What happened?

@SittiphanSittisak SittiphanSittisak changed the title The SetStage can't work with null value. The SetStage() in AggregationPipelineBuilder().addStage(SetStage()) isn't working with setting the field to the null value. Mar 13, 2023
@SittiphanSittisak
Copy link
Author

Now, I am using this to solve my job.

    final AggregationStage stage = newValue == null ? Unset(['value']) : SetStage({'value': newValue});
/*
from
    update: (AggregationPipelineBuilder()..addStage(SetStage({'value': newValue}))).build(),
to
    update: (AggregationPipelineBuilder()..addStage(stage)).build(),
*/

But I still wonder about this.

@giorgiofran
Copy link
Contributor

There is a problem in the code so that all the stages with a null value are excluded. I consider it a bug, but I have to do some tests to see if it is possible to simply remove that selection.
The reason why you are able to unset, but not to set, is that you are doing two different operations: unsetting, you are removing the field from the document, setting with a null value, you change the value of the field to null, but the field is still in the document.

@giorgiofran
Copy link
Contributor

Aside of the new mongo_db_driver package, I have also posted a new mongo_db_query one that should not have the problem you reported. If you could test it it would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants