이를 위해 UPDATE() 메서드를 사용하고 그 내에서 두 가지 조건을 설정합니다. 문서로 컬렉션을 만들자 −
> db.demo605.insertOne(
... {
... _id:1,
... "Information" : [
... {
... "id" : "Product-1",
... "Quantity" : 50,
... },
... {
... "id" : "Product-2",
... "Quantity" : 100,
... },
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : 1 }
>
>
> db.demo605.insertOne(
... {
... _id:2,
... "Information" : [
... {
... "id" : "Product-1",
... "Quantity" : 30,
... },
... {
... "id" : "Product-2",
... "Quantity" : 40,
... },
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : 2 } find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo605.find();
그러면 다음과 같은 출력 &mnus;
가 생성됩니다.{ "_id" : 1, "Information" : [
{ "id" : "Product-1", "Quantity" : 50 },
{ "id" : "Product-2", "Quantity" : 100 }
] }
{ "_id" : 2, "Information" : [
{ "id" : "Product-1", "Quantity" : 30 },
{ "id" : "Product-2", "Quantity" : 40 }
] } 다음은 두 가지 조건에 따라 MongoDB에서 수량을 업데이트하는 쿼리입니다 -
>db.demo605.update({_id:1,"Information.id":"Product1"},
{$set:{"Information.$.Quantity":1000}}
);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo605.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : 1,
"Information" : [
{
"id" : "Product-1",
"Quantity" : 1000
},
{
"id" : "Product-2",
"Quantity" : 100
}
]
}
{
"_id" : 2,
"Information" : [
{
"id" : "Product-1",
"Quantity" : 30
},
{
"id" : "Product-2",
"Quantity" : 40
}
]
}