Questions tagged [upsert]

UPSERT is a combination of UPDATE and INSERT, typically used in relational databases.

UPSERT refers to types of statements (usually database statements) that INSERT an item (record) to a resource (table in a database) where, if the item already exists, it will update the existing item with the fields provided. The term UPSERT is an amalgamation of UPDATE and INSERT and is common slang among database developers.

1227 questions
23
votes
13 answers

MongoTemplate upsert - easy way to make Update from pojo (which user has editted)?

Here is a simple pojo: public class Description { private String code; private String name; private String norwegian; private String english; } And please see the following code to apply an upsert to MongoDb via spring…
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
23
votes
4 answers

How to upsert with mongodb-java-driver

How can I upsert data into mongodb collection with java-driver? I try (with empty collection): db.getCollection(collection).update(new BasicDBObject("_id", "12"), dbobject, true, false); But document was created with _id == ObjectID(...). Not with…
user1312837
  • 1,268
  • 1
  • 11
  • 29
22
votes
1 answer

Postgres 9.5+: UPSERT to return the count of updated and inserted rows

I get the canonical example: INSERT INTO user_logins (username, logins) VALUES ('Naomi',1),('James',1) ON CONFLICT (username) DO UPDATE SET logins = user_logins.logins + EXCLUDED.logins; But now I also need to know: How many rows were…
sscarduzio
  • 5,938
  • 5
  • 42
  • 54
22
votes
4 answers

Check if record exists, if yes "update" if not "insert"

I want to check table PREMIUM_SERVICE_USER if any records exists for strClientID update timeValid for +30 if no records for strClientID insert to premium_service_user table. What am I doing wrong? It increases timeValid for +30 days but inserts…
Ali Demirci
  • 5,302
  • 7
  • 39
  • 66
22
votes
2 answers

MySQL behavior of ON DUPLICATE KEY UPDATE for multiple UNIQUE fields

From MySQL 4.1.0 onwards, it is possible to add ON DUPLICATE KEY UPDATE statement to specify behavior when values inserted (with INSERT or SET or VALUES) are already in destination table w.r.t. PRIMARY KEY or some UNIQUE field. If value for PRIMARY…
kiriloff
  • 25,609
  • 37
  • 148
  • 229
21
votes
2 answers

postgresql ON CONFLICT ON CONSTRAINT for 2 constraints

In am currently working with PostgreSQL 9.5 and was wondering if the is a possibility to include names of 2 constraints in the ON CONFLICT ON CONSTRAINT statement. My sql is below INSERT INTO LIVE.TABLE (column1, column2, column3) SELECT DISTINCT ON…
Sky21.86
  • 627
  • 2
  • 9
  • 26
21
votes
2 answers

how to use python Elasticsearch client upsert api

I'm using Elasticsearch python client as http://elasticsearch-py.readthedocs.org/ I tried hard but still could not find the update api with upsert. Could anyone give me an example with ES python client upsert api please.
Jack
  • 5,540
  • 13
  • 65
  • 113
21
votes
2 answers

bulk upserts within a sql transaction in golang

I've been messing around with golang's sql package with transactions, and I'm trying to understand how to do bulk upserts without the "per insert" round trip communication for each row. The examples here don't really show how any bulk queries would…
tbischel
  • 6,337
  • 11
  • 51
  • 73
21
votes
3 answers

What is USING in SQL Server 2008 MERGE syntax?

Jacob asked the perfect question: give me the MERGE syntax. Every answer out there immediately jumps to the most complicated case they can think of; obscuring the syntax with extraneous confusion. Marc gave an answer: MERGE member_topic AS…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
20
votes
3 answers

Is it possible to upsert nested fields in DynamoDB?

I would like to 'upsert' a document in DynamoDB. That is, I would like to specify a key, and a set of field/value pairs. If no document exists with that key, I want one created with that key and the key/value pairs I specified. If a document…
pkaeding
  • 36,513
  • 30
  • 103
  • 141
19
votes
2 answers

Mongodb - duplicate fields in $set and $setOnInsert

In this post, the accepted answer explains that you cannot have the same fields under $set and $setOnInsert in an upsert operation. Can someone explain why this is? It seems like the $setOnInsert shouldn't conflict with $set, since the former is…
jtmarmon
  • 5,727
  • 7
  • 28
  • 45
19
votes
2 answers

AddOrUpdate works not as expected and produces duplicates

I'm using Code-First DBContext-based EF5 setup. In DbMigrationsConfiguration.Seed I'm trying to fill DB with default dummy data. To accomplish this task, I use DbSet.AddOrUpdate method. The simplest code to illustrate my aim: j = 0; var cities =…
berezovskyi
  • 3,133
  • 2
  • 25
  • 30
18
votes
7 answers

MySQL "good" way to insert a row if not found, or update it if it is found

Very often, I want to run a query on one of my users where I want a row stored and associated with that user, in a 1-to-1 relationship. So let's say (this is just an arbitrary example), that I have a table that keeps track of a user's car, along…
DivideByHero
  • 19,715
  • 24
  • 57
  • 64
18
votes
1 answer

PostgreSQL Partial Indexes and UPSERT

After googling a lot my question is described below: CREATE TABLE security ( id SERIAL PRIMARY KEY, vendor VARCHAR(20), external_id VARCHAR(20), extinct BOOLEAN DEFAULT FALSE ); CREATE UNIQUE INDEX unique_vendor ON…
max.kuzmentsov
  • 766
  • 1
  • 10
  • 22
18
votes
1 answer

Node.js MongoDB Upsert update

I'm writing a little application which scores keywords. So if "beirut" and "education" get entered in, if they haven't been seen before, I want to create a mongo entry, and give them a score of 1. If they have, I want to increment their score by…
mradfo21
  • 195
  • 1
  • 1
  • 5