我正在使用 ObjCMongoDB 作为可可包装器来访问 mongoDB。在必须查找文档并将其替换为新文档的情况下,我遇到了困难。任何人都可以通过指出要使用的 ObjCMongoDB 的代码/API 来帮助我。
例如:
{
"_id" : { "$oid" : "51de4ed737965b2d233f4862"} ,
"milestone" : "Application 7.1 release" ,
"pendingtasklist" : [ task1 , task2 , task3]
}
在这里我必须用新列表替换pendingtasklist,结果应该是
{
"_id" : { "$oid" : "51de4ed737965b2d233f4862"} ,
"milestone" : "Application 7.1 release" ,
"someotherlist" : [ task12 , task33 , task32]
}
我附上了我用来实现这一目标的代码,但没有成功
NSError *connectionError = nil;
MongoConnection *dbConn = [MongoConnection connectionForServer:@"127.0.0.1:27017" error:&connectionError];
MongoDBCollection *collection = [dbConn collectionWithName:@"mydb.milestones"];
MongoKeyedPredicate *predicate = [MongoKeyedPredicate predicate];
[predicate keyPath:@"milestone" matches:@"Application 7.1 release"];
MongoUpdateRequest *updateReq = [MongoUpdateRequest updateRequestWithPredicate:predicate firstMatchOnly:YES];
NSDictionary *milestoneDict = @{@"problemlist": @[@"12345",@"112244",@"55543",@"009009"],@"milestone":@"Application 7.1 release"};
[updateReq replaceDocumentWithDictionary:milestoneDict];
BOOL result = [collection updateWithRequest:updateReq error:&connectionError];
在我的收藏之前会有这样的文件:
{ "_id" : { "$oid" : "51de4ed737965b2d233f4862"} , "milestone" : "Application 7.1 Release" , "problemlist" : [ 12345 , 112244 , 55543]}
{ "_id" : { "$oid" : "51de4ed737965b2d233f4864"} , "milestone" : "Application 7.1 UAT" , "problemlist" : [ 33545 , 7654 , 8767]}