Aggregation in mongo - mongodb

Below is a document from my database:
{
"_id" : ObjectId("58635ac32c9592064471cf5b"),
"agency_code" : "v5global",
"client_code" : "whirlpool",
"project_code" : "whirlpool",
"date" : {
"datetime" : 1464739200000.0,
"date" : 1464739200000.0,
"datejs" : ISODate("2016-06-01T00:00:00.000+0000"),
"datetimejs" : ISODate("2016-06-01T00:00:00.000+0000"),
"month" : NumberInt(5),
"year" : NumberInt(2016),
"day" : NumberInt(1)
},
"user" : {
"promoter_id" : NumberInt(19),
"promoter_name" : "Hira Singh Pawar",
"empcode" : "519230"
},
"counter" : {
"store_id" : NumberInt(4),
"store_name" : "Maya Sales ",
"chain_type" : "BS",
"address" : "6 Filamingo Market , Hissar",
"city" : "Hissar",
"state" : "Faridabad",
"region" : "North",
"sap_code" : "N_Far_91103948_1",
"unique_tp_code" : "91103948",
"location" : "6"
},
"insertedon" : {
"date" : 1464739200000.0,
"datejs" : ISODate("2016-06-01T00:00:00.000+0000"),
"datetimejs" : ISODate("2016-06-01T00:00:00.000+0000")
},
"insertedby" : "akshay",
"manager" : {
"manager_id" : NumberInt(5943),
"manager_name" : "Sonu Singh"
},
"type" : "display",
"data" : {
"brand" : "whirlpool",
"sku" : "60",
"model_name" : "Icemagic Fresh",
"sub_cat_name" : "DC",
"cat_name" : "Refrigerator",
"value" : NumberInt(1)
},
"IsDeleted" : false
}
I want to apply aggregation where I have to group it with city, state and region and if that counter has sold refrigerator I need that details in my result e.g if a counter has sold 2 refrigerators of whirlpool company then I want that to reflect in my result.
A counter can also sell other things like washing machines etc. So if they have sold 2 washing machines I want a result with { washingMachine: 2 }.
I have tried everything and nothing seems to be working here:
db.display_mop.aggregate( // Pipeline [
// Stage 1
{ $match: { "project_code":"whirlpool" } },
// Stage 2
{
$group: {
_id: {
"userid": "$user.promoter_id",
"userName": "$user.promoter_name",
"usercode": "$user.empcode",
"storename": "$counter.store_name",
"address": "$counter.address",
"city": "$counter.city",
"state": "$counter.state",
"region": "$counter.region"
}
}
},
],
// Options
{ allowDiskUse: true }

Related

Should I choose mongo aggregation or should do it at application level service

I have the below collections.
Company:
{
"_id" : ObjectId("5a7848e8ca70273218e9d743"),
"name" : "Google",
"departments" : [
{
"name" : "IT",
"_id" : "1234567890"
},
{
"name" : "Sales",
"_id" : "1234567891"
}
]
}
User:
{
"_id" : ObjectId("5a784977ca70273218e9d759"),
"name" : "Sankarshan",
"company" : ObjectId("5a7848e8ca70273218e9d743"),
"department" : "1234567890"
}
topicCategories
{
"_id" : ObjectId("5a784a10ca70273218e9d76f"),
"name" : "topicCtegoryOne",
"order" : 1
}
topics
{
"_id" : ObjectId("5a784cc3ca70273218e9d7d3"),
"topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
"name" : "TopicOne",
"order" : 1,
"label" : "oneTopic",
"color" : "red"
}
dimentions
{
"_id" : ObjectId("5a784fdcca70273218e9d869"),
"name" : "dimentionOne"
}
queries
{
"_id" : ObjectId("5a78519aca70273218e9d8d7"),
"topic" : ObjectId("5a784cc3ca70273218e9d7d3"),
"dimention" : ObjectId("5a784fdcca70273218e9d869"),
"order" : 1,
"label" : "queryLabelOne",
"statement" : "This is one question - Top1"
}
User_responses
{
"_id" : ObjectId("5a7859f5ca70273218e9da7d"),
"user" : ObjectId("5a784977ca70273218e9d759"),
"company" : ObjectId("5a7848e8ca70273218e9d743"),
"department" : "1234567890",
"response" : {
"question" : ObjectId("5a78519aca70273218e9d8d7"),
"topic" : ObjectId("5a784cc3ca70273218e9d7d3"),
"topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
"dimention" : ObjectId("5a784fdcca70273218e9d869"),
"rating" : 5,
"color" : "red"
}
}
What is happening above
Each User belongs to a company.
Each Topic belongs to a Topic category.
Each Query belongs to a Topic
Each query has a dimention.
A user can answer a Query in terms of rating.
The user response to each question will be stored in User_responses.
What I want to achieve
An aggregated result of responses based on Topic category and Dimension
Expected aggregated result format
The query is based on one topic category.
All questions on each topic is aggregated based on Topic
Each Topic will have calculated average ratings based on dimension.
Response
{
"topicCategory": "topic category",
"topics": [
{
"topicLabel": "label of the topic one",
"dimentions": [
{
"name": "dimention name one",
"avgValue": 5.6
}
{
"name": "dimention name two",
"avgValue": 4.5
}
]
},
{
"topicLabel": "label of the topic two",
"dimentions": [
{
"name": "dimention name one",
"avgValue": 6.6
}
{
"name": "dimention name two",
"avgValue": 9.5
}
]
}
]
}
Can I implement this using Mongo aggregation or should I do it at service level?
What I have tried doing
db.getCollection('collResp').aggregate([
{
$match:
{
company: ObjectId("5a7848e8ca70273218e9d743"),
department: "1234567890"
}
},
{
$group: {
_id: {
topic: "$response.topic",
category: "$response.topicCategory"
},
responses: {
$push: "$response"
}
}
},
{
$lookup: {
from: 'topics',
localField: '_id.topic',
foreignField: '_id',
as: "topic"
}
},
{
$unwind: {
path: "$topic"
}
},
{
$lookup: {
from: 'topicCategories',
localField: '_id.category',
foreignField: '_id',
as: "category"
}
},
{
$unwind: {
path: "$category"
}
}
])
What I got
/* 1 */
{
"_id" : {
"topic" : ObjectId("5a78512cca70273218e9d8bd"),
"category" : ObjectId("5a784a10ca70273218e9d76f")
},
"responses" : [
{
"question" : ObjectId("5a7851a6ca70273218e9d8d9"),
"topic" : ObjectId("5a78512cca70273218e9d8bd"),
"topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
"dimention" : ObjectId("5a784fdcca70273218e9d869"),
"rating" : 8,
"color" : "red"
},
{
"question" : ObjectId("5a78580eca70273218e9da10"),
"topic" : ObjectId("5a78512cca70273218e9d8bd"),
"topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
"dimention" : ObjectId("5a7851d5ca70273218e9d8ee"),
"rating" : 7,
"color" : "red"
}
],
"topic" : {
"_id" : ObjectId("5a78512cca70273218e9d8bd"),
"topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
"name" : "TopicTwo",
"order" : 2,
"label" : "twoTopic",
"color" : "green"
},
"category" : {
"_id" : ObjectId("5a784a10ca70273218e9d76f"),
"name" : "topicCtegoryOne",
"order" : 1
}
}
/* 2 */
{
"_id" : {
"topic" : ObjectId("5a784cc3ca70273218e9d7d3"),
"category" : ObjectId("5a784a10ca70273218e9d76f")
},
"responses" : [
{
"question" : ObjectId("5a78519aca70273218e9d8d7"),
"topic" : ObjectId("5a784cc3ca70273218e9d7d3"),
"topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
"dimention" : ObjectId("5a784fdcca70273218e9d869"),
"rating" : 5,
"color" : "red"
},
{
"question" : ObjectId("5a785807ca70273218e9da0e"),
"topic" : ObjectId("5a784cc3ca70273218e9d7d3"),
"topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
"dimention" : ObjectId("5a7851d5ca70273218e9d8ee"),
"rating" : 3,
"color" : "red"
}
],
"topic" : {
"_id" : ObjectId("5a784cc3ca70273218e9d7d3"),
"topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
"name" : "TopicOne",
"order" : 1,
"label" : "oneTopic",
"color" : "red"
},
"category" : {
"_id" : ObjectId("5a784a10ca70273218e9d76f"),
"name" : "topicCtegoryOne",
"order" : 1
}
}
What more is required
Each response had a question. And each question has a dimention
I need to get average rating based on dimention.
Thank you.

How to query in mongodb to get distinct record with count

I have collection who's name is transactions.
I'm sharing the object of transactions collection
{
"_id" : ObjectId("58aaec83f1dc6914082afe31"),
"amount" : "33.00",
"coordinates" : {
"lat" : "4.8168",
"lon" : "36.4909"
},
"cuisine" : "Mexican",
"date" : ISODate("0062-02-22T11:46:52.738+05:30"),
"location" : {
"address" : "2414 Trudie Rue",
"city" : "West Alisa",
"state" : "New York",
"zip" : "10000"
},
"place_name" : "Outdoors",
"place_type" : "Wooden"
},
{
"_id" : ObjectId("58aaec83f1dc6914082afe32"),
"amount" : "557.00",
"coordinates" : {
"lat" : "-36.6784",
"lon" : "131.3698"
},
"cuisine" : "Australian",
"date" : ISODate("1294-10-04T19:53:15.562+05:30"),
"location" : {
"address" : "5084 Buckridge Cove",
"city" : "Sylviaview",
"state" : "Hawaii",
"zip" : "51416-6918"
},
"place_name" : "Toys",
"place_type" : "Cotton"
},
{
"_id" : ObjectId("58aaec83f1dc6914082afe33"),
"amount" : "339.00",
"coordinates" : {
"lat" : "45.1468",
"lon" : "91.4097"
},
"cuisine" : "Mexican",
"date" : ISODate("1568-11-25T02:54:53.046+05:30"),
"location" : {
"address" : "94614 Harry Island",
"city" : "Cartwrightside",
"state" : "Louisiana",
"zip" : "18825"
},
"place_name" : "Clothing",
"place_type" : "Frozen"
},
{
"_id" : ObjectId("58aaec83f1dc6914082afe34"),
"amount" : "173.00",
"coordinates" : {
"lat" : "-57.2738",
"lon" : "19.6381"
},
"cuisine" : "Australian",
"date" : ISODate("0804-05-07T03:00:07.724+05:30"),
"location" : {
"address" : "1933 Lewis Street",
"city" : "Aufderharville",
"state" : "Louisiana",
"zip" : "23416"
},
"place_name" : "Beauty",
"place_type" : "Fresh"
},
{
"_id" : ObjectId("58aaec83f1dc6914082afe34"),
"amount" : "173.00",
"coordinates" : {
"lat" : "-57.2738",
"lon" : "19.6381"
},
"cuisine" : "Australian",
"date" : ISODate("0804-05-07T03:00:07.724+05:30"),
"location" : {
"address" : "1933 Lewis Street",
"city" : "Aufderharville",
"state" : "Louisiana",
"zip" : "23416"
},
"place_name" : "Beauty",
"place_type" : "Fresh"
}
I want to get the list of distinct cuisine with total count
Output
{
"name" : 'Mexican',
"count" : '2'
},
{
"name" : 'Australian',
"count" : '3'
},
I could have done easily with mysql but I dot know in mongodb as I'm new with mongodb
I have tried with the example and I found nothing:
db.transactions.aggregate(
{$group: {_id:'$cuisine'},count:{$sum:1}}
).result;
Please try the code below. You should group by cuisine the records and get the count of them. Later in project pipeline you can define the final look.
db.transactions.aggregate([
{ $group: { _id: "$cuisine", count: { $sum: 1 } } },
{ $project:{ _id: 0, name: "$_id", count:"$count" } }
]);

How to use MongoDB $group stage to both group and count repeated values?

I am having trouble with the $group stage in my aggregation. I want to group all the "recentPlays.quiz" values together and count the repeated values, so the end result I want from the aggregation is two fields: the quiz object and the total. In this case it would be something like:
{
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester1",
"title" : "Asdffff Dfasdf"
},
"count": 1
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"count": 1
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
},
"count": 4
}
]
}
Here is the aggregation I have so far:
db.users.aggregate([
{$match: { "recentPlays.date": {$gte:twentyFourHrsAgo}}},
{$project: {"recentPlays.quiz":1, _id:0}}
]).pretty();
Here is that aggregation's output:
MongoDB shell version: 3.2.1
connecting to: videoQuiz
{
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester1",
"title" : "Asdffff Dfasdf"
}
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
}
]
}
Here is the entire collection:
MongoDB shell version: 3.2.1
connecting to: videoQuiz
{
"_id" : ObjectId("580f7be62c6fd3c8065577f5"),
"user" : "blue-tester1",
"email" : "aslfjjcc#lkcjasdc.com",
"createdAt" : ISODate("2016-10-25T15:36:06.933Z"),
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester1",
"title" : "Asdffff Dfasdf"
},
"score" : "0",
"date" : ISODate("2016-10-25T15:36:27.546Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"score" : "100",
"date" : ISODate("2016-10-25T15:37:09.142Z")
}
],
"mostRecentQuiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"mostRecentQuizTime" : ISODate("2016-10-25T15:37:09.142Z"),
"plays" : 2
}
{
"_id" : ObjectId("580a5dea650296d808082e65"),
"user" : "red-tester3",
"email" : "aldkdk#ccc.com",
"createdAt" : ISODate("2016-10-21T18:26:50.870Z"),
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester2",
"title" : "TOP PLAYED QUIZ - Today"
},
"score" : "0",
"date" : ISODate("2016-10-21T18:27:16.292Z")
},
{
"quiz" : {
"author" : "red-tester2",
"title" : "TOP LIKED QUIZ - TODAY"
},
"score" : "100",
"date" : ISODate("2016-10-21T18:27:32.788Z")
},
{
"quiz" : {
"author" : "red-tester2",
"title" : "TOP LIKED QUIZ - TODAY"
},
"score" : "100",
"date" : ISODate("2016-10-21T18:27:44.497Z")
},
{
"quiz" : {
"author" : "Bertram",
"title" : "frfrf"
},
"score" : "100",
"date" : ISODate("2016-10-21T18:28:43.893Z")
},
{
"quiz" : {
"author" : "Bertram",
"title" : "Here We Go With the New Thing"
},
"score" : "0",
"date" : ISODate("2016-10-21T18:43:51.205Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Presidents of the United States"
},
"score" : "0",
"date" : ISODate("2016-10-23T00:53:29.167Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Presidents of the United States"
},
"score" : "0",
"date" : ISODate("2016-10-23T00:53:44.815Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 1"
},
"score" : "100",
"date" : ISODate("2016-10-23T23:50:55.355Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"score" : "100",
"date" : ISODate("2016-10-23T23:52:33.210Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Here Is a New Title"
},
"score" : "100",
"date" : ISODate("2016-10-23T23:58:53.683Z")
}
],
"mostRecentQuiz" : {
"author" : "red-tester3",
"title" : "Here Is a New Title"
},
"mostRecentQuizTime" : ISODate("2016-10-23T23:58:53.683Z"),
"plays" : 10,
"likedQuizzes" : [
{
"title" : "TOP LIKED QUIZ - TODAY",
"author" : "red-tester2",
"date" : ISODate("2016-10-21T18:27:34.893Z")
},
{
"title" : "frfrf",
"author" : "Bertram",
"date" : ISODate("2016-10-21T18:28:45.863Z")
},
{
"title" : "Here We Go With the New Thing",
"author" : "Bertram",
"date" : ISODate("2016-10-21T18:43:53.148Z")
}
],
"createdQuizzes" : [
{
"title" : "Yeah Here We Go",
"id" : ObjectId("580a63f274b9a89c061f973e")
},
{
"title" : "Z Alpha",
"id" : ObjectId("580a641474b9a89c061f973f")
},
{
"title" : "Tags Limit Test",
"id" : ObjectId("580a6bda8d8049ac0bc1df2e")
},
{
"title" : "Tags Limit test2",
"id" : ObjectId("580a6bf98d8049ac0bc1df2f")
},
{
"title" : "Presidents of the United States",
"id" : ObjectId("580c09d28d8049ac0bc1df30")
},
{
"title" : "Creation Test 1",
"id" : ObjectId("580d4cca8d8049ac0bc1df31")
},
{
"title" : "Creation Test 2",
"id" : ObjectId("580d4d2d8d8049ac0bc1df32")
},
{
"title" : "Here Is a New Title",
"id" : ObjectId("580d4ead8d8049ac0bc1df33")
}
]
}
Thanks in advance for any guidance. Please excuse the dummy text in these documents, it is for testing purposes only.
This will be a two step process. The first step is to $unwind the "recentPlays" array. The second step is to $group by "recentPlays.quiz".
For example:
db.users.aggregate([
{ "$match" : { "recentPlays.date": { "$gte" : twentyFourHrsAgo}}},
{ "$project" : {"recentPlays.quiz":1, _id:0}},
{ "$unwind" : "$recentPlays" },
{ "$group" : { "_id" : "$recentPlays.quiz", "total" : { "$sum" : 1 } } }
]).pretty();

Query a collection for the latest unique object

Question: Lets say I have the following objects in a collection:
How would I return one record per "product_id" and only the one with the highest "version" number? And is this possible to do within mongoose?
{
"_id" : ObjectId("54f765564b10883c1800002a"),
"total_invoice_fob_case" : 86.70999999999999,
"status" : "Draft",
"discount" : "3.40",
"effective_date" : ISODate("2013-08-01T06:00:00.000Z"),
"version" : 2,
"controlstate" : "AB",
"controlstate_id" : ObjectId("54d510e9e3d793f581b6bb27"),
"product" : "Product A",
"product_id" : ObjectId("54f75b5e4b1088801a000627"),
"size" : "1.75LTR",
"size_id" : ObjectId("5418a3dd750b4294c2cb3a47"),
"vendor" : "BEAM SUNTORY",
"vendor_id" : ObjectId("54ef5aa74b1088781b000169"),
"product_state_code" : "123",
"net_fob_cost" : 86.70999999999999,
"change_reason" : [
"Other"
],
"submitted" : {
"submitted_date" : ISODate("2014-05-16T06:00:00.000Z")
}
},
{
"_id" : ObjectId("54f765564b10883c1800002b"),
"total_invoice_fob_case" : 86.70999999999999,
"status" : "Draft",
"discount" : "4.40",
"effective_date" : ISODate("2013-08-01T06:00:00.000Z"),
"version" : 3,
"controlstate" : "AB",
"controlstate_id" : ObjectId("54d510e9e3d793f581b6bb27"),
"product" : "Product A",
"product_id" : ObjectId("54f75b5e4b1088801a000627"),
"size" : "1.75LTR",
"size_id" : ObjectId("5418a3dd750b4294c2cb3a47"),
"vendor" : "BEAM SUNTORY",
"vendor_id" : ObjectId("54ef5aa74b1088781b000169"),
"product_state_code" : "123",
"net_fob_cost" : 86.70999999999999,
"change_reason" : [
"Other"
],
"submitted" : {
"submitted_date" : ISODate("2014-05-16T06:00:00.000Z")
}
},
{
"_id" : ObjectId("54f765564b10883c1800002c"),
"total_invoice_fob_case" : 86.70999999999999,
"status" : "Draft",
"discount" : "3.40",
"effective_date" : ISODate("2013-08-01T06:00:00.000Z"),
"version" : 2,
"controlstate" : "AB",
"controlstate_id" : ObjectId("54d510e9e3d793f581b6bb27"),
"product" : "Product B",
"product_id" : ObjectId("54f75b5e4b1088801a000628"),
"size" : "1.75LTR",
"size_id" : ObjectId("5418a3dd750b4294c2cb3a47"),
"vendor" : "BEAM SUNTORY",
"vendor_id" : ObjectId("54ef5aa74b1088781b000169"),
"product_state_code" : "123",
"net_fob_cost" : 86.70999999999999,
"change_reason" : [
"Other"
],
"submitted" : {
"submitted_date" : ISODate("2014-05-16T06:00:00.000Z")
}
}
Sounds like a job for Mongo's aggregation framework. You can extrapolate from this example how to approach the problem.
Update: To retrieve one per product_id with the highest version you would need to also use $first:
db.products.aggregate([
{$sort: {product_id: 1, version: -1}}, // sort first so that $first pulls the correct record
{$group: {
_id: {product_id: '$product_id'}, // group by the product_id
product: {$first: $$ROOT} // only return the first document per group
}}
]);
You need an aggregation pipeline that first sorts the documents in the collection by version number descending using a $sort pipeline stage, then groups the ordered documents by product_id using the $group operator. Within the grouping use the $first operator on $$ROOT to return the first document in the sorted group:
var pipeline = [
{
"$sort": { "version": -1 }
},
{
"$group": {
"_id": "$product_id",
"value": {
"$first": "$$ROOT"
}
}
},
{
"$project": {
"_id": 0,
"product_id": "$_id",
"status": "$value.status",
"version": "$value.version",
"product" : "$value.product"
}
}
];
// Mongoose aggregation
Model.aggregate(pipeline, function (err, res) {
if (err) return handleError(err);
console.log(res); //
});
Console Output:
[
{
"product_id" : ObjectId("54f75b5e4b1088801a000628"),
"status" : "Draft",
"version" : 2,
"product" : "Product B"
},
{
"product_id" : ObjectId("54f75b5e4b1088801a000627"),
"status" : "Draft",
"version" : 3,
"product" : "Product A"
}
]
-- UPDATE --
To project the full document, replace the $project pipeline with the following:
{
"$project": {
"_id": 0,
"product": "$value"
}
}
Output:
/* 1 */
{
"result" : [
{
"product" : {
"_id" : ObjectId("54f765564b10883c1800002c"),
"total_invoice_fob_case" : 86.7099999999999940,
"status" : "Draft",
"discount" : "3.40",
"effective_date" : ISODate("2013-08-01T06:00:00.000Z"),
"version" : 2,
"controlstate" : "AB",
"controlstate_id" : ObjectId("54d510e9e3d793f581b6bb27"),
"product" : "Product B",
"product_id" : ObjectId("54f75b5e4b1088801a000628"),
"size" : "1.75LTR",
"size_id" : ObjectId("5418a3dd750b4294c2cb3a47"),
"vendor" : "BEAM SUNTORY",
"vendor_id" : ObjectId("54ef5aa74b1088781b000169"),
"product_state_code" : "123",
"net_fob_cost" : 86.7099999999999940,
"change_reason" : [
"Other"
],
"submitted" : {
"submitted_date" : ISODate("2014-05-16T06:00:00.000Z")
}
}
},
{
"product" : {
"_id" : ObjectId("54f765564b10883c1800002b"),
"total_invoice_fob_case" : 86.7099999999999940,
"status" : "Draft",
"discount" : "4.40",
"effective_date" : ISODate("2013-08-01T06:00:00.000Z"),
"version" : 3,
"controlstate" : "AB",
"controlstate_id" : ObjectId("54d510e9e3d793f581b6bb27"),
"product" : "Product A",
"product_id" : ObjectId("54f75b5e4b1088801a000627"),
"size" : "1.75LTR",
"size_id" : ObjectId("5418a3dd750b4294c2cb3a47"),
"vendor" : "BEAM SUNTORY",
"vendor_id" : ObjectId("54ef5aa74b1088781b000169"),
"product_state_code" : "123",
"net_fob_cost" : 86.7099999999999940,
"change_reason" : [
"Other"
],
"submitted" : {
"submitted_date" : ISODate("2014-05-16T06:00:00.000Z")
}
}
}
],
"ok" : 1
}

