-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
@amcgregor i mean use it in mongoengine function not a mongo sql |
[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 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…) |
@amcgregor thank you very much. your answer is very detailed。 I originally wanted to apply it to django rest framework.
now i will rewrite serializer.update(self, instance, validated_data)
if i use your method
if will delete bar field in all documents
i try it but it not work Thank you for your criticism, I will correct them later. |
I can not assist you with that.
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
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. |
you mean i cant achieve it only by a model instance? |
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. |
@amcgregor ok Maybe I didn't describe the problem clearly. |
Hi @cmdedj If you have an existing Document instance, you can unset the field by using python's |
@bagerard thank you very much, it's the best answer i wanted!!! |
@bagerard thank you,you can always know what i want and then solve it |
one document in db
how to delete lang field ?
The text was updated successfully, but these errors were encountered: