optional value returned by mongocxx::collection member functions

72 views
Skip to first unread message

li ning

unread,
Apr 7, 2016, 6:41:34 AM4/7/16
to mongodb-user
The insert, delete return an optional, but doc has not talk about in what situation the return value is null. I mean

auto r = connection.delete_one({x})

what is the different between:
if(r) and if(r->deleted_count() == 1)

If I need to check the delete result, which statement should I use?

In what situation the return value r will be NULL? I mean optional operator bool() const  return false

li ning

unread,
Apr 7, 2016, 9:44:22 PM4/7/16
to mongodb-user
I check source code find some method will return null like find_one, but other(delete_one) will not return null. This is inconsistent.

For methods return a result_type like insert delete, there is no need wrap the result_type by optional, because the result_type already can take the semantic of "no value", warp it by a optional means want to express "no value of no value", that is meaningless.
This design confuse me (so I ask this question), in fact only methods which return an "concrete type"(like find) needs wrap by optional to represent "no value"


在 2016年4月7日星期四 UTC+8下午6:41:34,li ning写道:
Message has been deleted

Andrew Morrow

unread,
Apr 15, 2016, 1:21:37 PM4/15/16
to mongod...@googlegroups.com

Hi -

The intention of the methods that return an optional<T> where T is in the mongocxx::result namespace is that they may or may not return a T value. The case where they do not return a T value is (intended) to represent things like unacknowledged operations (e.g. a w:0 write).

However, at this time, the C++11 driver does not ever return a disengaged optional in these circumstances, because of an issue in the C driver.

I've filed https://jira.mongodb.org/browse/CXX-894 so that we can resolve this when the underlying issue in the C driver is resolved.

Thanks,
Andrew

On Thu, Apr 7, 2016 at 9:45 PM, li ning <li.ni...@gmail.com> wrote:
I check source code find some method will return null like find_one, but other(delete_one) will not return null. This is inconsistent.

For methods return a result_type like insert delete, there is no need wrap the result_type by optional, because the result_type already can take the semantic of "no value", warp it by a optional means want to express "no value of no value", that is meaningless.
This design confuse me (so I ask this question), in fact only methods which return an "concrete type"(like find) needs wrap by optional to represent "no value"


在 2016年4月7日星期四 UTC+8下午6:41:34,li ning写道:
The insert, delete return an optional, but doc has not talk about in what situation the return value is null. I mean

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/f01fe764-b046-4925-bd17-5fbba9005065%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages