5

How to do & ldquo; insert if there is not otherwise update & rdquo; with...

 3 years ago
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.
neoserver,ios ssh client

How to do & ldquo; insert if there is not otherwise update & rdquo; with mongoengine?

advertisements

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;

When I start my application I get: The ConnectionString property has not been initialized. Web.config: <connectionStrings> <add name="MyDB" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=mydatabase;User Id=myuse

how to repair & ldquo; WARNING: The terminal is not fully functional & rdquo; error in the control console?

This question already has an answer here: Why does git diff on Windows warn that the "terminal is not fully functional"? 7 answers I was using hg, now I am converting to Git. But after installing it, every time I do "git diff" or "

How to catch & ldquo; Sorry, this video can not be played & rdquo; error on VideoView

I have a VideoView and I am streaming videos from a remote server. Most of the times It would play the videos very smoothly. But sometimes, it displays an error message "Sorry, This video cannot be played". I have a hunch that this is more on th

How to solve & ldquo; The node '& hellip;' was not found. & rdquo; in SVN

I've merged two branches (trunk and a development branch). A folder has dissapeared. I'm working in command line and when trying to say svn resolve --accept working src/path/to/folder the terminal spits out The node 'src/path/to/folder' was not found

How to fix & ldquo; TypeError Uncaught: undefined is not a function & rdquo;

Here's the JSfiddle: http://jsfiddle.net/mostthingsweb/cRayJ/1/ I'll include my code and then explain what's wrong Here's my HTML header to show that everything should be linked: <head> <script src="http://ajax.googleapis.com/ajax/libs/jquer

How to create the insert function for a binary search tree constructed with a vector?

I am trying to build a binary search tree, however, it is vital for the algorithm that I am implementing to do so with a vector to diminish cache misses. My original idea was to adapt something similar to the heap insertion technique , since data pla

GWT / JSNI - & ldquo; DataCloneError - An object could not be cloned & rdquo; - How do I debug?

I am attempting to call out to parallels.js via JSNI. Parallels provides a nice API around web workers, and I wrote some lightweight wrapper code which provides a more convenient interface to workers from GWT than Elemental. However I'm getting an er

How to debug & ldquo; Parameter invalid passed to C runtime function & rdquo;

Background I've got about a terabyte of files with raw data, with a relatively small subset of labelled data. I've written c++ code (calling some ancient MSVC++2003 code I heavily modified to get it to compile on recent compilers) to aggregate the an

How to do & ldquo; Make & lt; Subdir & gt; & Lt; Target & gt; & Rdquo;

I would like a makefile where I can call: 'make' / 'make <subdir>' / 'make clean' / 'make <subdir> clean' But I do not want it to attempt to resolve clean when called on a subdir! Here's an example of my makefile: SUBDIRS := a b c all : @echo

How to check the file is there or not in javascript?

This question already has an answer here: How do I check if file exists in jQuery or JavaScript? 15 answers Can you please tell me how to check if a file exists or not in javascript? Actually I'm using Fileserver.js. When run, it creates a file named

How do you check & ldquo; otherwise null & rdquo; with Eloquent?

How do you check if a field is not null with Eloquent? I tried Model::where('sent_at', 'IS NOT', DB::raw('null'))->... but it gives IS NOT as a binding instead of a comparison. This is what DB::getQueryLog() says about it: 'query' => string 'select

The Java MVC project configuration gives & ldquo; The requested resource is not available. & Rdquo;

Update: The project works in tomcat, I just wasn't browsing to the right url. This seems to be a JBoss problem. I have been trying to follow the steps in this tutorial: http://www.mkyong.com/spring-security/spring-security-hello-world-annotation-exam

How can I just have the date and not the time displayed with it in a mySQL table?

I have a table containing records of the date and time each product was purchased. I need to export only the 'date' leaving out the time into another column. How do I do this?Does not select name, address, DATE(dateofbirth) from myfriends not work? e

How to perform & ldquo; Insert or Increment & rdquo; In a MongoDB query?

I want to write a query in MongoDB to insert a object in a document if it doesn't exist or increment a certain field of it if it exists in the specified document. There are two queries follows as an example: Documents Structures: Movies: { integer _i

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK