How can I get the fields from another mongodb collection and add those field values as an array in the current collection?

NeedDevHelp

For example, I have two collections with the following data:

Collection 1:

{
code: 1,
name: "data1"
},
{
code: 2,
name: "data2"
}

Collection 2:

{
code: 1,
type: "type1"
},
{
code: 1,
type: "type2"
},
{
code: 2,
type: "type2"
},

I want the output of the aggregation to be as follows:

{
code:1,
name: "data1",
types: ["type1","type2"]
},
{
code:2,
name:"data2",
types:["type2"]
}
turivishal
  • $lookup to to join collection, pass code as localField and foreignField
  • $addFields to get only type field from collection2
db.col1.aggregate([
  {
    "$lookup": {
      "from": "col2",
      "localField": "code",
      "foreignField": "code",
      "as": "type"
    }
  },
  { $addFields: { type: "$type.type" } }
])

Playground

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

mongodb - get top items from a collection based on its usage count as a field in another collection

how can i access nested array from mongoDB collection using spring boot?

How to filter a MongoDB collection using information from another collection?

How can I replace values in a Python NumPy array with the index of those values found in another array?

How do I get specific values from a collection map

Cakephp collection how can I get array index?

Putting array of collection inside another collection in mongodb

How can i get all the user documents from a collection in flutter

How to Get an Aggregate from a MongoDB Collection

How do I filter a MongoDB collection using an OR for the values of a field using the NodeJS driver?

Rename a field from an array for all documents of collection - MongoDB

Can I add A Firebase collection in a collection

How to get a collection that is in a document of another collection in firebase

Meteor + React: Can't get data from mongoDB Collection

How to get mongodb collection iterator?

How can i select values from 2d array and add to another 2d array python

How to add String Array and Integer Array from XML into one Collection

MongoDB: how can I split a collection in n collections?

How can I get multiple elements from an array in MongoDB?

How to get documents from a collection using the $nin operator in mongodb

How to count instances of specific field in a mongodb collection

How can I add a document to a Firebase collection inside a transaction?

How can I use a macro to create an array of function names starting from a collection of function definitions?

How can I iterate over each record in collection while also manipulating another collection?

How to Get Array Field Values From Firestore

How can I use Java Stream in order to iterate on a collection on object and use a specific string field of each object to create a new array?

How can I sort this Collection using an array order?

How to copy field and value within a document to another document in a different collection in mongodb

MongoDB > extract collection from nested array

TOP lista

quentelabel

Arquivo