quering a data with multiple collections in mongodb

I have a question on multiple collections in mongoDB.
I have 3 collections in my database and the collections names are Building, History and basic_amenities.
My question is, i want to retrieve the data of building ,history and basi_amenties to a particular building . I mean i want the data from building ,history and basic_amenities.
I want to do using aggregate concept. Is it possible to do like that or else is there any alternative method.
Building:
{
"_id" : "B1",
"Sale_type" : "Rental",
"Building_name" : "swamy",
"Available_apartments" : {
"Apartment_num" : "A6",
"Apartment_num" : "A9"
},
"Owner" : [
"sreekanth Buddha",
"sreekanthb6#gmail.com"
],
"Address" : {
"Street" : "blumenstrasse",
"Plot_no" : "13",
"City" : "Hamburg",
"State" : "lower saxony",
"Country" : "Germany",
"Postal_code" : "68245"
},
"Rental" : {
"Currency" : "EUR",
"Rental_price" : "10000",
"Available_date" : "02.03.2015",
"Deposit_amount" : "60000 EUR"
},
"Total_area" : "1200 sq meters",
"Apartment_id" : [
{
"id1" : "A1"
},
{
"id2" : "A5"
},
{
"id3" : "A7"
},
{
"id4" : "A2"
},
{
"id5" : "A9"
}
],
"Features" : {
"No_of_apartments" : "70",
"Community_hall" : "1",
"Garden" : 3,
"Office_room" : 1,
"Parking" : "yes",
"Play_ground" : "yes"
}
}
History:
"_id" : "H-B1",
"Property_id" : "B1",
"Builtyear" : "April 1995",
"year_of_registration" : [
{
"year" : ISODate("1995-04-15T23:00:00.000Z"),
"name" : "krishna malli"
},
{
"year" : ISODate("2008-07-16T23:00:00.000Z"),
"name" : "manoj kumar alluri"
},
{
"year" : ISODate("2014-10-29T23:00:00.000Z"),
"name" : "Ram dev swamy"
}
],
"Renovate" : [
{
"1995" : " building painting Renovated"
},
{
"2008" : " pipeline system was renovated"
},
{
"2014" : " roof was renovated"
}
]
}
**Basic_amenities:**
{
"_id" : "BA-B1",
"Property_id" : "B1",
"hospital " : "5 km",
"bahn_station" : "6 km ",
"restaurant" : "4 km",
"University" : "20 km",
"police_station" : "8 km",
"Airport" : "40 km",
"city_center" : " 5 km",
"Public_transp_type" : [
{
"Bus" : "35"
},
{
"tram" : "5"
},
{
"train_station" : "5km"
}
],
"keylandmark" : "Altstadt",
"future_activity" : "church constructing"
}
Can anyone help me how to query this data using aggregations concept or else is there any alternative method? please help me
Regards
Sreekanth