mongodb query returns results at commandline, no results in C++What are the differences between a pointer variable and a reference variable in C++?How can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListWhat is the “-->” operator in C++?How to query MongoDB with “like”?Query for documents where array size is greater than 1How do I drop a MongoDB database from the command line?Mongodb very verbose despite log configuration set to 0Explain MongoDB log outputMongoDB count is very slow

What is the legal status of travelling with methadone in your carry-on?

What exactly is the 'online' in OLAP and OLTP?

Why tighten down in a criss-cross pattern?

Understanding the reasoning of the woman who agreed with King Solomon to "cut the baby in half"

How dangerous are set-size assumptions?

What happens to Cessna electric flaps that are moving when power is lost?

How to draw this center trajectory of rolling ball?

Can humans ever directly see a few photons at a time? Can a human see a single photon?

What does it mean to "control target player"?

Is it damaging to turn off a small fridge for two days every week?

What is the origin of Scooby-Doo's name?

Old sci-fi story: radiation mutated the animals, a boy loses a limb, but it's okay because "humans used to do great with only two arms"

Who are the remaining King/Queenslayers?

Loss of power when I remove item from the outlet

Relationship between woodwinds and brass in a marching band?

How to get cool night-vision without lame drawbacks?

Can there be an UN resolution to remove a country from the UNSC?

.NET executes a SQL query and Active Monitor shows multiple rows blocking each other

Why do textbooks often include the solutions to odd or even numbered problems but not both?

Why does Linux list NVMe drives as /dev/nvme0 instead of /dev/sda?

Apply brace expansion in "reverse order"

Parameterize chained calls to a utility program in Bash

Should I prioritize my 401k over my student loans?

How do I turn off a repeating trade?



mongodb query returns results at commandline, no results in C++


What are the differences between a pointer variable and a reference variable in C++?How can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListWhat is the “-->” operator in C++?How to query MongoDB with “like”?Query for documents where array size is greater than 1How do I drop a MongoDB database from the command line?Mongodb very verbose despite log configuration set to 0Explain MongoDB log outputMongoDB count is very slow






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I query mongodb via C++. One particular query is oddly returning no results via C++ but works fine at the mongo shell (using the same credentials). I'm trying to understand why they are different and how to make the C++ query work.



This is the code in C++:



bsoncxx::builder::basic::document match_records;
match_records.append(kvp("$and", [&group_id](sub_array sub_and)
LOGGABLE;
sub_and.append([&group_id](sub_document and_sub_doc_1)
and_sub_doc_1.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document fg_id)
fg_id.append(kvp("focus_group_id", group_id.GroupId()));
);
sub_or.append([&group_id](sub_document rg_id)
rg_id.append(kvp("reading_group_id", group_id.GroupId()));
);
));
);
if (something true)
sub_and.append([&group_id](sub_document and_sub_doc_2)
and_sub_doc_2.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document or_sub_stream)
or_sub_stream.append(
kvp("event_category", "focus_group"));
or_sub_stream.append(
kvp("event_category", [](sub_document subdoc)
subdoc.append(kvp("$exists", 0));
));
);
));
);
else ...
sub_and.append([&group_id](sub_document and_sub_doc_4)
and_sub_doc_4.append(kvp("utm", [](sub_document subdoc)
subdoc.append(kvp("$exists", 1));
));
);
));
LOG_INFO << bsoncxx::to_json(match_records);


That last log line emits this (which I've reformatted for readability):




"$and": [

"$or": [

"focus_group_id": 465
,

"reading_group_id": 465

]
,

"$or": [

"event_category": "focus_group",
"event_category":
"$exists": 0


]
,

"utm":
"$exists": 1


]



I execute that query thus:



bsoncxx::builder::basic::document project_fields;
project_fields.append(kvp("_id", 1), kvp("focus_group_id", 1),
kvp("reading_group_id", 1), kvp("user_id", 1),
kvp("device_id", 1), kvp("event", 1),
kvp("event_category", 1), kvp("server_time", 1),
kvp("details", 1), kvp("utm", 1));
mongocxx::options::find opts;
opts.projection(project_fields.view());
mongocxx::collection candy_events = mongo_db["candy_events"];
mongocxx::cursor cursor = candy_events.find(match_records.view(), opts);
for (const bsoncxx::document::view& doc : cursor) {
...


and see that the for loop body is never executed (the query returns no results).



Watching the mongod logs, I see that the query is received with no error flagged:



2019-03-25T08:13:27.864Z I NETWORK [conn6] received client metadata from x,y,z.w:49840 conn6: driver: name: "mongoc / mongocxx", version: "1.13.1-dev / 3.4.0" , os: type: "Linux", name: "Ubuntu", version: "16.04", architecture: "x86_64" , platform: "cfg=0x215680e9 posix=200809 stdc=201112 CC=GCC 5.4.0 20160609 CFLAGS="" LDFLAGS=""" 
2019-03-25T08:13:27.888Z I ACCESS [conn6] Successfully authenticated as principal analytics-staging on example_staging
2019-03-25T08:13:32.104Z I COMMAND [conn6] command example_staging.candy_events command: find find: "candy_events", filter: $and: [ $or: [ focus_group_id: 465 , reading_group_id: 465 ] , $or: [ event_category: "focus_group", event_category: $exists: 0 ] , utm: $exists: 1 ] , projection: _id: 1, focus_group_id: 1, reading_group_id: 1, user_id: 1, device_id: 1, event: 1, event_category: 1, server_time: 1, details: 1, utm: 1 , $db: "example_staging", $readPreference: mode: "primaryPreferred" , lsid: id: UUID("ed852824-f336-40a6-a5c4-54b36439e03b") planSummary: COLLSCAN keysExamined:0 docsExamined:899850 cursorExhausted:1 numYields:7030 nreturned:0 reslen:104 locks: Global: acquireCount: r: 14062 , Database: acquireCount: r: 7031 , Collection: acquireCount: r: 7031 protocol:op_msg 1043ms
2019-03-25T08:13:32.109Z I NETWORK [conn6] end connection x.y.z.w:49840 (1 connection now open)


If I simply copy/paste the query into a db.candy_events.find(), I do get results back and the log shows this:



2019-03-25T08:19:50.566Z I NETWORK [listener] connection accepted from x.y.z.w:40570 #11 (2 connections now open)
2019-03-25T08:19:50.569Z I NETWORK [conn11] received client metadata from x.y.z.w:40570 conn11: application: name: "MongoDB Shell" , driver: name: "MongoDB Internal Client", version: "3.6.5" , os: type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "16.04"
2019-03-25T08:19:50.585Z I ACCESS [conn11] Successfully authenticated as principal analytics-staging on example_staging
2019-03-25T08:19:50.586Z I ACCESS [conn11] Unauthorized: not authorized on admin to execute command getLog: "startupWarnings", $db: "admin"
2019-03-25T08:19:50.588Z I ACCESS [conn11] Unauthorized: not authorized on admin to execute command replSetGetStatus: 1.0, forShell: 1.0, $db: "admin"
2019-03-25T08:20:07.031Z I COMMAND [conn11] command example_staging.candy_events appName: "MongoDB Shell" command: find find: "candy_events", filter: $and: [ $or: [ focus_group_id: 465.0 , reading_group_id: 465.0 ] , $or: [ event_category: $exists: 0.0 ] , utm: $exists: 1.0 ] , $db: "example_staging" planSummary: COLLSCAN cursorid:116556503976 keysExamined:0 docsExamined:884119 numYields:6907 nreturned:101 reslen:77090 locks: Global: acquireCount: r: 13816 , Database: acquireCount: r: 6908 , Collection: acquireCount: r: 6908 protocol:op_msg 628ms
2019-03-25T08:20:30.732Z I NETWORK [conn11] end connection x.y.z.w:40570 (1 connection now open)


This is mongod 3.6.5. The difference in query time appears to be cache warming. I suspect the unauthorized admin execution warning is something the shell does at startup, I'm authenticating against example_staging.



Any suggestions what I might be doing wrong or how to go about making this work?



Update / solution



Thanks to @neil-lunn's suggestion to use the profiler, I found that a small mistake in my C++ invocation. That middle clause should have been this:



 if (something true) 
sub_and.append([&group_id](sub_document and_sub_doc_2)
and_sub_doc_2.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document is_fg)
is_fg.append(kvp("event_category", "focus_group"));
);
sub_or.append([&group_id](sub_document is_absent)
is_absent.append(
kvp("event_category", [](sub_document subdoc)
subdoc.append(kvp("$exists", 0));
));
);
));
);
else ...









share|improve this question



















  • 3





    Have you tried serializing the BSON to check it's actually the same? Or at least looked in the query profiler to see what was sent to the server and check that for consistency? Working with the BSON constructors in C++ can be "messy", so the profiler or any logging is really your new best friend and the first thing you should be checking.

    – Neil Lunn
    Mar 25 at 8:47

















2















I query mongodb via C++. One particular query is oddly returning no results via C++ but works fine at the mongo shell (using the same credentials). I'm trying to understand why they are different and how to make the C++ query work.



This is the code in C++:



bsoncxx::builder::basic::document match_records;
match_records.append(kvp("$and", [&group_id](sub_array sub_and)
LOGGABLE;
sub_and.append([&group_id](sub_document and_sub_doc_1)
and_sub_doc_1.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document fg_id)
fg_id.append(kvp("focus_group_id", group_id.GroupId()));
);
sub_or.append([&group_id](sub_document rg_id)
rg_id.append(kvp("reading_group_id", group_id.GroupId()));
);
));
);
if (something true)
sub_and.append([&group_id](sub_document and_sub_doc_2)
and_sub_doc_2.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document or_sub_stream)
or_sub_stream.append(
kvp("event_category", "focus_group"));
or_sub_stream.append(
kvp("event_category", [](sub_document subdoc)
subdoc.append(kvp("$exists", 0));
));
);
));
);
else ...
sub_and.append([&group_id](sub_document and_sub_doc_4)
and_sub_doc_4.append(kvp("utm", [](sub_document subdoc)
subdoc.append(kvp("$exists", 1));
));
);
));
LOG_INFO << bsoncxx::to_json(match_records);


That last log line emits this (which I've reformatted for readability):




"$and": [

"$or": [

"focus_group_id": 465
,

"reading_group_id": 465

]
,

"$or": [

"event_category": "focus_group",
"event_category":
"$exists": 0


]
,

"utm":
"$exists": 1


]



I execute that query thus:



bsoncxx::builder::basic::document project_fields;
project_fields.append(kvp("_id", 1), kvp("focus_group_id", 1),
kvp("reading_group_id", 1), kvp("user_id", 1),
kvp("device_id", 1), kvp("event", 1),
kvp("event_category", 1), kvp("server_time", 1),
kvp("details", 1), kvp("utm", 1));
mongocxx::options::find opts;
opts.projection(project_fields.view());
mongocxx::collection candy_events = mongo_db["candy_events"];
mongocxx::cursor cursor = candy_events.find(match_records.view(), opts);
for (const bsoncxx::document::view& doc : cursor) {
...


and see that the for loop body is never executed (the query returns no results).



Watching the mongod logs, I see that the query is received with no error flagged:



2019-03-25T08:13:27.864Z I NETWORK [conn6] received client metadata from x,y,z.w:49840 conn6: driver: name: "mongoc / mongocxx", version: "1.13.1-dev / 3.4.0" , os: type: "Linux", name: "Ubuntu", version: "16.04", architecture: "x86_64" , platform: "cfg=0x215680e9 posix=200809 stdc=201112 CC=GCC 5.4.0 20160609 CFLAGS="" LDFLAGS=""" 
2019-03-25T08:13:27.888Z I ACCESS [conn6] Successfully authenticated as principal analytics-staging on example_staging
2019-03-25T08:13:32.104Z I COMMAND [conn6] command example_staging.candy_events command: find find: "candy_events", filter: $and: [ $or: [ focus_group_id: 465 , reading_group_id: 465 ] , $or: [ event_category: "focus_group", event_category: $exists: 0 ] , utm: $exists: 1 ] , projection: _id: 1, focus_group_id: 1, reading_group_id: 1, user_id: 1, device_id: 1, event: 1, event_category: 1, server_time: 1, details: 1, utm: 1 , $db: "example_staging", $readPreference: mode: "primaryPreferred" , lsid: id: UUID("ed852824-f336-40a6-a5c4-54b36439e03b") planSummary: COLLSCAN keysExamined:0 docsExamined:899850 cursorExhausted:1 numYields:7030 nreturned:0 reslen:104 locks: Global: acquireCount: r: 14062 , Database: acquireCount: r: 7031 , Collection: acquireCount: r: 7031 protocol:op_msg 1043ms
2019-03-25T08:13:32.109Z I NETWORK [conn6] end connection x.y.z.w:49840 (1 connection now open)


If I simply copy/paste the query into a db.candy_events.find(), I do get results back and the log shows this:



2019-03-25T08:19:50.566Z I NETWORK [listener] connection accepted from x.y.z.w:40570 #11 (2 connections now open)
2019-03-25T08:19:50.569Z I NETWORK [conn11] received client metadata from x.y.z.w:40570 conn11: application: name: "MongoDB Shell" , driver: name: "MongoDB Internal Client", version: "3.6.5" , os: type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "16.04"
2019-03-25T08:19:50.585Z I ACCESS [conn11] Successfully authenticated as principal analytics-staging on example_staging
2019-03-25T08:19:50.586Z I ACCESS [conn11] Unauthorized: not authorized on admin to execute command getLog: "startupWarnings", $db: "admin"
2019-03-25T08:19:50.588Z I ACCESS [conn11] Unauthorized: not authorized on admin to execute command replSetGetStatus: 1.0, forShell: 1.0, $db: "admin"
2019-03-25T08:20:07.031Z I COMMAND [conn11] command example_staging.candy_events appName: "MongoDB Shell" command: find find: "candy_events", filter: $and: [ $or: [ focus_group_id: 465.0 , reading_group_id: 465.0 ] , $or: [ event_category: $exists: 0.0 ] , utm: $exists: 1.0 ] , $db: "example_staging" planSummary: COLLSCAN cursorid:116556503976 keysExamined:0 docsExamined:884119 numYields:6907 nreturned:101 reslen:77090 locks: Global: acquireCount: r: 13816 , Database: acquireCount: r: 6908 , Collection: acquireCount: r: 6908 protocol:op_msg 628ms
2019-03-25T08:20:30.732Z I NETWORK [conn11] end connection x.y.z.w:40570 (1 connection now open)


This is mongod 3.6.5. The difference in query time appears to be cache warming. I suspect the unauthorized admin execution warning is something the shell does at startup, I'm authenticating against example_staging.



Any suggestions what I might be doing wrong or how to go about making this work?



Update / solution



Thanks to @neil-lunn's suggestion to use the profiler, I found that a small mistake in my C++ invocation. That middle clause should have been this:



 if (something true) 
sub_and.append([&group_id](sub_document and_sub_doc_2)
and_sub_doc_2.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document is_fg)
is_fg.append(kvp("event_category", "focus_group"));
);
sub_or.append([&group_id](sub_document is_absent)
is_absent.append(
kvp("event_category", [](sub_document subdoc)
subdoc.append(kvp("$exists", 0));
));
);
));
);
else ...









share|improve this question



















  • 3





    Have you tried serializing the BSON to check it's actually the same? Or at least looked in the query profiler to see what was sent to the server and check that for consistency? Working with the BSON constructors in C++ can be "messy", so the profiler or any logging is really your new best friend and the first thing you should be checking.

    – Neil Lunn
    Mar 25 at 8:47













2












2








2








I query mongodb via C++. One particular query is oddly returning no results via C++ but works fine at the mongo shell (using the same credentials). I'm trying to understand why they are different and how to make the C++ query work.



This is the code in C++:



bsoncxx::builder::basic::document match_records;
match_records.append(kvp("$and", [&group_id](sub_array sub_and)
LOGGABLE;
sub_and.append([&group_id](sub_document and_sub_doc_1)
and_sub_doc_1.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document fg_id)
fg_id.append(kvp("focus_group_id", group_id.GroupId()));
);
sub_or.append([&group_id](sub_document rg_id)
rg_id.append(kvp("reading_group_id", group_id.GroupId()));
);
));
);
if (something true)
sub_and.append([&group_id](sub_document and_sub_doc_2)
and_sub_doc_2.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document or_sub_stream)
or_sub_stream.append(
kvp("event_category", "focus_group"));
or_sub_stream.append(
kvp("event_category", [](sub_document subdoc)
subdoc.append(kvp("$exists", 0));
));
);
));
);
else ...
sub_and.append([&group_id](sub_document and_sub_doc_4)
and_sub_doc_4.append(kvp("utm", [](sub_document subdoc)
subdoc.append(kvp("$exists", 1));
));
);
));
LOG_INFO << bsoncxx::to_json(match_records);


That last log line emits this (which I've reformatted for readability):




"$and": [

"$or": [

"focus_group_id": 465
,

"reading_group_id": 465

]
,

"$or": [

"event_category": "focus_group",
"event_category":
"$exists": 0


]
,

"utm":
"$exists": 1


]



I execute that query thus:



bsoncxx::builder::basic::document project_fields;
project_fields.append(kvp("_id", 1), kvp("focus_group_id", 1),
kvp("reading_group_id", 1), kvp("user_id", 1),
kvp("device_id", 1), kvp("event", 1),
kvp("event_category", 1), kvp("server_time", 1),
kvp("details", 1), kvp("utm", 1));
mongocxx::options::find opts;
opts.projection(project_fields.view());
mongocxx::collection candy_events = mongo_db["candy_events"];
mongocxx::cursor cursor = candy_events.find(match_records.view(), opts);
for (const bsoncxx::document::view& doc : cursor) {
...


and see that the for loop body is never executed (the query returns no results).



Watching the mongod logs, I see that the query is received with no error flagged:



2019-03-25T08:13:27.864Z I NETWORK [conn6] received client metadata from x,y,z.w:49840 conn6: driver: name: "mongoc / mongocxx", version: "1.13.1-dev / 3.4.0" , os: type: "Linux", name: "Ubuntu", version: "16.04", architecture: "x86_64" , platform: "cfg=0x215680e9 posix=200809 stdc=201112 CC=GCC 5.4.0 20160609 CFLAGS="" LDFLAGS=""" 
2019-03-25T08:13:27.888Z I ACCESS [conn6] Successfully authenticated as principal analytics-staging on example_staging
2019-03-25T08:13:32.104Z I COMMAND [conn6] command example_staging.candy_events command: find find: "candy_events", filter: $and: [ $or: [ focus_group_id: 465 , reading_group_id: 465 ] , $or: [ event_category: "focus_group", event_category: $exists: 0 ] , utm: $exists: 1 ] , projection: _id: 1, focus_group_id: 1, reading_group_id: 1, user_id: 1, device_id: 1, event: 1, event_category: 1, server_time: 1, details: 1, utm: 1 , $db: "example_staging", $readPreference: mode: "primaryPreferred" , lsid: id: UUID("ed852824-f336-40a6-a5c4-54b36439e03b") planSummary: COLLSCAN keysExamined:0 docsExamined:899850 cursorExhausted:1 numYields:7030 nreturned:0 reslen:104 locks: Global: acquireCount: r: 14062 , Database: acquireCount: r: 7031 , Collection: acquireCount: r: 7031 protocol:op_msg 1043ms
2019-03-25T08:13:32.109Z I NETWORK [conn6] end connection x.y.z.w:49840 (1 connection now open)


If I simply copy/paste the query into a db.candy_events.find(), I do get results back and the log shows this:



2019-03-25T08:19:50.566Z I NETWORK [listener] connection accepted from x.y.z.w:40570 #11 (2 connections now open)
2019-03-25T08:19:50.569Z I NETWORK [conn11] received client metadata from x.y.z.w:40570 conn11: application: name: "MongoDB Shell" , driver: name: "MongoDB Internal Client", version: "3.6.5" , os: type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "16.04"
2019-03-25T08:19:50.585Z I ACCESS [conn11] Successfully authenticated as principal analytics-staging on example_staging
2019-03-25T08:19:50.586Z I ACCESS [conn11] Unauthorized: not authorized on admin to execute command getLog: "startupWarnings", $db: "admin"
2019-03-25T08:19:50.588Z I ACCESS [conn11] Unauthorized: not authorized on admin to execute command replSetGetStatus: 1.0, forShell: 1.0, $db: "admin"
2019-03-25T08:20:07.031Z I COMMAND [conn11] command example_staging.candy_events appName: "MongoDB Shell" command: find find: "candy_events", filter: $and: [ $or: [ focus_group_id: 465.0 , reading_group_id: 465.0 ] , $or: [ event_category: $exists: 0.0 ] , utm: $exists: 1.0 ] , $db: "example_staging" planSummary: COLLSCAN cursorid:116556503976 keysExamined:0 docsExamined:884119 numYields:6907 nreturned:101 reslen:77090 locks: Global: acquireCount: r: 13816 , Database: acquireCount: r: 6908 , Collection: acquireCount: r: 6908 protocol:op_msg 628ms
2019-03-25T08:20:30.732Z I NETWORK [conn11] end connection x.y.z.w:40570 (1 connection now open)


This is mongod 3.6.5. The difference in query time appears to be cache warming. I suspect the unauthorized admin execution warning is something the shell does at startup, I'm authenticating against example_staging.



Any suggestions what I might be doing wrong or how to go about making this work?



Update / solution



Thanks to @neil-lunn's suggestion to use the profiler, I found that a small mistake in my C++ invocation. That middle clause should have been this:



 if (something true) 
sub_and.append([&group_id](sub_document and_sub_doc_2)
and_sub_doc_2.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document is_fg)
is_fg.append(kvp("event_category", "focus_group"));
);
sub_or.append([&group_id](sub_document is_absent)
is_absent.append(
kvp("event_category", [](sub_document subdoc)
subdoc.append(kvp("$exists", 0));
));
);
));
);
else ...









share|improve this question
















I query mongodb via C++. One particular query is oddly returning no results via C++ but works fine at the mongo shell (using the same credentials). I'm trying to understand why they are different and how to make the C++ query work.



This is the code in C++:



bsoncxx::builder::basic::document match_records;
match_records.append(kvp("$and", [&group_id](sub_array sub_and)
LOGGABLE;
sub_and.append([&group_id](sub_document and_sub_doc_1)
and_sub_doc_1.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document fg_id)
fg_id.append(kvp("focus_group_id", group_id.GroupId()));
);
sub_or.append([&group_id](sub_document rg_id)
rg_id.append(kvp("reading_group_id", group_id.GroupId()));
);
));
);
if (something true)
sub_and.append([&group_id](sub_document and_sub_doc_2)
and_sub_doc_2.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document or_sub_stream)
or_sub_stream.append(
kvp("event_category", "focus_group"));
or_sub_stream.append(
kvp("event_category", [](sub_document subdoc)
subdoc.append(kvp("$exists", 0));
));
);
));
);
else ...
sub_and.append([&group_id](sub_document and_sub_doc_4)
and_sub_doc_4.append(kvp("utm", [](sub_document subdoc)
subdoc.append(kvp("$exists", 1));
));
);
));
LOG_INFO << bsoncxx::to_json(match_records);


