INSERT INTO "sip_data"."sip_ip_2m_data" (time_first,time_last,ip,ip_port,port)
VALUES('2019/4/6 15:14:29','2019/4/6 14:59:28','203.190.77.198','203.190.77.198|42560','42560')
ON conflict(ip_port)
DO UPDATE SET time_last = '2019/6/4 15:14:29'
alter table "sip_data"."sip_ip_2m_data" add constraint id_cons unique(ip_port);
参考 :https://blog.51cto.com/qingmiao/2164032?source=dra
Mongo 实现 实现插入 更新操作
> db.task2_NO1.update({"BBBB":"CCCC"},{$set:{"name"}:{"ghj"}})
2019-05-22T11:07:33.635+0800 E QUERY [thread1] SyntaxError: missing : after property id @(shell):1:49
> db.task2_NO1.update({"BBBB":"CCCC"},{$set:{"name":"ghj"}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
>
>
> db.task2_NO1.find()
{ "_id" : ObjectId("5ce4bb04b34fbed67418308d"), "BBBB" : "CCC" }
{ "_id" : ObjectId("5ce4bbdfb34fbed67418308e"), "BBBB" : "CCC" }
>
>
> db.task2_NO1.update({"BBBB":"CCC"},{$set:{"name":"ghj"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.task2_NO1.find()
{ "_id" : ObjectId("5ce4bb04b34fbed67418308d"), "BBBB" : "CCC", "name" : "ghj" }
{ "_id" : ObjectId("5ce4bbdfb34fbed67418308e"), "BBBB" : "CCC" }
>
>
> db.task2_NO1.update({"BBBB":"CCCC"},{$set:{"name":"ghj"}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
>
> db.task2_NO1.find()
{ "_id" : ObjectId("5ce4bb04b34fbed67418308d"), "BBBB" : "CCC", "name" : "ghj" }
{ "_id" : ObjectId("5ce4bbdfb34fbed67418308e"), "BBBB" : "CCC" }
>
> db.task2_NO1.update({"BBBB":"CCCC"},{$set:{"name":"ghj"}},{upsert:true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("5ce4bdef044269e910105dab")
})
>
> db.task2_NO1.find()
{ "_id" : ObjectId("5ce4bb04b34fbed67418308d"), "BBBB" : "CCC", "name" : "ghj" }
{ "_id" : ObjectId("5ce4bbdfb34fbed67418308e"), "BBBB" : "CCC" }
{ "_id" : ObjectId("5ce4bdef044269e910105dab"), "BBBB" : "CCCC", "name" : "ghj" }
>
db.task2_NO1.update({"BBBB":"CCCC"},{$set:{"name":"ghj"}},{upsert:true})
加上$set表示符合条件更新当前 document内部更新或添加
upsert为true表示没有符合的文档直接添加文档
谢谢华杰的补充!