2

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"
            ]
        }
    ]
}

1 Answer 1

1

You have to do two queries to perform your operation,

0) Pre required things:

let pullOrderNumber = "100";
let itemId = "I000007";
let pushOrderNumber = "102";

1) pull the item from items array using condition:

  • the query will do the operation but will return an old document because we have used findOneAndUpdate method, and we need that item to push in another document
let oldDoc = await Model.findOneAndUpdate(
  { orderNumber: pullOrderNumber },
  {
    $pull: {
      items: { itemId: itemId }
    }
  }
);

Playground

2) Filter and find the removed item object from the above result document

let pushItem = oldDoc.items.find(i => i.itemId == itemId);

3) Push an above pulled item into another document

await Model.updateOne(
  { orderNumber: pushOrderNumber },
  {
    $push: {
      items: pushItem
    }
  }
);

Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.