How to Update Nested Array Subdocuments Using Main Document Field with Mongoose

Updating nested array subdocuments based on a field in the main document is a common task when working with MongoDB and Mongoose. Here’s a simple guide and example to help you accomplish this. Plan Use Model.updateMany() or Model.findOneAndUpdate() to update multiple or a single document. Use an aggregation pipeline ([{}]) as the second argument to reference document fields. Use the $set stage within the pipeline to update the nested array. Use the $$ROOT variable to reference fields from the main document. Example Assume you have a User model where each user has an array of posts, and you want to set each post’s authorName field based on the user’s name field. ...

How to update a specified item in a nested array with Mongoose

In this blog post, we’ll go over how to update a specified item in a nested array inside a Mongoose document. ##Setting up the environment First, let’s set up the environment by creating a Mongoose schema and model. Here’s an example schema that has a nested array of items: const mongoose = require('mongoose'); const schema = new mongoose.Schema({ items: [{ name: String, quantity: Number }] }); const Model = mongoose.model('Model', schema); ##Updating a specified item in a nested array ...

How to merge a nested list in Mongo document with another found in adjacent collection

Let’s consider a situation where you have two collections, houses and people. Each house has a collection of key holders, which link to the persons collection with their IDs. Key holders list also holds information when the key was given for the identified person. In bson, the situation in the database looks like this: { "houses": [ { "_id": ObjectId("5fba17c1c4566e57fafdcd7e"), "address": "Main street 1", "keyHolders": [ { "keyDelivered": "2022-02-02T02:02:02", "personId": "5fbb5ab778045a985690b5fc" }, { "keyDelivered": "2021-01-01T01:01:01", "personId": "5fbb5ab778045a985690b5fd" } ] }, { "_id": ObjectId("5fba17c1c4566e57fafdcd7f"), "address": "Broadway 3", "keyHolders": [ { "keyDelivered": "1993-03-03T03:03:03", "personId": "5fbb5ab778045a985690b5fc" } ] } ], "persons": [ { "_id": ObjectId("5fbb5ab778045a985690b5fc"), "name": "Jack Bauer", }, { "_id": ObjectId("5fbb5ab778045a985690b5fd"), "name": "James Bond", } ] } You can also do some mapping for the source list, for instance convert foreign keys from strings to ObjectIds. ...

How to Copy a Collection of Documents to Another Database with mongosh

If you need to copy a collection of documents from one Mongo collection to another, you can use the mongodump and mongorestore. But if the source database is in the same instance as the target, then you may be able to take a shortcut with mongosh. Here is the script to use: db.<source collection>.find().forEach(function(d){ db.getSiblingDB('<dest db>')['<dest collection>'].insert(d); }); For example, if you are copying all documents from current databases cars collection to a database called another-db’s collection vehicles: ...