upsert

Mongodb upsert only update selected fields, but insert all

心不动则不痛 提交于 2019-11-29 12:37:35
问题 I am trying to use upsert in MongoDB to update a single field in a document if found OR insert a whole new document with lots of fields. The problem is that it appears to me that MongoDB either replaces every field or inserts a subset of fields in its upsert operation, i.e. it can not insert more fields than it actually wants to update. What I want to do is the following: I query for a single unique value If a document already exists, only a timestamp value (lets call it 'lastseen') is

How to upsert with mongodb-java-driver

◇◆丶佛笑我妖孽 提交于 2019-11-29 11:17:00
问题 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 "12" value. This code (js) add document with _id = "12" as expected db.metaclass.update( { _id:12}, { $set: {b:1} }, { upsert: true } ) mongo-java-driver-2.11.2 回答1: You cannot set _id if dbobject is just a document and does not contain an update

postgres syntax error at or near “ON”

一世执手 提交于 2019-11-29 11:01:13
I created this table: CREATE TABLE IF NOT EXISTS config_activity_log ( id serial primary key, activity_name varchar(100) NOT NULL, last_config_version varchar(50) NOT NULL, activity_status varchar(100) NOT NULL DEFAULT 'Awaiting for cofman', cofman_last_update bigint NOT NULL DEFAULT -1, is_error boolean DEFAULT FALSE, activity_timestamp timestamp DEFAULT current_timestamp ); I try to run this postgres script: INSERT INTO config_activity_log (activity_name, last_config_version, activity_status) VALUES ('test awating deployment','5837-2016-08-24_09-12-22', 'Awaiting for deployment') ON CONFLICT

Detect if the row was updated or inserted

亡梦爱人 提交于 2019-11-29 08:46:34
I am doing an INSERT with ON CONFLICT to postgre using java. Is there any way to find out if the executeUpdate inserted the row or updated it? Erwin Brandstetter You can look at the system column xmax to tell the difference. It's 0 for inserted rows in this case. CREATE TABLE tbl(id int PRIMARY KEY, col int); INSERT INTO tbl VALUES (1, 1); INSERT INTO tbl(id, col) VALUES (1,11), (2,22) ON CONFLICT (id) DO UPDATE SET col = EXCLUDED.col RETURNING *, (xmax = 0) AS inserted ; This is building on an undocumented implementation detail that might change in future releases (even if unlikely). It works

Get Id from a conditional INSERT

流过昼夜 提交于 2019-11-29 06:50:28
For a table like this one: CREATE TABLE Users( id SERIAL PRIMARY KEY, name TEXT UNIQUE ); What would be the correct one-query insert for the following operation: Given a user name , insert a new record and return the new id . But if the name already exists, just return the id . I am aware of the new syntax within PostgreSQL 9.5 for ON CONFLICT(column) DO UPDATE/NOTHING , but I can't figure out how, if at all, it can help, given that I need the id to be returned. It seems that RETURNING id and ON CONFLICT do not belong together. Erwin Brandstetter The UPSERT implementation is hugely complex to

How to correctly do upsert in postgres 9.5

谁都会走 提交于 2019-11-29 05:27:23
correct syntax of upsert with postgresql 9.5, below query shows column reference "gallery_id" is ambiguous error , why? var dbQuery = `INSERT INTO category_gallery ( category_id, gallery_id, create_date, create_by_user_id ) VALUES ($1, $2, $3, $4) ON CONFLICT (category_id) DO UPDATE SET category_id = $1, last_modified_date = $3, last_modified_by_user_id = $4 WHERE gallery_id = $2`; I tried change WHERE gallery_id = $2; to WHERE category_gallery.gallery_id = $2; then shows error there is no unique or exclusion constraint matching the ON CONFLICT specification , but I don't want to set gallery

Performing Insert OR Update (upsert) on sql server compact edition

十年热恋 提交于 2019-11-29 05:17:18
I have c# project that is using sqlserver compact edition and entity framework for data access. I have the need to insert or update a large amount of rows, 5000+ or more to the db, so if the key exists update the record if not insert it. I can not find a way to do this with compact edition and EF with out horrible performance, ie taking 2 mins plus on a core i7 computer. I have tried searching for the record to see if it exists then inserting if not or update if it does, the search is the killer on that. I have tried compiling the search query and that only gave a small improvement. Another

get mongodb _id object after upsert with php

試著忘記壹切 提交于 2019-11-28 20:49:00
is it possible to get the new/updated _id after the query? example code: $key = array( 'something' => 'unique' ); $data = array( '$inc' => array( 'someint' => 1 ) ); $mongodb->db->collection->update( $key, $data, array( 'upsert' => true ) ); $key is not holding the new/old _id object and i assume that $data will not either because its just an instruction. Yes -- It is possible using a single query. MongoDB includes a findAndModify command that can atomically modify a document and return it (by default it actually returns the document before it's been modified). The PHP drivers don't include a

UPSERT into table with dynamic table name

邮差的信 提交于 2019-11-28 13:09:42
Any better method to UPSERT into a table, provided : Data upsert at ~1 row/second Table Name is DYNAMIC, generated using ObjectID parameter passed to it THE FOLLOWING PROCEDURE THROWS : "ORA-00942: table or view does not exist" CREATE OR REPLACE PROCEDURE PROCEDURE "SPINSERTDATA" ( pObjectID IN RAW, pDateTime IN TIMESTAMP, pValue IN BINARY_DOUBLE, ) AS BEGIN Declare vQueryInsert VARCHAR2(1000); vQueryUpdate VARCHAR2(1000); vTableName VARCHAR2(30); Begin vTableName := FGETTABLENAME(POBJECTID => pObjectID); vQueryUpdate := 'UPDATE ' || vTableName || ' SET "VALUE" = :1'; vQueryInsert := 'INSERT

How to update and upsert multiple documents in MongoDB using C# Drivers

丶灬走出姿态 提交于 2019-11-28 10:56:25
I am using MongoDB 2, and I want to update multiple documents and upsert a value like processed:true into the collection. But MongoDB c# api only allows us to either Update Multiple Records or Upsert a single record. How to solve this problem using the C# api? After Mongo 2.6 you can do Bulk Updates/Upserts . Example below does bulk update using c# driver. MongoCollection<foo> collection = database.GetCollection<foo>(collectionName); var bulk = collection.InitializeUnorderedBulkOperation(); foreach (FooDoc fooDoc in fooDocsList) { var update = new UpdateDocument { {fooDoc.ToBsonDocument() } };