Transferring content of one collection into a document in a different collection

Hi,
I want to ask for help with how to do this in MongoDB.

I created a collection named “resources” which contains the resources in my school like computers.
I have created the following collection.

[{
  "description": "PC-13",
  "comments": "",
  "parts": [
    {
      "id": "PARTS-23aq",
      "type": "mouse",
      "description": ""
    },
    {
      "id": "PARTS-18be",
      "type": "keyboard",
      "description": ""
    },
    {
      "id": "PARTS-53ih",
      "type": "monitor",
      "description": ""
    }
  ]
}, {
  "description": "PC-09",
  "comments": "",
  "parts": [
    {
      "id": "PARTS-31me",
      "type": "monitor",
      "description": ""
    },
    {
      "id": "PARTS-36hs",
      "type": "mouse",
      "description": ""
    },
    {
      "id": "PARTS-74bc",
      "type": "keyboard",
      "description": ""
    }
  ]
}, {
  "description": "PC-49",
  "comments": "",
  "parts": [
    {
      "id": "PARTS-48up",
      "type": "monitor",
      "description": ""
    },
    {
      "id": "PARTS-90hz",
      "type": "mouse",
      "description": ""
    },
    {
      "id": "PARTS-14zg",
      "type": "keyboard",
      "description": ""
    }
  ]
}
]

Now, I wanted to execute an “audit” where I would check the status of my resources.
So basically, in my application administrator would verify each computer resource part if they are still in order. If they are still working, in failure, etc.

I would like to create a document in a separate collection named “resources_audit” where I could transfer the content of one collection (resources) into another (resources_audit).
In that document, I would log the date of the audit, who did the audit, and the audit status for each resource.

Similar to the one below

{
	"_id": ObjectId("507f1f77bcf86cd799439011"),
	"audit_date": ISODate("2023-10-03T08:00:00.000Z"),
	"status": "IN-PROGRESS",
	"audit_notes": {
		"comments": "IN-PROGRESS",
		"auditor": "Jane Doe",
		"result": [{
			  "description": "PC-13",
			  "comments": "",
			  "audit_overall_status": "IN-PROGRESS",
			  "parts": [
				{
				  "id": "PARTS-23aq",
				  "type": "mouse",
				  "description": "",
				  "audit_status": "PASSED"
				},
				{
				  "id": "PARTS-18be",
				  "type": "keyboard",
				  "description": "",
				  "audit_status": "PASSED"
				},
				{
				  "id": "PARTS-53ih",
				  "type": "monitor",
				  "description": "",
				  "audit_status": "PASSED"
				}
			  ]
			}, {
			  "description": "PC-09",
			  "comments": "",
			  "parts": [
				{
				  "id": "PARTS-31me",
				  "type": "monitor",
				  "description": "",
				  "audit_status": "IN-PROGRESS"
				},
				{
				  "id": "PARTS-36hs",
				  "type": "mouse",
				  "description": "",
				  "audit_status": "FAILED"
				},
				{
				  "id": "PARTS-74bc",
				  "type": "keyboard",
				  "description": "",
				  "audit_status": "PASSED"
				}
			  ]
			}, {
			  "description": "PC-49",
			  "comments": "",
			  "parts": [
				{
				  "id": "PARTS-48up",
				  "type": "monitor",
				  "description": "",
				  "audit_status": "IN-PROGRESS"
				},
				{
				  "id": "PARTS-90hz",
				  "type": "mouse",
				  "description": "",
				  "audit_status": "IN-PROGRESS"
				},
				{
				  "id": "PARTS-14zg",
				  "type": "keyboard",
				  "description": "",
				  "audit_status": "IN-PROGRESS"
				}
			  ]
			}
			]
	}
}

Is what I am thinking feasible? Or how could this be done?

You could group, pushing all documents into a single field:

db.collection.aggregate([
  {
    $group: {
      _id: null,
      allItems: {
        $push: "$$ROOT"
      }
    }
  }
])

Obviously you can re-shape as needed before combining them. Combining multiple into one document, watch out for the document size limit of 16MB, but that’s still a lot of data you could fit in there.

1 Like

To add to @John_Sewell’s response there are two aggregation stages that can output to another collection they are $merge and $out and $merge

The main difference between the two is that $out will completely overwrite the output collection and $merge will allow you to merge the aggregation output into the target collection.

2 Likes

First of all, I would like to say thanks to both of you for introducing me to the aggregation framework.
I have only learned about it today and how powerful it could be.

I do have a question though that I cannot figure out how to do this. I have my playground here https://www.mongoplayground.net/p/cgE58Ys1kbD

I would like to know how can I insert a new field to the array that we have pushed?
The current output looks like this.

{
        "_id": ObjectId("5a934e000102030405000000"),
        "comments": "",
        "description": "PC-13",
        "parts": [
          {
            "description": "",
            "id": "PARTS-23aq",
            "type": "mouse"
          },
          {
            "description": "",
            "id": "PARTS-18be",
            "type": "keyboard"
          },
          {
            "description": "",
            "id": "PARTS-53ih",
            "type": "monitor"
          }
        ]
      },

But I wanted to add a default field like below and set it to FAILED.

{
	"_id": ObjectId("5a934e000102030405000000"),
	"comments": "",
	"description": "PC-13",
	"parts": [
		{
			"description": "",
			"id": "PARTS-23aq",
			"type": "mouse",
			"audit_status": "FAILED"
		},
		{
			"description": "",
			"id": "PARTS-18be",
			"type": "keyboard",
			"audit_status": "FAILED"
		},
		{
			"description": "",
			"id": "PARTS-53ih",
			"type": "monitor",
			"audit_status": "FAILED"
		}
	]
 }

Not sure if this is feasible?

Thank you.

You can use the dot notation:

  {
    "$addFields": {
      "overall_status": "IN-PROGRESS",
      "auditor": "Jane Doe",
      "audit_notes.parts.result": "FAILED"
    }
  },

The aggregation framework is an awesome tool to slice and dice data, update it or all kinds of things!

If you’ve not done so already, check out the MongoDB university courses, i.e.

1 Like

Hey,
I was thinking along the lines about doing something about the

db.collection.aggregate([
  {
    $group: {
      _id: null,
      allItems: {
        $push: "$$ROOT"
      }
    }
  }
])

But alas, I am overthinking things and I just needed the dot notation on the $addfields

Thank you very much for your help and will take some of the MongoDB courses to learn more about MongoDB.

Also check out:

Which is also available in print. Lucky me received a free copy recently.

That from the mongo local event?

Yes it was. Great event.

I was in the London event but did hang about long enough to get a copy!

Getting off topic!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.