Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to delete a field in a document? #2128

Closed
cmdedj opened this issue Jul 10, 2019 · 11 comments
Closed

how to delete a field in a document? #2128

cmdedj opened this issue Jul 10, 2019 · 11 comments

Comments

@cmdedj
Copy link

cmdedj commented Jul 10, 2019

one document in db

{
    "_id": ObjectId("5d25c7998742685c658ea9a7"),
    "lang": "hi",
    "title": "miao",
    "en_title": "miao",
    "created_at": ISODate("2019-07-10T11:10:17.621Z"),
    "creator": "cmdedj",
    "last_edit_at": ISODate("2019-07-10T11:10:17.619Z"),
    "seq_id": NumberInt("439"),
    "search_titles": [ ],
    "alias": [ ],
    "enable_clean_outdate": true
}

how to delete lang field ?

{
    "_id": ObjectId("5d25c7998742685c658ea9a7"),
    "title": "miao",
    "en_title": "miao",
    "created_at": ISODate("2019-07-10T11:10:17.621Z"),
    "creator": "cmdedj",
    "last_edit_at": ISODate("2019-07-10T11:10:17.619Z"),
    "seq_id": NumberInt("439"),
    "search_titles": [ ],
    "alias": [ ],
    "enable_clean_outdate": true
}
@amcgregor
Copy link
Contributor

$unset

@cmdedj
Copy link
Author

cmdedj commented Jul 11, 2019

@amcgregor i mean use it in mongoengine function not a mongo sql

@amcgregor
Copy link
Contributor

amcgregor commented Jul 11, 2019

[Let me prefix this with an apology. Your exact type of dismissal of the valid answer without even seemingly looking at it is something that gets under my skin. So while this may seem overly hostile, there is some level of need for this type of response to discourage that process of asking questions and expecting whole solutions be handed back in the future. I'm not respectful, here, but neither is that stance for a questioner respectful of the time of those helping.]

Your statement is gibberish. MongoEngine or not, that is the operation that needs to be executed. Do you see unset in the documentation on atomic updates? Sure you do! I don't believe you're blind. Just unmotivated to RTFM yourself and desirous of a complete, packaged solution tailored to your needs to be handed to you wholesale. Work is hard. That ain't gonna happen. So please, use your head and/or read links provided before dismissing them outright and/or RTFM.

Additionally, you haven't provided any MongoEngine model, there. It looks like plain PyMongo. So I have nothing to base a MongoEngine-specific example on. "One document in db" also provides no descriptive value. I don't even know what name to call my hypothetical MongoEngine model class, here, so you get the most generic example possible:

class Foo(Document):
    bar = StringField()

Foo.objects.update(unset__bar=True)  # Note: Updated! Whoops!

That's the best that can be provided, really. The irony being to unset a field using MongoEngine, you need to ensure you have a field defined by that name! (Which is silly!) There are also potential issues with loading documents which do not conform to the declared schema; frequently I have explosions (uncaught exceptions and associated traceback) caused by apparent schema violations egregious enough to throw up hands and give up. Apparently.

There's also the sticky point that MongoEngine may be too sensitive to the presence or lack of presence of declared fields, unless you do the silly thing and use DynamicDocument everywhere, but at that point, I have questions re: the point of continued use of MongoEngine given its problems (and I haven't even updated this list in the last two years… I was literally in a coma for a portion of this time…)

@cmdedj
Copy link
Author

cmdedj commented Jul 11, 2019

@amcgregor thank you very much. your answer is very detailed。
Let me apologize for not describing my question clearly.

I originally wanted to apply it to django rest framework.
i want to access a value from body then can delete the field.

    body: {
            "lang": null
    }

now i will rewrite serializer.update(self, instance, validated_data)

     def update(self, instance, validated_data):
          pass

if i use your method

class Foo(Document):
    bar = StringField()

Foo.objects(unset__bar=True)

if will delete bar field in all documents
but i only want delete it in one document
how should i rewrite the update function?
may i use it?

    instance.update_one(unset__bar=True)

i try it but it not work
how can i use the instance to unset field?

Thank you for your criticism, I will correct them later.
And thank you again for your enthusiastic answer.

@amcgregor
Copy link
Contributor

I originally wanted to apply it to django rest framework.

I can not assist you with that.

if i use your method

Good point, there's a problem in that. I have corrected it, which should now more obviously identify the correct mechanism to restrict that change to a specific document. Note the use of .objects, and remember how documents are queried using MongoEngine. And how you filter them.

i try it but it not work

You can't just invent syntax like that; MongoEngine Document instances don't have that as a method. There are APIs that do exist that you must utilize and conform to. I have linked the documentation for that API several times, it includes pure API references as well as a more tutorial-like guide that may offer a more step by step, progressive path to learning the APIs.

@cmdedj
Copy link
Author

cmdedj commented Jul 11, 2019

you mean i cant achieve it only by a model instance?

@amcgregor
Copy link
Contributor

Your question is unclear: what do you mean by "model instance"? If you mean model class, if you are using MongoEngine for data access, you ought to already have one, and yes, use it, see my three line example. You have been provided absolutely everything you need to be able to do what you initially asked, in two major ways (with and without MongoEngine), with a demonstration and links to relevant documentation. There's not much more I could do than literally write your code for you, which I already essentially have with the three line example.

Good luck, you have exceeded my capacity to assist.

@cmdedj
Copy link
Author

cmdedj commented Jul 11, 2019

@amcgregor ok Maybe I didn't describe the problem clearly.
Thank you for your patience.

@bagerard
Copy link
Collaborator

Hi @cmdedj If you have an existing Document instance, you can unset the field by using python's del keyword. It will translate to an $unset behind the scene when you'll .save() the instance

See below
image

@cmdedj
Copy link
Author

cmdedj commented Jul 15, 2019

@bagerard thank you very much, it's the best answer i wanted!!!
you solve my question~

@cmdedj cmdedj closed this as completed Jul 15, 2019
@cmdedj
Copy link
Author

cmdedj commented Jul 15, 2019

@bagerard thank you,you can always know what i want and then solve it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants