[CXX-860] Fail Combine Multiple Nested Queries with MongoDB C+11 Driver Created: 09/Mar/16  Updated: 24/Mar/16  Resolved: 11/Mar/16

Status: Closed
Project: C++ Driver
Component/s: API
Affects Version/s: 3.0.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: zy1989 [X] Assignee: Matt Cotter
Resolution: Done Votes: 0
Labels: query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CXX-877 Have no idea how to add bsoncxx::docu... Closed

 Description   

error: invalid operands to binary expression ('key_context > >' and 'bsoncxx::v_noabi::builder::stream::document') open_document << conditionI << close_document << ~~~~~~~~~~~~~ ^ ~~~~~~~~~~

using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::finalize;
typedef mongocxx::cursor::iterator dociter;
typedef bsoncxx::v_noabi::document::view docview;
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{"mongodb://10.9.0.1:27017"}};
mongocxx::collection collection = conn["crawler"]["article"];
mongocxx::options::find opts;
document query, conditionI, conditionII, options;
conditionI << "publishTime" << open_document << "$gte" << timedomain << close_document;
conditionII << "lastread" << open_document << "$gte" << readdomain << close_document;
options << "read" << 1 << "likes" << 1 << "body.title" << 1 << "body.content" << 1;
opts.limit(100);
opts.sort(options.view());
query << "$or" << open_array <<
open_document << conditionI << close_document <<
open_document << conditionII << close_document <<
close_array;
mongocxx::cursor cursor = collection.find(conditionI.view(), opts);



 Comments   
Comment by Matt Cotter [ 11/Mar/16 ]

Glad to help! If you have other feedback on the driver, please let us know!

Comment by zy1989 [X] [ 10/Mar/16 ]

In addition, following code also work.

condition << "$and" << open_array <<
open_document << "subscribeRobot" << open_document << "$exists" << 1 << close_document << close_document << open_document << "body.title" << open_document << "$exists" << 1 << close_document << close_document <<
open_document << "body.content" << open_document << "$exists" << 1 << close_document << close_document <<
close_array;

options << "subscribeRobot" << 1 << "body.title" << 1 << "body.content" << 1;
opts.projection(options.view());
copts.limit(1000);

mongocxx::cursor cursor = collection.find(condition.view(), opts);

Comment by zy1989 [X] [ 10/Mar/16 ]

Thank Matt, It works.

Comment by Matt Cotter [ 09/Mar/16 ]

Hi LeoZhao,

The issue is that you are trying to append the bsoncxx::builder::stream::document into the stream after the open document:

open_document << conditionI << close_document

In order to accomplish this, you can either concatenate the document:

open_document << concatenate_doc{conditionI.view()} << close_document

or you can simply append the `conditionI` document as a bson document type:

bsoncxx::types::b_document{conditionI.view()}

Below is an example to illustrate this:

#include <iostream>
 
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/builder/concatenate.hpp>
#include <bsoncxx/json.hpp>
 
using bsoncxx::builder::stream::document;
using bsoncxx::builder::concatenate_doc;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::finalize;
 
int main() {
    document query1, query2, conditionI, conditionII;
 
    conditionI << "publishTime" << open_document << "$gte" << 1 << close_document;
    conditionII << "lastread" << open_document << "$gte" << 2 << close_document;
 
    query1 << "$or" << open_array <<
        open_document << concatenate_doc{conditionI.view()} << close_document <<
        open_document << concatenate_doc{conditionII.view()} << close_document <<
        close_array;
 
    query2 << "$or" << open_array << bsoncxx::types::b_document{conditionI.view()}
        << bsoncxx::types::b_document{conditionII.view()} << close_array;
 
 
    std::cout << bsoncxx::to_json(query1.view()) << std::endl;
    std::cout << bsoncxx::to_json(query2.view()) << std::endl;
}

Generated at Wed Feb 07 22:00:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.