That last log line emits this (which I've reformatted for readability):




"$and": [

"$or": [

"focus_group_id": 465
,

"reading_group_id": 465

]
,

"$or": [

"event_category": "focus_group",
"event_category":
"$exists": 0


]
,

"utm":
"$exists": 1


]



I execute that query thus:



bsoncxx::builder::basic::document project_fields;
project_fields.append(kvp("_id", 1), kvp("focus_group_id", 1),
kvp("reading_group_id", 1), kvp("user_id", 1),
kvp("device_id", 1), kvp("event", 1),
kvp("event_category", 1), kvp("server_time", 1),
kvp("details", 1), kvp("utm", 1));
mongocxx::options::find opts;
opts.projection(project_fields.view());
mongocxx::collection candy_events = mongo_db["candy_events"];
mongocxx::cursor cursor = candy_events.find(match_records.view(), opts);
for (const bsoncxx::document::view& doc : cursor) {
...


and see that the for loop body is never executed (the query returns no results).



Watching the mongod logs, I see that the query is received with no error flagged:



2019-03-25T08:13:27.864Z I NETWORK [conn6] received client metadata from x,y,z.w:49840 conn6: driver: name: "mongoc / mongocxx", version: "1.13.1-dev / 3.4.0" , os: type: "Linux", name: "Ubuntu", version: "16.04", architecture: "x86_64" , platform: "cfg=0x215680e9 posix=200809 stdc=201112 CC=GCC 5.4.0 20160609 CFLAGS="" LDFLAGS=""" 
2019-03-25T08:13:27.888Z I ACCESS [conn6] Successfully authenticated as principal analytics-staging on example_staging
2019-03-25T08:13:32.104Z I COMMAND [conn6] command example_staging.candy_events command: find find: "candy_events", filter: $and: [ $or: [ focus_group_id: 465 , reading_group_id: 465 ] , $or: [ event_category: "focus_group", event_category: $exists: 0 ] , utm: $exists: 1 ] , projection: _id: 1, focus_group_id: 1, reading_group_id: 1, user_id: 1, device_id: 1, event: 1, event_category: 1, server_time: 1, details: 1, utm: 1 , $db: "example_staging", $readPreference: mode: "primaryPreferred" , lsid: id: UUID("ed852824-f336-40a6-a5c4-54b36439e03b") planSummary: COLLSCAN keysExamined:0 docsExamined:899850 cursorExhausted:1 numYields:7030 nreturned:0 reslen:104 locks: Global: acquireCount: r: 14062 , Database: acquireCount: r: 7031 , Collection: acquireCount: r: 7031 protocol:op_msg 1043ms
2019-03-25T08:13:32.109Z I NETWORK [conn6] end connection x.y.z.w:49840 (1 connection now open)


If I simply copy/paste the query into a db.candy_events.find(), I do get results back and the log shows this:



2019-03-25T08:19:50.566Z I NETWORK [listener] connection accepted from x.y.z.w:40570 #11 (2 connections now open)
2019-03-25T08:19:50.569Z I NETWORK [conn11] received client metadata from x.y.z.w:40570 conn11: application: name: "MongoDB Shell" , driver: name: "MongoDB Internal Client", version: "3.6.5" , os: type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "16.04"
2019-03-25T08:19:50.585Z I ACCESS [conn11] Successfully authenticated as principal analytics-staging on example_staging
2019-03-25T08:19:50.586Z I ACCESS [conn11] Unauthorized: not authorized on admin to execute command getLog: "startupWarnings", $db: "admin"
2019-03-25T08:19:50.588Z I ACCESS [conn11] Unauthorized: not authorized on admin to execute command replSetGetStatus: 1.0, forShell: 1.0, $db: "admin"
2019-03-25T08:20:07.031Z I COMMAND [conn11] command example_staging.candy_events appName: "MongoDB Shell" command: find find: "candy_events", filter: $and: [ $or: [ focus_group_id: 465.0 , reading_group_id: 465.0 ] , $or: [ event_category: $exists: 0.0 ] , utm: $exists: 1.0 ] , $db: "example_staging" planSummary: COLLSCAN cursorid:116556503976 keysExamined:0 docsExamined:884119 numYields:6907 nreturned:101 reslen:77090 locks: Global: acquireCount: r: 13816 , Database: acquireCount: r: 6908 , Collection: acquireCount: r: 6908 protocol:op_msg 628ms
2019-03-25T08:20:30.732Z I NETWORK [conn11] end connection x.y.z.w:40570 (1 connection now open)


This is mongod 3.6.5. The difference in query time appears to be cache warming. I suspect the unauthorized admin execution warning is something the shell does at startup, I'm authenticating against example_staging.



Any suggestions what I might be doing wrong or how to go about making this work?



Update / solution



Thanks to @neil-lunn's suggestion to use the profiler, I found that a small mistake in my C++ invocation. That middle clause should have been this:



 if (something true) 
sub_and.append([&group_id](sub_document and_sub_doc_2)
and_sub_doc_2.append(kvp("$or", [&group_id](sub_array sub_or)
sub_or.append([&group_id](sub_document is_fg)
is_fg.append(kvp("event_category", "focus_group"));
);
sub_or.append([&group_id](sub_document is_absent)
is_absent.append(
kvp("event_category", [](sub_document subdoc)
subdoc.append(kvp("$exists", 0));
));
);
));
);
else ...






c++ mongodb mongodb-query






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 12:27







jma

















asked Mar 25 at 8:42









jmajma

1,45212032




1,45212032







  • 3





    Have you tried serializing the BSON to check it's actually the same? Or at least looked in the query profiler to see what was sent to the server and check that for consistency? Working with the BSON constructors in C++ can be "messy", so the profiler or any logging is really your new best friend and the first thing you should be checking.

    – Neil Lunn
    Mar 25 at 8:47












  • 3





    Have you tried serializing the BSON to check it's actually the same? Or at least looked in the query profiler to see what was sent to the server and check that for consistency? Working with the BSON constructors in C++ can be "messy", so the profiler or any logging is really your new best friend and the first thing you should be checking.

    – Neil Lunn
    Mar 25 at 8:47







3




3





Have you tried serializing the BSON to check it's actually the same? Or at least looked in the query profiler to see what was sent to the server and check that for consistency? Working with the BSON constructors in C++ can be "messy", so the profiler or any logging is really your new best friend and the first thing you should be checking.

– Neil Lunn
Mar 25 at 8:47





Have you tried serializing the BSON to check it's actually the same? Or at least looked in the query profiler to see what was sent to the server and check that for consistency? Working with the BSON constructors in C++ can be "messy", so the profiler or any logging is really your new best friend and the first thing you should be checking.

– Neil Lunn
Mar 25 at 8:47












0






active

oldest

votes














Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55333972%2fmongodb-query-returns-results-at-commandline-no-results-in-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55333972%2fmongodb-query-returns-results-at-commandline-no-results-in-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현