Mongo DB에서 저장과 삽입의 차이점은 무엇입니까?
Mongo DB에서 저장과 삽입의 차이점은 무엇입니까? 둘 다 똑같아 보인다
db.users.save({username:"google",password:"google123"})
db.users.insert({username:"google",password:"google123"})
Vs 삽입 저장 :
주어진 예에서, 행동은 본질적으로 동일합니다.
save
"_id"매개 변수와 함께 전달되면 다르게 작동합니다.
저장을 위해 문서에가 포함되어 있으면 필드 _id
에서 컬렉션에 대한 쿼리를 업스 퍼링하고 _id
, 그렇지 않은 경우 삽입합니다.
지정된 _id 값을 가진 문서가 없으면 save () 메소드는 문서의 지정된 필드를 사용하여 삽입을 수행합니다.
지정된 _id 값을 가진 문서가 있으면 save () 메서드가 업데이트를 수행하여 기존 레코드의 모든 필드를 문서의 필드로 바꿉니다.
저장 대 업데이트 :
update
쿼리 매개 변수와 일치하는 기존 문서를 수정합니다. 일치하는 문서가 없으면 바로 그 시점 upsert
입니다.
upsert : false
: 해당 문서가 없으면 아무 일도 일어나지 않습니다upsert : true
: 쿼리 매개 변수 및 업데이트 매개 변수와 동일한 내용으로 새 문서가 작성됩니다.
save
: 검색어 매개 변수를 허용하지 않습니다. _id
존재하고 동일한 문서와 일치하는 문서가 있으면 이를 _id
대체합니다. _id가 지정되지 않거나 일치하는 문서가 없으면 문서를 새 문서로 삽입합니다.
저장을 위해 두 가지 경우를 고려하십시오.
1) doc에 _id가 있습니다.
2) doc에 _id가 없습니다.
Save ()
/ \
/ \
Having _id Not Having _id
->In this case save will do -> It will do normal insertion
upsert to insert.Now in this case as insert() do.
what that means, it means
take the document and replace
the complete document having same
_id.
삽입에 대한 두 가지 경우를 고려해 보겠습니다.
1) doc의 _id를 수집합니다.
2) _id of doc이 컬렉션에 없습니다.
Insert()
/ \
/ \
Doc Having _id in collection Doc Not Having _id
-> E11000 duplicate key ->Insert a new doc inside the collection.
error index:
save
문서를 삽입하거나 업데이트하십시오.
insert
삽입 만합니다.
그러나 귀하의 경우 저장에 제공된 문서에 _id
필드 가 없으므로 동일한 작업을 수행합니다 .
예를 들어서
사과 저장
db.fruit.save({"name":"apple", "color":"red","shape":"round"})
WriteResult({ "nInserted" : 1 })
db.fruit.find();
{
"_id" : ObjectId("53fa1809132c1f084b005cd0"),
"color" : "red",
"shape" : "round",
"name" : "apple"
}
이전에 저장된 사과의 _id로 사과 저장
db.fruit.save(
{"_id" : ObjectId("53fa1809132c1f084b005cd0"),"name":"apple",
"color":"real red","shape":"round"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
우리가 저장 한 사과는 빨간색에서 실제 빨간색으로 색상이 업데이트되었습니다.
db.fruit.find();
{
"_id" : ObjectId("53fa1809132c1f084b005cd0"),
"color" : "real red",
"shape" : "round",
"name" : "apple"
}
_id로 사과 저장
db.fruit.save({"_id" : ObjectId("55551809132c1f084b005cd0"),
"name":"apple", "color":"real red","shape":"round"})
WriteResult({ "nMatched" : 0, "nUpserted" : 1,
"nModified" : 0, "_id": 55551809132c1f084b005cd0 })
동일한 개체 ID를 가진 사과가 없어서 Apple이 삽입되었습니다.
오렌지 삽입
db.fruit.insert({"name":"orange", "color":"orange","shape":"round"})
WriteResult({ "nInserted" : 1 })
주황색 삽입
db.fruit.find();
{
"_id" : ObjectId("53fa1809132c1f084b005cd0"),
"color" : "real red",
"shape" : "round",
"name" : "apple"
}
{
"_id" : ObjectId("53fa196d132c1f084b005cd7"),
"color" : "orange",
"shape" : "round",
"name" : "orange"
}
{
"_id" : ObjectId("55551809132c1f084b005cd0"),
"color" : "real red",
"shape" : "round",
"name" : "apple"
}
따라서 객체 ID와 함께 제공된 경우 save는 업데이트와 같은 역할을합니다. 객체 ID가 이미 다른 방법으로 삽입되어 있으면 삽입됩니다.
동일한 콜렉션에서 이전에 사용 된 ID로 "삽입"을 사용하려고하면 중복 키 오류가 발생합니다. 이미 동일한 컬렉션에있는 ID로 "저장"을 사용하면 업데이트 / 덮어 쓰기됩니다.
If you are looking to do a true update I would suggest using "update". Update will not overwrite in the way Save would if you are Saving using the same ID that is already in the collection.
For example you have two fields "x" and "y" and you want to keep both but change the value of "x". If you chose the "save" command and did not include y with the previous value or not have y at all in your save, then y would no longer have the same value or be there. However if you chose to update using $set and only had x included in your update statement, you would not affect y.
As you can see here, the save method will essentially do an upsert (update if it finds the doc, insert otherwise):
http://docs.mongodb.org/manual/reference/method/db.collection.save/#db.collection.save
Insert is just that, a straight insert.
Consider the below document
{ "_id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" }
if db already contains the document with _id:1, then
save operation will throw the exception like below
E11000 duplicate key error index ...........
and where as insert operation , will just override the document.
In terms of ORACLE: mongo insert => Oracle insert mongo save => Oracle merge
db.<collection_name>.save(<Document>)
is equivalent to InsertOrUpdate Query.
While, db.<collection_name>.insert(<Document>)
is equivalent to just Insert Query.
'programing tip' 카테고리의 다른 글
WPF 바인딩을 사용하여 두 개의 명령 매개 변수 전달 (0) | 2020.06.19 |
---|---|
Python : TypeError : 'str'및 'int'개체를 연결할 수 없습니다. (0) | 2020.06.19 |
포인터 설명을 가리키는 포인터 (0) | 2020.06.19 |
고품질 이미지 스케일링 라이브러리 (0) | 2020.06.19 |
배치 파일을 기다리지 않고 응용 프로그램을 시작하는 방법은 무엇입니까? (0) | 2020.06.19 |