upsert

MongoDB Collection update: initialize a document with default values

为君一笑 提交于 2019-12-04 06:29:34
I am trying to deal with time series using MongoDB. The common solution adopted by community is to use subdocuments to store information at different level of granularity (see Schema Design for Time Series Data in MongoDB ). For example, take a look at the following document: { timestamp_minute: ISODate("2013-10-10T23:06:00.000Z"), type: “memory_used”, values: [ 999999, // 1 second … 1000000, // nth second 1500000, // n+1th second … 2000000 // 60th ] } The document is indexed by minute information and contains a subdocument which store more detailed information for each second. So far so good.

IF-Statement in SQLite: update or insert?

走远了吗. 提交于 2019-12-04 06:18:26
I Can't run this query with SQLite if 0<(select COUNT(*) from Repetition where (Word='behnam' and Topic='mine')) begin update Repetition set Counts=1+ (select Counts from Repetition where (Word='behnam' and Topic='mine')) end else begin insert Repetition(Word,Topic,Counts)values('behnam','mine',1) end It says "Syntax error near IF" How can I solve the problem jklemmack SQLite does not have an IF statement ( see the list of supported queries ) Insetad, check out out ERIC B 's suggestion on another thread . You're effectively looking at doing an UPSERT (UPdate if the record exists, INSERT if not

How to mimic upsert behavior using Hibernate?

ⅰ亾dé卋堺 提交于 2019-12-04 02:57:58
问题 I'm writing an application that sync's entities from a third party datasource into our own schema, with a transformation/mapping step in between. I'm using Hibernate to represent and persist the entities in our own schema. A problem I'm running into is that I have a unique multi-column key on one of my tables. The behavior I would like to see is analogous to an upsert: when Hibernate goes to persist an entity and detects a unique constraint violation, it does an update instead. We are using

Upsert (update or insert) in Sybase ASE?

谁说胖子不能爱 提交于 2019-12-04 02:10:17
I'm writing an application to move data from Oracle to Sybase and need to perform update / insert operations. In Oracle, I'd use MERGE INTO, but it doesn't seem to be available in Sybase (not in ASE, anyway). I know this can be done with multiple statements, but for a couple of reasons, I'm really trying to get this into a single statement. Any suggestions? Ritter ASE 15.7 has this feature. Find the docs here: http://infocenter.sybase.com/help/topic/com.sybase.infocenter.dc36272.1570/html/commands/commands84.htm unfortunately, it is impossible to insert and update a table in one statement

Postgresql - Clean way to insert records if they don't exist, update if they do

天涯浪子 提交于 2019-12-03 21:05:51
问题 Here's my situation. I have a table with a bunch of URLs and crawl dates associated with them. When my program processes a URL, I want to INSERT a new row with a crawl date. If the URL already exists, I want to update the crawl date to the current datetime. With MS SQL or Oracle I'd probably use a MERGE command for this. With mySQL I'd probably use the ON DUPLICATE KEY UPDATE syntax. I could do multiple queries in my program, which may or may not be thread safe. I could write a SQL function

Postgres UPSERT (INSERT or UPDATE) only if value is different

青春壹個敷衍的年華 提交于 2019-12-03 12:49:50
问题 I'm updating a Postgres 8.4 database (from C# code) and the basic task is simple enough: either UPDATE an existing row or INSERT a new one if one doesn't exist yet. Normally I would do this: UPDATE my_table SET value1 = :newvalue1, ..., updated_time = now(), updated_username = 'evgeny' WHERE criteria1 = :criteria1 AND criteria2 = :criteria2 and if 0 rows were affected then do an INSERT: INSERT INTO my_table(criteria1, criteria2, value1, ...) VALUES (:criteria1, :criteria2, :newvalue1, ...)

Mongodb - duplicate fields in $set and $setOnInsert

安稳与你 提交于 2019-12-03 12:44:47
问题 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 used when a document is inserted, and the latter is used when the document is updated. 回答1: $set operator is used on upsert too. So it's nonsense to refer same fields both on $set and $setOnInsert . Just try this on an empty collection: db.items

Upsert in Rails ActiveRecord

不羁的心 提交于 2019-12-03 09:26:15
Does ActiveRecord have a built-in upsert functionality? I know I could write it myself but I obviously don't want to if such a thing already exists. Model.find_or_initialize likely does what you want. You can chain it with save or update_attributes if that makes sense. More info in the Rails Guides . I just ran across this library: https://github.com/seamusabshere/upsert I haven't tested it yet, but it looks promising There is an awesome new feature in rails 6: they added upsert and upsert_all to ActiveRecord More info can be found here https://edgeapi.rubyonrails.org/classes/ActiveRecord

UPSERT in PostgreSQL using jOOQ

扶醉桌前 提交于 2019-12-03 08:10:50
问题 I am trying to perform an UPSERT in PostgreSQL using the jOOQ library. For doing this I am currently trying to implement the following SQL statement in jOOQ: https://stackoverflow.com/a/6527838 My code looks like this so far: public class UpsertExecutor { private static final Logger logger = LoggerFactory.getLogger(UpsertExecutor.class); private final JOOQContextProvider jooqProvider; @Inject public UpsertExecutor(JOOQContextProvider jooqProvider) { Preconditions.checkNotNull(jooqProvider);

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

随声附和 提交于 2019-12-03 06:32:34
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 inserted How many rows were updated because existing How many rows could not be inserted because of constraints If the constraint is not respected for the last row, will the previous inserted/updated rows be persisted in the DB? I do not know how else you can understand what event occurred. You should look at the value of xmax, if xmax = 0 means there row was