how to do aggregation query with pagination in druid(Druid is a column-oriented, distributed data store). Does it support query with offset? - druid

how to do aggregation query with pagination in druid(Druid is a column-oriented, distributed data store). Does it support query with offset?
I have searched but did not get anything so posting here. Thanks in advance.

Yes you can do pagination with select queries. See below druid documentation link for details - http://druid.io/docs/latest/querying/select-query.html
eg. you can send below query -
{
"queryType": "select",
"dataSource": "wikipedia",
"descending": "false",
"dimensions":[],
"metrics":[],
"granularity": "all",
"intervals": [
"2013-01-01/2013-01-02"
],
"pagingSpec":{"pagingIdentifiers": {}, "threshold":5}
}
Result -
[{
"timestamp" : "2013-01-01T00:00:00.000Z",
"result" : {
"pagingIdentifiers" : {
"wikipedia_2012-12-29T00:00:00.000Z_2013-01-10T08:00:00.000Z_2013-01-10T08:13:47.830Z_v9" : 4
},
"events" : [ {
"segmentId" : "wikipedia_editstream_2012-12-29T00:00:00.000Z_2013-01-10T08:00:00.000Z_2013-01-10T08:13:47.830Z_v9",
"offset" : 0,
"event" : {
"timestamp" : "2013-01-01T00:00:00.000Z",
"robot" : "1",
"namespace" : "article",
"anonymous" : "0",
"unpatrolled" : "0",
"page" : "11._korpus_(NOVJ)",
"language" : "sl",
"newpage" : "0",
"user" : "EmausBot",
"count" : 1.0,
"added" : 39.0,
"delta" : 39.0,
"variation" : 39.0,
"deleted" : 0.0
}
}, {
"segmentId" : "wikipedia_2012-12-29T00:00:00.000Z_2013-01-10T08:00:00.000Z_2013-01-10T08:13:47.830Z_v9",
"offset" : 1,
"event" : {
"timestamp" : "2013-01-01T00:00:00.000Z",
"robot" : "0",
"namespace" : "article",
"anonymous" : "0",
"unpatrolled" : "0",
"page" : "112_U.S._580",
"language" : "en",
"newpage" : "1",
"user" : "MZMcBride",
"count" : 1.0,
"added" : 70.0,
"delta" : 70.0,
"variation" : 70.0,
"deleted" : 0.0
}
}, {
"segmentId" : "wikipedia_2012-12-29T00:00:00.000Z_2013-01-10T08:00:00.000Z_2013-01-10T08:13:47.830Z_v9",
"offset" : 2,
"event" : {
"timestamp" : "2013-01-01T00:00:00.000Z",
"robot" : "0",
"namespace" : "article",
"anonymous" : "0",
"unpatrolled" : "0",
"page" : "113_U.S._243",
"language" : "en",
"newpage" : "1",
"user" : "MZMcBride",
"count" : 1.0,
"added" : 77.0,
"delta" : 77.0,
"variation" : 77.0,
"deleted" : 0.0
}
}, {
"segmentId" : "wikipedia_2012-12-29T00:00:00.000Z_2013-01-10T08:00:00.000Z_2013-01-10T08:13:47.830Z_v9",
"offset" : 3,
"event" : {
"timestamp" : "2013-01-01T00:00:00.000Z",
"robot" : "0",
"namespace" : "article",
"anonymous" : "0",
"unpatrolled" : "0",
"page" : "113_U.S._73",
"language" : "en",
"newpage" : "1",
"user" : "MZMcBride",
"count" : 1.0,
"added" : 70.0,
"delta" : 70.0,
"variation" : 70.0,
"deleted" : 0.0
}
}, {
"segmentId" : "wikipedia_2012-12-29T00:00:00.000Z_2013-01-10T08:00:00.000Z_2013-01-10T08:13:47.830Z_v9",
"offset" : 4,
"event" : {
"timestamp" : "2013-01-01T00:00:00.000Z",
"robot" : "0",
"namespace" : "article",
"anonymous" : "0",
"unpatrolled" : "0",
"page" : "113_U.S._756",
"language" : "en",
"newpage" : "1",
"user" : "MZMcBride",
"count" : 1.0,
"added" : 68.0,
"delta" : 68.0,
"variation" : 68.0,
"deleted" : 0.0
}
} ]
}
} ]
Result comes with pagingIdentifiers which you can pass in next query.
Please note it doesn't work with top N queries as of now.
Update -
For top N queries or aggregated results, there is currently no direct way of fetching paginated results, but you can use bigger threshold and limits and exclude the previous results at client side.

Related

Update nested documents inside array based on condition

i have the next document and i have to update promotion object according to promotionId
executing i can update, but this query erases the other products that have no the promotion in the condition:
if i use update many i get Error: the update operation document must contain atomic operators.
Please help me to get the error to update but keeping the products that does not have the promo i am updating.
/* 1 */
db.fulFillmentCenter.update({},[
{
$addFields: {
exclusiveProducts: {
$filter: {
input: "$exclusiveProducts",
as: "product",
cond: {
$eq: [
"$$product.promotion.promotionId",
'promo1'
]
}
}
}
}
},
{$set: { "exclusiveProducts.promotion.description": "multi 2"}}
/* 2 */
{
"_id" : 1,
"_class" : "com.walmart.labsmx.exclusiveproducts.model.FulFillmentCenter",
"minDateDelivery" : 9,
"maxDateDelivery" : 2,
"deliveryCost" : 10.0,
"origin" : "string",
"active" : true,
"exclusiveProducts" : [
{
"_id" : NumberLong(1585867035599),
"upc" : "p1",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "debe aparece",
"startDate" : ISODate("2020-02-10T02:47:03.472Z"),
"endDate" : ISODate("2020-04-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035613),
"promotionId" : "promo1",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035642),
"upc" : "p2",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "no debe aparece",
"startDate" : ISODate("2020-02-10T02:47:03.472Z"),
"endDate" : ISODate("2020-04-10T02:47:03.472Z"),
"active" : false,
"promotion" : {
"_id" : NumberLong(1585867035654),
"promotionId" : "promo1",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035607),
"upc" : "p3",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "no debe aparece",
"startDate" : ISODate("2020-02-10T02:47:03.472Z"),
"endDate" : ISODate("2020-03-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035668),
"promotionId" : "promo1",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035644),
"upc" : "p4",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "debe aparece",
"startDate" : ISODate("2020-02-10T02:47:03.472Z"),
"endDate" : ISODate("2020-05-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035632),
"promotionId" : "promo1",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035666),
"upc" : "p5",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "no debe aparece",
"startDate" : ISODate("2020-05-10T02:47:03.472Z"),
"endDate" : ISODate("2020-07-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035591),
"promotionId" : "promo1",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035669),
"upc" : "p20",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "no debe aparece",
"startDate" : ISODate("2020-05-10T02:47:03.472Z"),
"endDate" : ISODate("2020-07-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035661),
"promotionId" : "promo2",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035643),
"upc" : "p21",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "no debe aparece",
"startDate" : ISODate("2020-05-10T02:47:03.472Z"),
"endDate" : ISODate("2020-07-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035612),
"promotionId" : "promo3",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
}
],
"exclusiveStores" : [
{
"storeId" : 123,
"origin" : "string"
}
]
}
This update query updates all the nested documents promotion within the array exclusiveProducts. The value of "promotion.description" is modified with the new value "multi 2", where the "promotion.promotionId" = "promo1".
var new_desc = "multi 2";
db.collection.updateMany(
{ "exclusiveProducts.promotion.promotionId": "promo1" },
{ $set: { "exclusiveProducts.$[pr].promotion.description": new_desc } },
{ arrayFilters: [ { "pr.promotion.promotionId": "promo1" } ] }
)

Nested object update query in mongodb

I have object in my procedures collection.
I want to update item_status of item_id 5996c80fca423ce1228f7690 which is available in preferences.items.
I want to get all records who has mentioned item_id so that we can update all occurrences.
{
"_id" : ObjectId("5996d0a1ca423ce1228f777a"),
"status" : "Active",
"procedure_name" : "ATHA",
"procedure_for" : "Admin",
"created_by" : "5940c3e8734d1d79866338cf",
"created_on" : ISODate("2017-10-19T18:44:22.702+0000"),
"speciality_id" : "5751131e3a1253845560a984",
"speciality" : "Orthopdics",
"master_template_id" : "",
"hospital_id" : "",
"surgeon_nurse_id" : "",
"procedure_type" : "Published",
"published_on" : ISODate("2017-10-19T18:44:22.702+0000"),
"surgical_sequence" : [
],
"preferences" : [
{
"category_id" : "5971fae84658f5241d8a5b70",
"category" : "Instruments",
"_id" : ObjectId("59e8f2861c999f292a837304"),
"items" : [
{
"item_id" : "5996c80fca423ce1228f7690",
"item_name" : "Battery",
"item_description" : "",
"image_name" : "BATTERY.png",
"icon_image" : "",
"side_view_image" : "",
"_id" : ObjectId("59e8f2861c999f292a837306"),
"attributes" : [
],
"subcategory" : [
{
"name" : "Power tool",
"subcategory_id" : "5996c80eca423ce1228f7549",
"parent_id" : "",
"_id" : ObjectId("5996c80fca423ce1228f7709")
},
{
"name" : "Battery",
"subcategory_id" : "5996c80eca423ce1228f750e",
"parent_id" : "5996c80eca423ce1228f7549",
"_id" : ObjectId("5996c80fca423ce1228f7708")
}
]
}
]
}
],
"__v" : NumberInt(0),
"modified_by" : "5940c3e8734d1d79866338cf",
"modified_on" : ISODate("2017-10-19T18:44:22.702+0000")
}
I'm assuming you are talking about updating status field since I don't find any field with the name item_status. If that's the case, the below query should work:
> db.procedures.updateMany({"preferences.$.items.$.item_id": "5996c80fca423ce1228f7690"}, {"$set": {"status": "new_status"}})

String to ISO Date

I've looked this up a few times and no answer has worked so far. I want to turn a string date into an ISODate - I don't want it converted to or from UTC, I just want to make the date value which is stored as string to be stored as a date.
I don't want UTC because I want to query against and calculate based upon a time of day (example: 12pm) anywhere in the world and I'm summing by hour, day, week and month and comparing data across many countries based upon local time.
Currently I have a date as a string which I want to add to a new document with a new name:
"transaction_date": "$object_raw_origin_data.register_sales.sale_date"
The date value as string below is shown as:
"sale_date" : "2018-03-13 20:05:46"
I want it to be: "transaction_date" : ISODate("2018-03-13 20:05:46")
The date and time must read 2018-03-13 20:05:46 not converted to UTC.
Thanks, Matt
Origin Data JSON
{
"object_category" : "application",
"object_type" : "register-sales-24-months",
"object_origin" : "vend",
"tenant_uuid" : "00000000-0000-0009-9999-999999999999",
"party_uuid" : "8d519765-05d2-469f-ad35-d7a22fa9df2f",
"subscription_uuid" : "0",
"connection_uuid" : "6ed9bd79-d9c5-4296-a821-7e15b1c69e6c",
"status" : "",
"object_created" : ISODate("2018-03-15T21:40:57.158+0000"),
"object_raw_origin_data" : {
"pagination" : {
"results" : NumberInt(75964),
"page" : NumberInt(1),
"page_size" : NumberInt(200),
"pages" : NumberInt(380)
},
"register_sales" : {
"id" : "776a66f2-993c-b372-11e8-26f9d3c1bdde",
"source" : "USER",
"source_id" : "",
"register_id" : "02dcd191-ae55-11e6-edd8-ec8dce1d9e1c",
"market_id" : "3",
"customer_id" : "02d59481-b67d-11e5-f667-b08185e8f6d5",
"customer_name" : "",
"customer" : {
"id" : "02d59481-b67d-11e5-f667-b08185e8f6d5",
"name" : "",
"customer_code" : "WALKIN",
"customer_group_id" : "02d59481-b67d-11e5-f667-b08185e893f8",
"customer_group_name" : "All Customers",
"updated_at" : "2016-01-01 12:16:44",
"deleted_at" : "",
"balance" : "0",
"year_to_date" : "0",
"date_of_birth" : "",
"sex" : "",
"custom_field_1" : "",
"custom_field_2" : "",
"custom_field_3" : "",
"custom_field_4" : "",
"note" : "",
"contact" : {
}
},
"user_id" : "02d59481-b655-11e5-f667-dca974edc4ea",
"user_name" : "Alvaro Velosa",
"sale_date" : "2018-03-13 20:05:46",
"created_at" : "2018-03-13 20:06:00",
"updated_at" : "2018-03-13 20:06:00",
"total_price" : 4.5,
"total_cost" : 3.34,
"total_tax" : NumberInt(0),
"tax_name" : "No Tax",
"note" : "",
"status" : "CLOSED",
"short_code" : "wqgsgi",
"invoice_number" : "Masonic2108Temple",
"accounts_transaction_id" : "",
"return_for" : "",
"register_sale_products" : [
{
"id" : "776a66f2-993c-b372-11e8-26f9e92cb9bc",
"product_id" : "02dcd191-ae55-11e7-f130-9d4f4bcd91b1",
"register_id" : "02dcd191-ae55-11e6-edd8-ec8dce1d9e1c",
"sequence" : "0",
"handle" : "LAGERDRAUGHT300",
"sku" : "10287",
"name" : "LAGER DRAUGHT £3.00",
"quantity" : NumberInt(1),
"price" : 3.5,
"cost" : 2.74,
"price_set" : NumberInt(0),
"discount" : NumberInt(0),
"loyalty_value" : NumberInt(0),
"tax" : NumberInt(0),
"tax_id" : "02d59481-b67d-11e5-f667-b08185ec2871",
"tax_name" : "No Tax",
"tax_rate" : NumberInt(0),
"tax_total" : NumberInt(0),
"price_total" : 3.5,
"display_retail_price_tax_inclusive" : "0",
"status" : "CONFIRMED",
"attributes" : [
{
"name" : "line_note",
"value" : ""
}
]
},
{
"id" : "776a66f2-993c-b372-11e8-26f9e98104e0",
"product_id" : "02dcd191-ae55-11e7-f130-9d50e948a0b5",
"register_id" : "02dcd191-ae55-11e6-edd8-ec8dce1d9e1c",
"sequence" : "0",
"handle" : "SOFTDRINK",
"sku" : "10292",
"name" : "SOFT DRINK",
"quantity" : NumberInt(1),
"price" : NumberInt(1),
"cost" : 0.6,
"price_set" : NumberInt(0),
"discount" : NumberInt(0),
"loyalty_value" : NumberInt(0),
"tax" : NumberInt(0),
"tax_id" : "02d59481-b67d-11e5-f667-b08185ec2871",
"tax_name" : "No Tax",
"tax_rate" : NumberInt(0),
"tax_total" : NumberInt(0),
"price_total" : NumberInt(1),
"display_retail_price_tax_inclusive" : "0",
"status" : "CONFIRMED",
"attributes" : [
{
"name" : "line_note",
"value" : ""
}
]
}
],
"totals" : {
"total_tax" : NumberInt(0),
"total_price" : 4.5,
"total_payment" : 4.5,
"total_to_pay" : NumberInt(0)
},
"register_sale_payments" : [
{
"id" : "776a66f2-993c-b372-11e8-26f9eab2cb46",
"payment_type_id" : "1",
"register_id" : "02dcd191-ae55-11e6-edd8-ec8dce1d9e1c",
"retailer_payment_type_id" : "02d59481-b655-11e5-f667-b0a23bc0e7bc",
"name" : "Cash",
"label" : "Account Customer",
"payment_date" : "2018-03-13 20:05:46",
"amount" : NumberInt(10)
},
{
"id" : "776a66f2-993c-b372-11e8-26f9eab6b21a",
"payment_type_id" : "1",
"register_id" : "02dcd191-ae55-11e6-edd8-ec8dce1d9e1c",
"retailer_payment_type_id" : "02d59481-b655-11e5-f667-b0a23bc0e7bc",
"name" : "Cash",
"label" : "Account Customer",
"payment_date" : "2018-03-13 20:05:46",
"amount" : -5.5
}
]
}
}
}
Thanks for the help from #Saleem and #Krishna however, I have tried to create an ISODate using a $concat of $substr from a string and it isn't working.
db.Vend_raw_transactions.aggregate(
[{
"$project": {
"object_origin": "$object_origin",
"company": "$party_uuid",
"connection": "$connection_uuid",
"object_creation_date": "$object_created",
// "transaction_date": "$object_raw_origin_data.register_sales.sale_date",
"transaction_date": {
"$dateFromString": {
"dateString": {
"$concat": [
"$substr": [ "$object_raw_origin_data.register_sales.sale_date", 0, 10 ],
"T",
"$substr": [ "$object_raw_origin_data.register_sales.sale_date", 11, 9]
]
}
}
},
"transaction_gross_value": {
"$add": [
"$object_raw_origin_data.register_sales.total_price",
"$object_raw_origin_data.register_sales.total_tax"
]
},
"transaction_net_value": "$object_raw_origin_data.register_sales.total_price",
"transaction_tax_value": "$object_raw_origin_data.register_sales.total_tax",
"transaction_cost_value": "$object_raw_origin_data.register_sales.total_cost",
"object_class": "goods-service-transaction",
"object_origin_category": "point-of-sale",
"object_type": "receipt",
"object_origin_type": "offline",
"transaction_reference": "$object_raw_origin_data.register_sales.invoice_number",
"transaction_status": "$object_raw_origin_data.register_sales.status",
"transaction_currency": "GBP",
"party_name": "$object_raw_origin_data.register_sales.customer.customer_name",
"party_identifier": "$object_raw_origin_data.register_sales.customer.customer_code",
"staff_identifier": "$object_raw_origin_data.register_sales.user_id",
"staff_name": "$object_raw_origin_data.register_sales.user_name",
"line_items" : {
"$map": {
"input": "$object_raw_origin_data.register_sales.register_sale_products",
"as" : "product",
"in": {"item_name": "$$product.name",
"item_system_id": "$$product.id",
"item_identifier": "$$product.sku",
"item_category" : "sales-revenue",
"item_quantity" : "$$product.quantity",
"item_net_unit_sale_value" : "$$product.price",
"item_net_unit_discount_value" : "$$product.discount",
"item_net_unit_member_value" : "$$product.loyalty_value",
"item_net_unit_cost_value" : "$$product.cost",
"item_unit_tax_value" : "$$product.tax",
"item_price_list_reference" : "$$product.price_set",
"item_total_sale_value" : "$$product.price_total",
"item_total_tax_value" : "$$product.tax_total"
}
}
}
}
}
// ,{"$out": "9SP_Source" }
])
I cannot work out why this isn't working, it seems like it should - I'm creating a new string based upon an existing string value and adding a T in the middle but it doesn't recognise $DateFromString. Thanks
Error Message
{
"message" : "Unrecognized expression '$dateFromString'",
"stack" : "MongoError: Unrecognized expression '$dateFromString'" +
"at queryCallback (C:\Users\mattl\AppData\Local\Programs\nosqlbooster4mongo\resources\app.asar\node_modules\mongodb-core\lib\cursor.js:223:25)" +
"at C:\Users\mattl\AppData\Local\Programs\nosqlbooster4mongo\resources\app.asar\node_modules\mongodb-core\lib\connection\pool.js:541:18" +
"at _combinedTickCallback (internal/process/next_tick.js:131:7)" +
"at process._tickCallback (internal/process/next_tick.js:180:9)",
"name" : "MongoError",
"ok" : 0,
"errmsg" : "Unrecognized expression '$dateFromString'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"
}
I've managed to make another string using the below script but $dateFromString unrecognised
db.Vend_raw_transactions.aggregate(
[{
"$project": {
"origin_date": "$object_raw_origin_data.register_sales.sale_date",
"transaction_date": {
"$substr": [ "$object_raw_origin_data.register_sales.sale_date", 0, 10 ]
},
"transaction_time": {
"$substr": [ "$object_raw_origin_data.register_sales.sale_date", 11, 9 ]
},
"new_string_date": {
"$concat" :[
{"$substr": [ "$object_raw_origin_data.register_sales.sale_date", 0, 10 ]},
"T",
{"$substr": [ "$object_raw_origin_data.register_sales.sale_date", 11, 9 ]}]
}
}
}
])
Thanks

MongoDB shell mongoimport not importing document from json file

Can someone help me figure out what the problem is with this cos i have been trying to get it work but all efforts avail abortive
mongoimport --db test --collection products --drop --file"C:\Program Files\MongoDB\Server\scripts\products.json"
running the above on my shell keeps given me the below error
2018-01-13T19:55:55.019+0100 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:14
======================Update=========================
{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "warranty_years" : 1, "available" : true }
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "warranty_years" : 1, "available" : false }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bf9" }, "name" : "AC3 Series Charger", "type" : [ "accessory", "charger" ], "price" : 19, "warranty_years" : 0.25, "for" : [ "ac3", "ac7", "ac9" ] }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfa" }, "name" : "AC3 Case Green", "type" : [ "accessory", "case" ], "color" : "green", "price" : 12, "warranty_years" : 0 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfb" }, "name" : "Phone Extended Warranty", "type" : "warranty", "price" : 38, "warranty_years" : 2, "for" : [ "ac3", "ac7", "ac9", "qp7", "qp8", "qp9" ] }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfc" }, "name" : "AC3 Case Black", "type" : [ "accessory", "case" ], "color" : "black", "price" : 12.5, "warranty_years" : 0.25, "available" : false, "for" : "ac3" }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfd" }, "name" : "AC3 Case Red", "type" : [ "accessory", "case" ], "color" : "red", "price" : 12, "warranty_years" : 0.25, "available" : true, "for" : "ac3" }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfe" }, "name" : "Phone Service Basic Plan", "type" : "service", "monthly_price" : 40, "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } }, "term_years" : 2 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bff" }, "name" : "Phone Service Core Plan", "type" : "service", "monthly_price" : 60, "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "term_years" : 1 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15c00" }, "name" : "Phone Service Family Plan", "type" : "service", "monthly_price" : 90, "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "sales_tax" : true, "term_years" : 2 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15c01" }, "name" : "Cable TV Basic Service Package", "type" : "tv", "monthly_price" : 50, "term_years" : 2, "cancel_penalty" : 25, "sales_tax" : true, "additional_tarriffs" : [ { "kind" : "federal tarriff", "amount" : { "percent_of_service" : 0.06 } }, { "kind" : "misc tarriff", "amount" : 2.25 } ] }

