1

I have two collection

Products

    "_id" : "1",
    "Product" : "Phone",
    "Godown" :"Godown1",
    "Info" : [
            {
                    "Type":"iphone",
                    "Quantity" : "10",
                    "Price" : "30000"
            },
            {
                    "Type":"Samsung",
                    "Quantity" : "10",
                    "Price" : "30000"
            }
            ]})

and Sales

"_id" : "5551",
"Product" : "Phone",
"Godown" :"Godown1",        
"Type":"iphone",
"Quantity" : "10",
"Price" : "30000"

If i want merge this collection into one and store it in a variable is it possible?

Community
  • 1
  • 1
Harsh Makadia
  • 3,263
  • 5
  • 30
  • 42

2 Answers2

2

Usually, this is not necessary, but a problem in data modeling. Ask yourself what you really want to achieve. Usually, querying related data boils down to

For a given X, what is the related Y?

As per your example:

For "Info.Type" == iphone, what is the related sales document?

Which in shell terms would be easy to look up with

db.sales.find({"Type":"iphone"})

Now that you have the Products document and the related Sales document available, you can merge them within your code for delivery to the client, for example. One of my MongoDB mantras

Keep the logic where it belongs to – your code.

Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
1

You can use MapReduce: https://docs.mongodb.org/manual/core/map-reduce/#MapReduce-Outputoptions

For details see this answer: MongoDB: Combine data from multiple collections into one..how?

To access raw collection/db use rawCollection() on Mongo.Collection - http://info.meteor.com/blog/meteor-104-mongo-cordova-template-subscriptions

Community
  • 1
  • 1
MatiK
  • 573
  • 1
  • 7
  • 21