My document structure is shown here:
{
"_id" : ObjectId("52517d9e6e0af435ddd48219"),
"date" : ISODate("2013-10-06T18:11:26.329Z"),
"engines" : {
"ahnlab" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "ahnlab",
"node_name" : "",
"status" : 1,
"task_id" : "7fac4f67-2bde-49de-980a-4f7fa1d46db7",
"threat" : "EICAR_Test_File"
},
"avast" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "avast",
"node_name" : "",
"status" : 4,
"task_id" : "fdce50e9-9bf3-4cc9-91a4-b674a108d478",
"threat" : ""
},
"avg" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "avg",
"node_name" : "",
"status" : 1,
"task_id" : "4bdb4a37-80ab-4631-8587-edcbde7e2592",
"threat" : "EICAR_Test"
},
"avira" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "avira",
"node_name" : "",
"status" : 4,
"task_id" : "43b43bab-ff67-440c-9919-f6241ccaf539",
"threat" : ""
},
"bitdefender" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "bitdefender",
"node_name" : "",
"status" : 4,
"task_id" : "ba7efe74-ef93-465f-b6c3-6c4fc746934d",
"threat" : ""
},
"comodo" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "comodo",
"node_name" : "",
"status" : 4,
"task_id" : "831d2dc8-704b-4eb7-9a75-9a0364a8ab09",
"threat" : ""
},
"drweb" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "drweb",
"node_name" : "",
"status" : 4,
"task_id" : "ebb758c3-9146-4a99-b36f-0fb6ee024a33",
"threat" : ""
},
"f-prot" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "f-prot",
"node_name" : "",
"status" : 4,
"task_id" : "13590a25-ac0d-4b1b-b93e-bc715009432a",
"threat" : ""
},
"forticlient" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "forticlient",
"node_name" : "",
"status" : 4,
"task_id" : "",
"threat" : ""
},
"kaspersky" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "kaspersky",
"node_name" : "",
"status" : 4,
"task_id" : "",
"threat" : ""
},
"malwarebytes" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "malwarebytes",
"node_name" : "",
"status" : 4,
"task_id" : "a97f3d9a-d6a9-44df-8355-c053e9f4980a",
"threat" : ""
},
"mcafee" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "mcafee",
"node_name" : "",
"status" : 1,
"task_id" : "e7a592bb-84fc-4c47-a1ed-3719874b19cc",
"threat" : "EICAR test file"
},
"msessentials" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "msessentials",
"node_name" : "",
"status" : 4,
"task_id" : "01c9fb71-155a-473d-b45c-91fa117ae649",
"threat" : ""
},
"nod32" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "nod32",
"node_name" : "",
"status" : 4,
"task_id" : "a715cc8f-0e2f-4698-b883-a35fa6add13e",
"threat" : ""
},
"norman" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "norman",
"node_name" : "",
"status" : 4,
"task_id" : "29da2955-0674-45d2-ac4d-c0b3ea401cba",
"threat" : ""
},
"norton" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "norton",
"node_name" : "",
"status" : 4,
"task_id" : "c4cd1e3e-2f0d-4bf6-84cf-ab0962f7f4ed",
"threat" : ""
},
"panda" : {
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "panda",
"node_name" : "",
"status" : 4,
"task_id" : "b0888f7c-e4a4-4b4e-b163-21d283e166f1",
"threat" : ""
}
},
"expiration_date" : ISODate("2013-10-06T18:11:36.329Z"),
"file_name" : "ffgtr.exe",
"scan_status" : "DONE",
"task_id" : "4ce4ae9e-ef0a-476a-8189-92a5bfe328bd"
}
I would like to project with aggregation the fields 'date' and 'definitions' of every engine.
('definitions' is an embedded field)
I succeeded doing it only if I specify the specific engine:
cursor = collection.aggregate([
{ "$match": { "date": { "$gte": startdate } } },
{'$project': { "def": "$engines.avast.definitions","date":1,"_id": 0 }}
], allowDiskUse=True)
But I would like to run the query for all engines without specifying specific engine, something like:
cursor = collection.aggregate([
{ "$match": { "date": { "$gte": startdate } } },
{'$project': { "def": "$engines.$elemMatch.definitions","date":1,"_id": 0 }}
], allowDiskUse=True)
(which doesn't work)
What I want to find is the average difference between "date" and each "engine" definition date per engine type.
The main problem here is the structure of your data. In order to work with the aggregation framework and also happily work with most other MongoDB query operations and indexing practices then your data should be structured like this instead.
{
"_id" : ObjectId("52517d9e6e0af435ddd48219"),
"date" : ISODate("2013-10-06T18:11:26.329Z"),
"engines" : [
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "ahnlab",
"node_name" : "",
"status" : 1,
"task_id" : "7fac4f67-2bde-49de-980a-4f7fa1d46db7",
"threat" : "EICAR_Test_File"
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "avast",
"node_name" : "",
"status" : 4,
"task_id" : "fdce50e9-9bf3-4cc9-91a4-b674a108d478",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "avg",
"node_name" : "",
"status" : 1,
"task_id" : "4bdb4a37-80ab-4631-8587-edcbde7e2592",
"threat" : "EICAR_Test"
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "avira",
"node_name" : "",
"status" : 4,
"task_id" : "43b43bab-ff67-440c-9919-f6241ccaf539",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "bitdefender",
"node_name" : "",
"status" : 4,
"task_id" : "ba7efe74-ef93-465f-b6c3-6c4fc746934d",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "comodo",
"node_name" : "",
"status" : 4,
"task_id" : "831d2dc8-704b-4eb7-9a75-9a0364a8ab09",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "drweb",
"node_name" : "",
"status" : 4,
"task_id" : "ebb758c3-9146-4a99-b36f-0fb6ee024a33",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "f-prot",
"node_name" : "",
"status" : 4,
"task_id" : "13590a25-ac0d-4b1b-b93e-bc715009432a",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "forticlient",
"node_name" : "",
"status" : 4,
"task_id" : "",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "kaspersky",
"node_name" : "",
"status" : 4,
"task_id" : "",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "malwarebytes",
"node_name" : "",
"status" : 4,
"task_id" : "a97f3d9a-d6a9-44df-8355-c053e9f4980a",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "mcafee",
"node_name" : "",
"status" : 1,
"task_id" : "e7a592bb-84fc-4c47-a1ed-3719874b19cc",
"threat" : "EICAR test file"
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "msessentials",
"node_name" : "",
"status" : 4,
"task_id" : "01c9fb71-155a-473d-b45c-91fa117ae649",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "nod32",
"node_name" : "",
"status" : 4,
"task_id" : "a715cc8f-0e2f-4698-b883-a35fa6add13e",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "norman",
"node_name" : "",
"status" : 4,
"task_id" : "29da2955-0674-45d2-ac4d-c0b3ea401cba",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "norton",
"node_name" : "",
"status" : 4,
"task_id" : "c4cd1e3e-2f0d-4bf6-84cf-ab0962f7f4ed",
"threat" : ""
},
{
"definitions" : ISODate("2000-01-01T00:00:00.000Z"),
"name" : "panda",
"node_name" : "",
"status" : 4,
"task_id" : "b0888f7c-e4a4-4b4e-b163-21d283e166f1",
"threat" : ""
}
],
"expiration_date" : ISODate("2013-10-06T18:11:36.329Z"),
"file_name" : "ffgtr.exe",
"scan_status" : "DONE",
"task_id" : "4ce4ae9e-ef0a-476a-8189-92a5bfe328bd"
}
Then working with that structure is as simple as processing $unwind and then a $group for your results:
db.collection.aggregate([
{ "$unwind": "$engines" },
{ "$group": {
"_id": "$engines.name",
"avgInterval": { "$avg": {
"$subtract": [ "$date", "$engines.definitions" ]
}}
}}
])
The difference will be in milliseconds between two date objects.
Otherwise you are stuck with traversing the object keys in code using JavaScript with mapReduce:
db.collection.mapReduce(
function() {
var doc = this;
Object.keys(doc.engines).forEach(function(key) {
emit( key,
( doc.date.valueOf() -
doc.engines[key].definitions.valueOf())
);
})
},
function(key,values) {
return ( Array.sum(values) / values.length );
},
{ "out": { "inline": 1 } }
);
Both will give you the average value for each engine "name" accross the collection or other query input.
Try to change the structure as it is better suited to your ongoing needs and provides much faster processing.
Related
my need is copy one subdocument from a document and insert into the
same document.But i use forEach and findAndModify it can not insert
into the same document.
document example:
{
"_id" : ObjectId("59b5e84d71ab5580d643d070"),
"modifiedOn" : ISODate("2019-04-03T14:57:22.177+0000"),
"modifiedBy" : "XXX",
"createdOn" : ISODate("2017-09-09T16:33:34.464+0000"),
"createdBy" : "liuyu",
"channelSales" : [
{
"platform" : "amazon",
"channel" : "amazon_ca",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39dc1")
},
{
"platform" : "amazon",
"channel" : "amazon_uk",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39dc0")
},
{
"platform" : "amazon",
"channel" : "amazon_us",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39dbf")
},
{
"platform" : "amazon",
"channel" : "amazon_jp",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39dbe")
},
{
"platform" : "amazon",
"channel" : "amazon_de",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39dbd")
},
{
"platform" : "amazon",
"channel" : "amazon_es",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39dbc")
},
{
"platform" : "amazon",
"channel" : "amazon_fr",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39dbb")
},
{
"platform" : "amazon",
"channel" : "amazon_it",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39dba")
},
{
"platform" : "ebay",
"channel" : "ebay_au",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39db9")
},
{
"platform" : "ebay",
"channel" : "ebay_de",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39db8")
},
{
"platform" : "ebay",
"channel" : "ebay_es",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39db7")
},
{
"platform" : "ebay",
"channel" : "ebay_fr",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39db6")
},
{
"platform" : "ebay",
"channel" : "ebay_it",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39db5")
},
{
"platform" : "ebay",
"channel" : "ebay_uk",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39db4")
},
{
"platform" : "ebay",
"channel" : "ebay_us",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("59b5f86aaa0ee15555a39db3")
},
{
"platform" : "walmart",
"channel" : "walmart_us",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("5a4d9de2bb1aee844f03e1a6")
},
{
"platform" : "walmart",
"channel" : "walmart_ca",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("5a4d9de2bb1aee844f03e1a5")
},
{
"platform" : "amazon",
"channel" : "amazon_au",
"saleStatus" : "T",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("5abe095bb1d48d194f6187c0")
},
{
"platform" : "amazon",
"channel" : "amazon_in",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("5c9af2776f3dcf04491818f2")
}
],
"statusLevel" : "",
"statusType" : "",
"status" : "A",
"skuId" : "abc001",
"__v" : NumberInt(3)
}
i want to copy:
{
"platform" : "walmart",
"channel" : "walmart_us",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("5a4d9de2bb1aee844f03e1a6")
}
and change the "channel" : "walmart_dsv",the other files is the same like this :
{
"platform" : "walmart",
"channel" : "walmart_dsv",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"_id" : ObjectId("5a4d9de2bb1aee844f03e1a6")
}
and insert into the same document.
i use this command:
db.getCollection("0521").aggregate([
{$unwind: "$channelSales"},
{$project: {platform: "$channelSales.platform",
channel: "$channelSales.channel",
saleStatus: "$channelSales.saleStatus",
type: "$channelSales.type",
url: "$channelSales.url",
remark: "$channelSales.remark",
isCaught: "$channelSales.isCaught",
_id: "$channelSales._id"
}},
{ $match : { "channel" : "amazon_us"} }
]).forEach(function(award_team){
if(award_team != null)
{
db.getCollection("0521").findAndModify(
{
query: {_id: award_team._id},
update: { $push: {channelSales: [ {platform: award_team.platform, channel: "walmart_dsv", saleStatus: award_team.saleStatus, type: award_team.type, url: award_team.url, remark: award_team.remark, isCaught: award_team.isCaught, _id: award_team._id }] } },
upsert: true,
});
}
});
but it is add a new document,what can i do?
You are making a mistake in projection:
You are using sub-document _id to update the main document and you are using option {upsert: true} so every time it is inserting a new document in case it does not find _id.
Update your project as below:
db.collection.aggregate([
{$unwind: "$channelSales"},
{$project: {
_id: "$_id", // Main Document Id
platform: "$channelSales.platform",
channel: "$channelSales.channel",
saleStatus: "$channelSales.saleStatus",
type: "$channelSales.type",
url: "$channelSales.url",
remark: "$channelSales.remark",
isCaught: "$channelSales.isCaught",
channelSalesId: "$channelSales._id" // Sub-Document Id (Caannel Sales Id)
}},
{ $match : { "channel" : "amazon_us"} }
])
Now you will get a response like below:
{
"_id" : ObjectId("59b5e84d71ab5580d643d070"),
"platform" : "amazon",
"channel" : "amazon_us",
"saleStatus" : "A",
"type" : "",
"url" : "",
"remark" : "",
"isCaught" : "0",
"channelSalesId" : ObjectId("59b5f86aaa0ee15555a39dbf")
}
Now you can use "award_team._id" to update Document:
db.getCollection("0521").findAndModify(
{
query: {_id: award_team._id},
update: { $push: {channelSales: [ {platform: award_team.platform, channel: "walmart_dsv", saleStatus: award_team.saleStatus, type: award_team.type, url: award_team.url, remark: award_team.remark, isCaught: award_team.isCaught, _id: award_team._id }] } },
upsert: true,
});
}
});
I have a data set where a documents looks like this.
{
"_id" : ObjectId("5c9873aa0fd62b0c9c84e749"),
"account_id" : "1",
"company_tree" : [
{
"id" : "5d561a07-6d2c-45ae-b249-757aa41e9b2b",
"name" : "dept12",
"privacy_advisor" : {
"name" : "wfewf",
"email" : "fwefw",
"phone" : "fwef"
},
"controller" : {
"name" : "qwqwerf",
"email" : "fwefew",
"phone" : "fwefwe"
},
"index" : 1,
"items" : [
{
"id" : "a2aae8b2-24d1-44ee-98a6-29d78835fcfd",
"name" : "dept3",
"privacy_advisor" : {
"name" : "wfewf",
"email" : "fwefw",
"phone" : "fwef"
},
"controller" : {
"name" : "qwqwerf",
"email" : "fwefew",
"phone" : "fwefwe"
},
"index" : 0,
"users" : [
{
"id" : "1",
"name" : "admin",
"email" : "admin#admin.com",
"primary_locale" : "nb",
"role_membership" : {
"id" : 1,
"active_status" : true,
"name" : "leader"
}
},
{
"id" : "f18ed780-c3c4-4a43-b1e2-f7b5deef2660",
"name" : "user",
"email" : "tromesh#tromesh.com",
"primary_locale" : "nb",
"role_membership" : {
"id" : 1,
"active_status" : true,
"name" : "user"
}
}
],
"items" : [
{
"id" : "83923ba0-8d0f-4c47-8953-0f9a86a7fcbf",
"name" : "dept4",
"privacy_advisor" : {
"name" : "wfewf",
"email" : "fwefw",
"phone" : "fwef"
},
"controller" : {
"name" : "qwqwerf",
"email" : "fwefew",
"phone" : "fwefwe"
},
"index" : 0,
"users" : [
{
"id" : "1",
"name" : "admin",
"email" : "admin#test.com",
"primary_locale" : "nb",
"role_membership" : {
"id" : 1,
"active_status" : true,
"name" : "leader"
}
},
{
"id" : "f18ed780-c3c4-4a43-b1e2-f7b5deef2660",
"name" : "user",
"email" : "tromesh#tromesh.com",
"primary_locale" : "nb",
"role_membership" : {
"id" : 2,
"active_status" : true,
"name" : "user"
}
}
],
"items" : [
{
"id" : "34bfbd43-0d48-4ccf-a1a8-27770ee9048f",
"name" : "dept5",
"privacy_advisor" : {
"name" : "wfewf",
"email" : "fwefw",
"phone" : "fwef"
},
"controller" : {
"name" : "qwqwerf",
"email" : "fwefew",
"phone" : "fwefwe"
},
"index" : 0,
"items" : [
{
"id" : "1b8f13af-ff80-46c1-ad59-df454268b4fd",
"name" : "dept6",
"privacy_advisor" : {
"name" : "wfewf",
"email" : "fwefw",
"phone" : "fwef"
},
"controller" : {
"name" : "qwqwerf",
"email" : "fwefew",
"phone" : "fwefwe"
},
"index" : 0,
"statistics" : {
"insight" : {
"value" : "88"
},
"privacyQuality" : {
"value" : "44"
}
}
}
],
"expanded" : true,
"statistics" : {
"privacyQuality" : {
"value" : "56"
}
}
}
],
"expanded" : true,
"selected" : true,
"isAccount" : true
}
],
"expanded" : true,
"statistics" : {
"insight" : {
"value" : "44"
},
"privacyQuality" : {
"value" : "89"
}
},
"isAccount" : true,
"details" : {
"company_name" : "New Company"
},
"public_id" : "e4f5ed70-3be6-11e9-b57b-ed1bd06820e4",
"internal_id" : "e4f5ed70-3be6-11e9-8fde-3df658dfaac7",
"is_public_link_published" : true,
"theme" : {
"primary" : "#3c99df",
"secondary" : "#5cc1ed",
"tertiary" : "#2176b6"
}
}
],
"expanded" : true,
"statistics" : {
"insight" : {
"value" : "75"
}
}
},
//more objects with nested properties.
];
I need to insert data to "users" array in a object in company_tree array checking the name property ("name" : "dept12"). Each object in company_tree array has nested object in "items". I already have the document _id,user data, name of department ("name" : "dept12").
This is the code that i have tried and im kind a stuck here. It would be greate if someone can guide me on hos to update this.
db.getCollection('organization').find({"_id" : ObjectId("5cdbc8d4a3a4280fe86b9085")},{"company_tree":1,'_id': false}).toArray(function (err, result) {
console.log(result);
};
My query:
let query = recentRef.queryOrderedByChild(FRECENT_GROUPID).queryEqualToValue(group_id)
query.observeSingleEventOfType(.Value, withBlock: { snapshot in
And database structure is :
And my query looks like:
(/Recent {
ep = fb343534ca520c70fe35b0a316ea8e4c;
i = groupId;
sp = fb343534ca520c70fe35b0a316ea8e4c;
})
and getting Snap (Recent) <null> when I print(snapshot).
Its strange that it was working fine but now its suddenly stopped working.
EDIT:
Complete JSON:
{
"Message" : {
"fb343534ca520c70fe35b0a316ea8e4c" : {
"-Kp0jed1EZ5BLllL5_cm" : {
"createdAt" : 1.500046597341153E9,
"groupId" : "fb343534ca520c70fe35b0a316ea8e4c",
"objectId" : "-Kp0jed1EZ5BLllL5_cl",
"senderId" : "lI6SRppSboScWo5xVjcfLL82Ogr2",
"senderName" : "Test1 Test1",
"status" : "",
"text" : "hi",
"type" : "text",
"updatedAt" : 1.50004659734136E9
}
}
},
"Recent" : {
"-Kp0jecwejhzQbbm62CW" : {
"counter" : 0,
"createdAt" : 1.500046600967624E9,
"description" : "Test1 Test1",
"groupId" : "fb343534ca520c70fe35b0a316ea8e4c",
"lastMessage" : "hi",
"members" : [ "lI6SRppSboScWo5xVjcfLL82Ogr2", "fnRvHFpaoDhXqM1se7NoTSiWZIZ2" ],
"objectId" : "-Kp0jecwejhzQbbm62CV",
"picture" : "",
"type" : "private",
"updatedAt" : 1.500046600967647E9,
"userId" : "fnRvHFpaoDhXqM1se7NoTSiWZIZ2"
},
"-Kp0jed-FU1PXt1iPr29" : {
"counter" : 0,
"createdAt" : 1.500046600971885E9,
"description" : "Srikant Root",
"groupId" : "fb343534ca520c70fe35b0a316ea8e4c",
"lastMessage" : "hi",
"members" : [ "lI6SRppSboScWo5xVjcfLL82Ogr2", "fnRvHFpaoDhXqM1se7NoTSiWZIZ2" ],
"objectId" : "-Kp0jed-FU1PXt1iPr28",
"picture" : "https://s3.amazonaws.com/top500golfdev/uploads/profile/srikant.yadav#rootinfosol.com/profilepicture.jpg",
"type" : "private",
"updatedAt" : 1.500046600971896E9,
"userId" : "lI6SRppSboScWo5xVjcfLL82Ogr2"
}
},
"User" : {
"fnRvHFpaoDhXqM1se7NoTSiWZIZ2" : {
"createdAt" : 1.500045753102713E9,
"email" : "srikant.yadav#rootinfosol.com",
"firstname" : "Srikant",
"fullname" : "Srikant Yadav",
"handle" : "Srikant",
"lastname" : "Yadav",
"networkImage" : "https://s3.amazonaws.com/top500golfdev/uploads/profile/srikant.yadav#rootinfosol.com/profilepicture.jpg",
"objectId" : "fnRvHFpaoDhXqM1se7NoTSiWZIZ2",
"online" : false,
"updatedAt" : 1.500045753102731E9
},
"lI6SRppSboScWo5xVjcfLL82Ogr2" : {
"createdAt" : 1.500045791892967E9,
"email" : "test1#gmail.com",
"firstname" : "Test1",
"fullname" : "Test1 Test1",
"handle" : "test1",
"lastname" : "Test1",
"networkImage" : "",
"objectId" : "lI6SRppSboScWo5xVjcfLL82Ogr2",
"online" : false,
"updatedAt" : 1.500046571456235E9
}
}
}
{
"_id" : "WR10005",
"_class" : "com.bioraid.mes.model.WorkOrders",
"rountingNumber" : "R006",
"orderId" : "MR-1017",
"consumables" : "Chip",
"workOrderStatus" : "pending",
"deliveryStatus" : "on time",
"lastCompletedStage" : "testProces",
"dateAssigned" : ISODate("2017-02-17T05:38:57.631Z"),
"dateCompleted" : ISODate("2017-02-17T07:53:32.680Z"),
"opretorProcessStatus" : [
{
"opretorid" : "USER114",
"process" : "testProces",
"status" : "Done",
"workCenterId" : "WC1",
"startDate" : ISODate("2017-02-17T06:30:16.813Z"),
"endDate" : ISODate("2017-02-17T06:42:23.237Z"),
"qualityManagerReview" : {
"qaId" : "",
"status" : "Done",
"note" : ""
}
},
{
"opretorid" : "USER116",
"process" : "testProces",
"status" : "Done",
"workCenterId" : "WC1",
"startDate" : ISODate("2017-02-17T06:30:16.813Z"),
"endDate" : ISODate("2017-02-17T06:42:23.237Z")
}
],
"workOrdersRouting" : {
"testProces" : [
{
"routingStep" : "R006*010",
"operationName" : "Operation1",
"operationStatus" : "Completed",
"operationResult" : "Pass",
"operationNote" : "test"
},
{
"routingStep" : "R006*020",
"operationName" : "adalks",
"operationStatus" : "Completed",
"operationResult" : "Pass",
"operationNote" : "sddsdfsf"
}
],
"Embossing" : [
{
"routingStep" : "R006*030",
"operationName" : "Water Cleaning",
"operationStatus" : "",
"operationResult" : "",
"operationNote" : ""
}
],
"Turbidity" : [
{
"routingStep" : "R006*040",
"operationName" : "Embossing Assembly",
"operationStatus" : "",
"operationResult" : "",
"operationNote" : ""
}
]
}
}
I want to get {'opretorProcessStatus.qualityManagerReview.status' : {$ne : null}} but this is not giving proper result can any one help me to write this.
and other one is db.getCollection('workOrders').find({'opretorProcessStatus.qualityManagerReview.status' : "Done"}).
You can use $elemMatch to get the Document which you need.
To find documents with opretorProcessStatus.qualityManagerReview.status = Done, use this query:
db.getCollection("workOrders").find({"opretorProcessStatus":{"$elemMatch":{""qualityManagerReview.status":"Done"}}})
To find the documents which don't have status as null:
db.getCollection("workOrders").find({"opretorProcessStatus":{"$elemMatch":{""qualityManagerReview.status":{"$ne":null}}}})
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
}}])