简体   繁体   中英

MongoDB finding using an _id

I am using the latest version of the new C++ Mongodb driver/library (not the legacy, 26compat or C version) along with the Qt framework (latest 64b on Linux). Within the same program I am successfully reading and writing to the database and everything is working well.

I realize this version is unstable, but I don't want the boost dependencies and it is a new project, that only I am working on.

I'm not a professional programmer, so please forgive any knowledge gaps.

In my database I have a supporting collection that just remembers the last project a user was working on and I want to do is use a value stored within that document as a string with a field name, to load that project when the program is started.

I am wanting to use the key stored within the m_Current_Project_key variable to load the project data from the project collection.

In the code below the first line after the find statement, carries out the search using different hard coded field name and data in the same collection, just to prove the code works more generally.

The problem I am having is getting the program to search for a specific "_id" that I can see is correctly in the collection and document from the mongo command line.

The comments on the end of the lines of code below show the output achieved for different things I have tried.

This sits within a method that reads a different collection from the same database and get a value from within it, that it puts in the m_Current_Project_key variable which is a QString.

    qDebug() << m_Current_Project_key;                  // "553b976484e4b5167e39b6f1"
qDebug() << Utility::format_key(m_Current_Project_key);         // "ObjectId("553b976484e4b5167e39b6f1")" - this utility function just modifies the value passed to it to look like the output

QString test = Utility::format_key(m_Current_Project_key);
test.remove('\"');
qDebug() << test;                           // "ObjectId(553b976484e4b5167e39b6f1)"

char const *c = m_Current_Project_key.toStdString().c_str();
qDebug() << c;                          // 553b976484e4b5167e39b6f1
bsoncxx::oid hhh(c, 12);
qDebug() << hhh.get_time_t();                   // 892679010

auto cursor = db["project"].find(document{}
// << "title" << "Testing Project"
<< "_id"
<< c
//   << hhh
//   << m_Current_Project_key.toStdString()
//   << m_Current_Project_key.toStdString().c_str()
//   << Utility::format_key(m_Current_Project_key).toStdString()
//   << test.toStdString()
<< finalize);

The cursor only points to a value when I use the title line above, without the next two lines - the value I get is the document I want, but in the real situation the only thing the program would know, at this point would be the "_id". The project name might not be unique.

I have tried casting the std::string to an OID, but that wasn't recognised as a type.

I've done a lot of Googling and a lot of trying things out and I can't believe there isn't a straight forward way to find a document based on it's "_id". In the examples the only finding examples use values other than the "_id".

db.project.find({ "_id" : ObjectId("553b976484e4b5167e39b6f1")}, { title : 1  })

Does what I want on the Mongo command line.

I would appreciate any assistance I could get with this, I have spent a lot of time trying.

Thanks.

The issue here is that you are using the wrong bsoncxx::oid constructor. When creating an oid from a std::string of the hex representation of the ObjectId (eg "553b976484e4b5167e39b6f1" ) you should use the single-argument constructor that takes a stdx::string_view .

The correct code looks like this:

using bsoncxx::stdx::string_view;

auto cursor = db["project"].find(document{} 
    << "_id"
    << bsoncxx::oid{stdx::string_view{m_Current_Project_key.toStdString()}}
    << finalize
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM