[SERVER-14345] Give driver additional details on $pull Created: 24/Jun/14  Updated: 10/Dec/14  Resolved: 24/Jun/14

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

Type: Improvement Priority: Minor - P4
Reporter: Daryl Tucker Assignee: Unassigned
Resolution: Done Votes: 0
Labels: $pull
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

debian 7 (Wheezy) mongodb 2.4.10


Participants:

 Description   

I understand why one would appreciate $pull success whether or not the item was actually pulled (since the effect is the same), but it would be nice to be able to determine if something was removed/if the document was changed.

    > db.test.findOne({})
    {
            "_id" : ObjectId("53a9e45a9593e7fb44ad0aae"),
            "credits" : [
                    "11111",
                    "22222",
                    "33333"
            ]
    }
 
    >>> x = db.test.update({'_id': ObjectId("53a9e45a9593e7fb44ad0aae")},{'$pull': {'test': '2222'}})
    >>> x
    {u'updatedExisting': True, u'connectionId': 7, u'ok': 1.0, u'err': None, u'n': 1}
    >>> x = db.test.update({'_id': ObjectId("53a9e45a9593e7fb44ad0aae")},{'$pull': {'test': '2222'}})
    >>> x
    {u'updatedExisting': True, u'connectionId': 7, u'ok': 1.0, u'err': None, u'n': 1}

If some field:value could be added, such as

{'documentChanged': True}

, then one could programmatically determine if a $pull query has made any modifications to the document.



 Comments   
Comment by Bernie Hackett [ 24/Jun/14 ]

Great! Let us know if you run into any other issues.

Comment by Daryl Tucker [ 24/Jun/14 ]

Thank you for your patience and clarification.

I have updated to mongo version `2.6.1` (after realizing the packages changed from *-10gen* to *-org*). You are absolutely correct that 'nModified' is returned with this version and that it reflects whether or not the document has been changed/modified.

        > db.test.update({'_id': ObjectId("53a9e45a9593e7fb44ad0aae")},{'$pull': {'credits': '22222'}})
        WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
        > db.test.update({'_id': ObjectId("53a9e45a9593e7fb44ad0aae")},{'$pull': {'credits': '22222'}})
        WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

Comment by Bernie Hackett [ 24/Jun/14 ]

I think MongoDB 2.6 may do what you want. It returns an nModified field with a count of the documents that were actually modified:

http://docs.mongodb.org/manual/reference/command/update/#update-specific-fields-of-one-document

An example in python using your example document:

>>> db.test.update({'_id': ObjectId("53a9e45a9593e7fb44ad0aae")},{'$pull': {'test': '2222'}})
{u'nModified': 0, u'ok': 1, u'n': 0, u'updatedExisting': False, u'electionId': ObjectId('53a9ea4fb757c690e63e0c73'), u'lastOp': Timestamp(0, 0)}

Comment by Bernie Hackett [ 24/Jun/14 ]

The python driver is just returning the raw result from the server. The server itself would have to return the field you are asking for.

Comment by Daryl Tucker [ 24/Jun/14 ]

I appologize for the horrible formatting. I was attempting to use Markdown and I see no way to edit for clarity.

Generated at Thu Feb 08 03:34:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.