Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Update a document by replacing it with a new document in a collection of mongoDB
    primarykey
    data
    text
    <p>I am using ObjCMongoDB as a cocoa wrapper for accessing mongoDB. I am facing difficulty in a scenario where I have to find and replace a document with a new document. Can any one help me by pointing out the code/API of ObjCMongoDB to use.</p> <p>For example:</p> <pre><code>{ "_id" : { "$oid" : "51de4ed737965b2d233f4862"} , "milestone" : "Application 7.1 release" , "pendingtasklist" : [ task1 , task2 , task3] } </code></pre> <p>here I have to <strong>replace</strong> pendingtasklist with new list and result should be </p> <pre><code>{ "_id" : { "$oid" : "51de4ed737965b2d233f4862"} , "milestone" : "Application 7.1 release" , "someotherlist" : [ task12 , task33 , task32] } </code></pre> <p>I have attached the code I am using to achieve this, but without success </p> <pre><code>NSError *connectionError = nil; MongoConnection *dbConn = [MongoConnection connectionForServer:@"127.0.0.1:27017" error:&amp;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:&amp;connectionError]; </code></pre> <p>Before my collection will have documents like this:</p> <pre><code>{ "_id" : { "$oid" : "51de4ed737965b2d233f4862"} , "milestone" : "Application 7.1 Release" , "problemlist" : [ 12345 , 112244 , 55543]} { "_id" : { "$oid" : "51de4ed737965b2d233f4864"} , "milestone" : "Application 7.1 UAT" , "problemlist" : [ 33545 , 7654 , 8767]} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload