How to do & ldquo; insert if there is not otherwise update & rdquo; with...
source link: https://www.codesd.com/item/how-to-do-insert-if-there-is-not-otherwise-update-with-mongoengine.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to do & ldquo; insert if there is not otherwise update & rdquo; with mongoengine?
I'm working with mongoengine in Django,
this is my document defination:
class Location(mongoengine.Document):
user_id = mongoengine.IntField(required=True)
point = mongoengine.GeoPointField(required=True)
I want to do this:
given a user_id
and a point
:
if there is no document that have this user_id
, create one with the user_id
and point
and save it;
else update the document with user_id
with point
.
Can I do this in one statement with mongoengine?
Note that get_or_create
is now scheduled to be deprecated, because with no transaction support in MongoDB it cannot ensure atomicity.
The preferred way is update with upsert:
Location.objects(user_id=user_id).update_one(set__point=point, upsert=True)
More on upserts on the MongoDB documentation.
Related Articles
How to repair & ldquo; The ConnectionString property has not been initialized & rdquo;
how to repair & ldquo; WARNING: The terminal is not fully functional & rdquo; error in the control console?
How to catch & ldquo; Sorry, this video can not be played & rdquo; error on VideoView
How to solve & ldquo; The node '& hellip;' was not found. & rdquo; in SVN
How to fix & ldquo; TypeError Uncaught: undefined is not a function & rdquo;
How to create the insert function for a binary search tree constructed with a vector?
GWT / JSNI - & ldquo; DataCloneError - An object could not be cloned & rdquo; - How do I debug?
How to debug & ldquo; Parameter invalid passed to C runtime function & rdquo;
How to do & ldquo; Make & lt; Subdir & gt; & Lt; Target & gt; & Rdquo;
How to check the file is there or not in javascript?
How do you check & ldquo; otherwise null & rdquo; with Eloquent?
The Java MVC project configuration gives & ldquo; The requested resource is not available. & Rdquo;
How can I just have the date and not the time displayed with it in a mySQL table?
How to perform & ldquo; Insert or Increment & rdquo; In a MongoDB query?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK