I am a Mongodb beginner. I have a collection where I want to find and pull an item from one document and push it to another matched document in the same collection.
Given: Move the item with orderNumber: 100 and itemId: I000007 to orderNumber: 102 items array
Below is the orders collection.
/* 1 */
{
"_id" : ObjectId("61264e2dc0e8b5b3daa16c9a"),
"orderNumber" : "100",
"__v" : 0,
"items" : [
{
"itemId" : "I000005",
"region" : [
"US",
"EUROPE"
]
},
{
"itemId" : "I000006",
"region" : []
},
{
"itemId" : "I000007",
"region" : [
"JAPAN"
]
}
]
}
/* 2 */
{
"_id" : ObjectId("61268283c0e8b5b3daa1f245"),
"orderNumber" : "101",
"__v" : 0,
"items" : [
{
"itemId" : "I000008",
"region" : []
}
]
}
/* 3 */
{
"_id" : ObjectId("61268283c0e8b5b3daa1fg3c"),
"orderNumber" : "102",
"__v" : 0,
"items" : [
{
"itemId" : "I000009",
"region" : []
}
]
}
After performing the query, I want the below result.
/* 1 */
{
"_id" : ObjectId("61264e2dc0e8b5b3daa16c9a"),
"orderNumber" : "100",
"__v" : 0,
"items" : [
{
"itemId" : "I000005",
"region" : [
"AMRS",
"APAC"
]
},
{
"itemId" : "I000006",
"region" : []
}
]
}
/* 2 */
{
"_id" : ObjectId("61268283c0e8b5b3daa1f245"),
"orderNumber" : "101",
"__v" : 0,
"items" : [
{
"itemId" : "I000008",
"region" : []
}
]
}
/* 3 */
{
"_id" : ObjectId("61268283c0e8b5b3daa1fg3c"),
"orderNumber" : "102",
"__v" : 0,
"items" : [
{
"itemId" : "I000009",
"region" : [],
},
{
"itemId" : "I000007",
"region" : [
"JAPAN"
]
}
]
}