[SERVER-1527] findandmodify positional operator $ in C++ driver Created: 17/Aug/10  Updated: 30/Mar/12  Resolved: 28/Feb/11

Status: Closed
Project: Core Server
Component/s: Internal Client
Affects Version/s: 1.6.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: m savy Assignee: Mathias Stearn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 10.1,


Attachments: File test.cpp    
Operating System: ALL
Participants:

 Description   

Seems to be related to this issue:" findandmodify doesn't support positional ($) operator" => http://jira.mongodb.org/browse/SERVER-1095

When I attempt to execute a positional set operation, the following error is emitted on the console: (with same example as linked).

update EXAMPLE_ARRAY query:

{ _id: ObjectId('4c6a7075c42868719b80c2e1') }

byid exception 13048 can't append to array using string field name [$] 0ms

It works fine when I use the console. My find-and-modify command is as follows:

BSONObjBuilder b;
b << "findandmodify" << collection_name.c_str() <<
"query" << Query(query) <<
"update" << update <<
"new" << see_modified;

bool result = dbcb->runCommand(string(MONGO_DBNAME),
b.obj(),
rtn);

The query phase works perfectly well.



 Comments   
Comment by Mathias Stearn [ 28/Feb/11 ]

Clearing out old cases. Please let me know if you are still having issues

Comment by Mathias Stearn [ 03/Nov/10 ]

Sorry, I seemed to have missed your comment.

BSONObj is the class for BSON objects. BSON is a macro to make them.

Query is a helper class to make the BSONObj for calls to find() including flags like sort and explain. It shouldn't be used with commands.

Comment by m savy [ 23/Sep/10 ]

I've been really busy recently and haven't had a chance to test.

The query macro is a BSONobject? I remember trying this directly without using the query macro but it caused some of my other queries to break.

When I have a bit of time I'll double check what is going on.

Comment by Mathias Stearn [ 23/Sep/10 ]

Were you able to resolve your issue?

Comment by Mathias Stearn [ 08/Sep/10 ]

I'm attaching a simple file to prove that findAndModify works with the c++ driver. You can compile it with:

g++ test.cpp -lmongoclient -lboost_thread-mt -lboost_system-mt

The only issue I think I see in your code is that you are using a Query object instead of a BSONObj. Is there any reason for this?

Comment by m savy [ 26/Aug/10 ]

Yes, in the actual version I used the dollars are in quotations, but I simplified it here - of course it would not compile otherwise with random $ symbols. As I stated in the original comment it works fine in the JS shell.

Perhaps you have managed to replicate it without the problem (which would be odd, and suggest I've done something strange)?

Comment by Mathias Stearn [ 25/Aug/10 ]

You don't seem to be quoting your string keys. CURRENT_ASSIGNEES.$.EXPIRY should probably be CURRENT_ASSIGNEES".$."EXPIRY, assuming that those are string macros.

Have you tried the equivalent findAndModify using the JS shell?

Comment by m savy [ 17/Aug/10 ]

Code I was trying to use:

/Current assignees is an array/

==== Query:
BSONObjBuilder b;
b << CURRENT_ASSIGNEES"."TOKEN << token.c_str();
return b.obj();

==== Update:

BSONObjBuilder b;
//Set new expiry
b << "$set" << BSON(CURRENT_ASSIGNEES.$.EXPIRY <<t) <<
//Revert checkins to zero
"$set" << BSON(CURRENT_ASSIGNEES.$.FAILED_CHECKINS << 0);

return b.obj();

==== Find and modify method signature

static BSONObj find_and_modify(shared_ptr<DBClientBase> dbcb, const string &collection_name,
const BSONObj query, const BSONObj update, bool see_modified=true, bool upsert = false)

Works fine with update() on this query/update pair.

Demonstration structure info:

{"DemoAttr" : "blah",
"CURRENT_ASSIGNEES" : [

{ "TOKEN" : <some token>, "USER" : "test-usr", "FAILED_CHECKINS" : 0 }

]
}

Comment by Eliot Horowitz (Inactive) [ 17/Aug/10 ]

you need an array query part in the query to use $

can you send your full update

Comment by m savy [ 17/Aug/10 ]

note that see modified was set to true.

Generated at Thu Feb 08 02:57:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.