Need Help Join Collection

Collection Inventory sample data:
{
"_id" : "89011704252315531324",
"sku" : "A2015-01-000",
"type" : "package",
"status" : "active",
"lng" : "-72.789153",
"lat" : "44.173515",
"acq" : "28",
"gtime" : ISODate("2017-01-11T22:27:48.000Z"),
"qlng" : "-72.796501",
"qlat" : "44.214783",
"qtime" : ISODate("2016-11-27T18:21:10.000Z"),
"timestamp" : ISODate("2017-01-12T14:43:29.000Z"),
"modified" : Date(-62135596800000),
"battery" : "60",
"wearables" : [
{
"_id" : "0009003C100228234E45",
"type" : "wearable",
"status" : "active",
"battery" : "50",
"timestamp" : ISODate("2017-01-12T11:43:33.000Z")
},
{
"_id" : "004A003F200B36634E45",
"type" : "cradle",
"status" : "active",
"battery" : "64",
"timestamp" : ISODate("2017-01-11T22:27:26.000Z")
},
{
"_id" : "11223344556600000B55",
"type" : "falldetect",
"status" : "active",
"battery" : "64",
"timestamp" : ISODate("2017-01-12T08:43:29.000Z")
}
],
"company" : "ConnectAmericaProduction",
"companies" : [],
"remoteIp" : "172.31.45.196:53864",
"subscriber" : "5783e20aa2c89f346e000006",
"ring" : "90",
"speaker" : "90",
"mic" : "55",
"version" : "4352",
"cradle" : "OFF",
"ctime" : ISODate("2017-01-11T23:13:59.000Z"),
"csqtime" : Date(-62135596800000)
}
collection calllog sample data
{
"_id" : "89011704252315531324",
"cdr" : [
{
"direction" : "Outgoing",
"duration" : 46,
"timestamp" : ISODate("2016-11-23T03:25:06.000Z"),
"number" : "",
"name" : "Call Center",
"lng" : "-71.208061",
"lat" : "42.330265",
"acq" : "",
"timezone" : {
"dstOffset" : 0.0,
"rawOffset" : 0.0,
"status" : "",
"timeZoneId" : "",
"timeZoneName" : ""
}
},
{
"direction" : "Incoming",
"duration" : 51,
"timestamp" : ISODate("2016-11-23T03:26:02.000Z"),
"number" : "",
"name" : "Call Center",
"lng" : "-71.205727",
"lat" : "42.333347",
"acq" : "",
"timezone" : {
"dstOffset" : 0.0,
"rawOffset" : 0.0,
"status" : "",
"timeZoneId" : "",
"timeZoneName" : ""
}
},
{
"direction" : "Outgoing",
"duration" : 49,
"timestamp" : ISODate("2016-11-27T18:21:04.000Z"),
"number" : "",
"name" : "Call Center",
"lng" : "-72.796501",
"lat" : "44.214783",
"acq" : "",
"timezone" : {
"dstOffset" : 0.0,
"rawOffset" : -18000.0,
"status" : "OK",
"timeZoneId" : "America/New_York",
"timeZoneName" : "Eastern Standard Time"
}
}
]
}
after run this aggrgrate function
db.calllog.aggregate([{$unwind: "$cdr"}, {$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as: "wearables" }}, { "$project": { "cdr.direction": 1, "cdr.duration": 1,"cdr.date": 1,"wearables.type": 1, "wearables.status": 1, "wearables.battery": 1} }])
Result:
{ "_id" : "89011704252315531324", "cdr" : { "direction" : "Outgoing", "duration" : 46 }, "wearables" : [ { "type" : "package", "status" : "active", "battery" : "60" } ] }
{ "_id" : "89011704252315531324", "cdr" : { "direction" : "Incoming", "duration" : 51 }, "wearables" : [ { "type" : "package", "status" : "active", "battery" : "60" } ] }
{ "_id" : "89011704252315531324", "cdr" : { "direction" : "Outgoing", "duration" : 49 }, "wearables" : [ { "type" : "package", "status" : "active", "battery" : "60" } ] }
needed help can not get query to shows wearables type such as wearable, cradle, falldetect
thank
Did you try to do wearables.wearables.type? When you call wearables.type, you are actually getting the type of the Inventory. If you need the type of the wearable that is inside the inventory. You need to put inventory.wearables.type. The initial problem is that you're calling the inventory "wearables" and making a confusion out of it.
I would do the following:
db.calllog.aggregate([{$unwind: "$cdr"},
{$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as: "inventory" }},
{$unwind: "$inventory.wearables"},
{ "$project": {
"cdr.direction": 1,
"cdr.duration": 1,
"cdr.date": 1,
"inventory.type": 1,
"inventory.status": 1,
"inventory.battery": 1,
"inventory.wearables.type":1
}}])