mongodb query explain and index nscanned bigger than nscannedObjects - mongodb

I been having a problem with my query. its not performing well.
its doing like 300ms - 1000ms.
I added index on each of the column. but can't seem to get it right.
sorry forgot to mention the document below is the query for upsert.
there is just an additiona {value.count:1}
document:
{
"_id.browser" : "Facebook",
"_id.c1" : "hpb",
"_id.c2" : "003",
"_id.c3" : "us100_01",
"_id.c4" : "us-18-13a",
"_id.campaign_id" : "47cff2989aef12808a8a596b9b703efc",
"_id.city" : "Edgewood",
"_id.cloaked" : "0",
"_id.country" : "United States",
"_id.date_visited" : "2016-11-16",
"_id.device_name" : "Samsung Galaxy S6",
"_id.device_type" : "mobile",
"_id.domain" : "healthypowerbuzz.com",
"_id.hour_visited" : "10",
"_id.isp" : "AT&T U-verse",
"_id.landingpage_id" : "38a67be59dc9d8f6db81be94b207f2d7",
"_id.netspeed_id" : NumberLong(3),
"_id.offer_id" : [
{
"offer_id" : "cd0d1a0fa7021b99a6f1b630cac64a39"
},
{
"offer_id" : "1f42d6d81d69bf8c9912ae75544eea9c"
}
],
"_id.org" : "AT&T U-verse",
"_id.platform" : "Android",
"_id.server_id" : "b5e04772f2ee237a36f24e8298c3d002",
"_id.set" : "13",
"_id.traffic_account_id" : "cdd60f26f1723f05ee4e964a8e6f43a7",
"_id.week_visited" : "Wednesday",
"value.count" : 100
}
query:
db.tes.update(
{
"_id.browser" : "Facebook",
"_id.c1" : "hpb",
"_id.c2" : "003",
"_id.c3" : "us100_01",
"_id.c4" : "us-18-13a",
"_id.campaign_id" : "47cff2989aef12808a8a596b9b703efc",
"_id.city" : "Edgewood",
"_id.cloaked" : "0",
"_id.country" : "United States",
"_id.date_visited" : "2016-11-16",
"_id.device_name" : "Samsung Galaxy S6",
"_id.device_type" : "mobile",
"_id.domain" : "healthypowerbuzz.com",
"_id.hour_visited" : "10",
"_id.isp" : "AT&T U-verse",
"_id.landingpage_id" : "38a67be59dc9d8f6db81be94b207f2d7",
"_id.netspeed_id" : NumberLong(3),
"_id.offer_id" : [
{
"offer_id" : "cd0d1a0fa7021b99a6f1b630cac64a39"
},
{
"offer_id" : "1f42d6d81d69bf8c9912ae75544eea9c"
}
],
"_id.org" : "AT&T U-verse",
"_id.platform" : "Android",
"_id.server_id" : "b5e04772f2ee237a36f24e8298c3d002",
"_id.set" : "13",
"_id.traffic_account_id" : "cdd60f26f1723f05ee4e964a8e6f43a7",
"_id.week_visited" : "Wednesday"},
{
$inc: { value.count:1 }
},
{ upsert: true }
)
I added a compound index on each of those. because I use it for upsert $inc count: 1. but its really slow.
when I explain I get this. I need help on how to handle this.
"n" : 1,
"nscannedObjects" : 7773,
"nscanned" : 15546,
"nscannedObjectsAllPlans" : 7773,
"nscannedAllPlans" : 15546,

Related

Mongodb agregation. how to enter a list of all hosts who received at least two negative ratings (5 or less) for each review

this is example of document. Now i want to get all host who received at least two negative ratings (5 or less) for each review.
{
"_id" : "5bf31f8c856a12c73c888cee",
"index" : NumberInt(0),
"guid" : "f99844b5-7ecd-4c90-860f-b79a742fcac1",
"isActive" : true,
"registered" : "2012-06-14T02:14:30 -02:00",
"host" : {
"name" : "Grimes",
"surname" : "Terrell",
"gender" : "male",
"age" : NumberInt(55),
"picture" : "https://api.adorable.io/avatars/285/GrimesTerrell.png",
"languages" : [
"spanish",
"slovenian",
"english"
],
"contact" : {
"company" : "QUANTALIA",
"email" : "grimesterrell#quantalia.com",
"phone" : "+1 (987) 474-3543"
},
"reviews" : [
{
"reviewer" : "Mae Ryan",
"date" : "2015-06-01T02:41:46 -02:00",
"helpful" : NumberInt(8),
"kind" : NumberInt(1),
"responsive" : NumberInt(5)
},
{
"reviewer" : "Nixon Johnson",
"date" : "2016-02-08T10:35:12 -01:00",
"helpful" : NumberInt(1),
"kind" : NumberInt(1),
"responsive" : NumberInt(9)
},
{
"reviewer" : "Dorothy Copeland",
"date" : "2016-07-17T12:44:31 -02:00",
"helpful" : NumberInt(3),
"kind" : NumberInt(3),
"responsive" : NumberInt(4)
},
{
"reviewer" : "Kathleen Roth",
"date" : "2014-12-26T04:38:36 -01:00",
"helpful" : NumberInt(10),
"kind" : NumberInt(3),
"responsive" : NumberInt(5)
},
{
"reviewer" : "Hallie Hart",
"date" : "2016-02-14T10:21:50 -01:00",
"helpful" : NumberInt(9),
"kind" : NumberInt(4),
"responsive" : NumberInt(9)
}
]
},
"lodging" : {
"price" : "$86",
"address" : {
"street" : "645 Louisa Street",
"city" : "Fairhaven",
"state" : "Nebraska",
"coordinates" : [
5.439653,
30.761532
]
},
"amenities" : {
"parking" : true,
"petsAllowed" : true,
"airConditioning" : true,
"elevator" : true
},
"reviews" : [
{
"reviewer" : "Susanne Walton",
"date" : "2018-03-20T04:50:18 -01:00",
"cleanliness" : NumberInt(8),
"location" : NumberInt(1),
"food" : NumberInt(10)
},
{
"reviewer" : "Henrietta Manning",
"date" : "2017-12-14T07:05:46 -01:00",
"cleanliness" : NumberInt(10),
"location" : NumberInt(5),
"food" : NumberInt(1)
},
{
"reviewer" : "Newman West",
"date" : "2018-05-07T05:23:55 -02:00",
"cleanliness" : NumberInt(9),
"location" : NumberInt(10),
"food" : NumberInt(6)
},
{
"reviewer" : "Brennan Rowland",
"date" : "2016-09-20T04:49:15 -02:00",
"cleanliness" : NumberInt(10),
"location" : NumberInt(1),
"food" : NumberInt(8)
},
{
"reviewer" : "Beulah Lambert",
"date" : "2016-04-20T11:51:43 -02:00",
"cleanliness" : NumberInt(4),
"location" : NumberInt(8),
"food" : NumberInt(9)
},
{
"reviewer" : "Joseph Mcdowell",
"date" : "2015-02-10T12:14:20 -01:00",
"cleanliness" : NumberInt(7),
"location" : NumberInt(9),
"food" : NumberInt(3)
}
]
}
}
i dont now how to loop trough all host reviews and then check if any of two (helpfull or kind or responsive) are less than 5 and if all have such reviews how to project those to output. i am new to mongodb:)
db.lodging.aggregate([
{
$project: {
'host': 1,
numberOfReviews: { $size: '$host.reviews' },
}
}
])
i tried to and new filed to each review with true or false where false mean that there are no two negative rating and true mean that there are two negative ratings (les than or equal 5)
but if statement dont work corectly
db.lodging.aggregate([
{
$project: {
'host': 1,
steviloKritik: { $size: '$host.reviews' },
}
},
{
$addFields: {
"host.reviews.condition": {
$cond: [{ $lte: ["$host.reviews.helpful", 5] }, true, false]
}
}
},
])

Find element from document

i've passed a mysql database to mongoDB for a project. My db is about a pharmacy. I have a collection of factures, where which has the list of medicines sold. I'm trying to find the medicine which was sold the most.
{
"_id" : ObjectId("5c3c71f2760c4f47c701fe13"),
"cliente" : {
"tlmv" : "910987654",
"nome" : "Josefina Vivida da Paz",
"nif" : "122133144",
"pontos" : NumberLong(0),
"id" : NumberLong(2),
"pass" : "1eab06cab995dfeb32b6b7c709b8a6c62cabacfe",
"email" : "josefina#hotmail.pt"
},
"data_f" : ISODate("2018-06-03T00:00:01Z"),
"data_s" : ISODate("2018-06-02T23:55:59Z"),
"desconto" : 0,
"funcionario" : {
"tlmv" : "934567123",
"nome" : "Pedro Jorge Rito Lima",
"ordenado" : 800.32,
"iban" : "PT 50 2751 3262 76598707612",
"pass" : "3cfa1c281281ffe4f5db2ccfbe7a17f8a9479808",
"niss" : "14385639201",
"id" : NumberLong(2),
"cedula" : "54321"
},
"id" : NumberLong(15),
"id_c" : NumberLong(2),
"id_func" : NumberLong(2),
"medicamentos" : [
{
"categoria" : "Analg�sico",
"receita" : "N",
"des" : "Ben-U-Ron 500",
"qt" : 20,
"formato" : "granulado",
"qt_v" : NumberLong(1),
"pos" : "A12",
"lab" : "Laborat�rio do Rio Ave",
"preco_l" : 2.51,
"un" : "un",
"preco" : 2.51,
"preco_v" : 2.51,
"id" : NumberLong(1),
"stock" : NumberLong(21)
},
{
"categoria" : "Estatina",
"receita" : "S",
"des" : "Sinvastatina",
"qt" : 30,
"formato" : "comprimido",
"qt_v" : NumberLong(1),
"pos" : "K23",
"lab" : "Mylan",
"preco_l" : 16.45,
"un" : "un",
"preco" : 16.45,
"preco_v" : 16.45,
"id" : NumberLong(6),
"stock" : NumberLong(25)
}
],
"pontos_r" : NumberLong(10),
"pontos_u" : NumberLong(0),
"total" : 18.96
}
So my objective is to count every medicine -"medicamento"- sorted by different descriptions-"des". Similiar to Count on mysql. Any ideas how? The code above is abount 1 facture.
You need $unwind to get a medicine per document and then $group with $sum to get count per medicine, try:
db.collection.aggregate([
{
$unwind: "$medicamentos"
},
{
$group: {
_id: "$medicamentos.des",
count: { $sum: 1 }
}
}
])

mongoDb field count query

I have MongoDB collection with the following format:
{
"_id" : ObjectId("5b6b112d1bbd972848b97df4"),
"Game" : [
{
"City" : "Nizhny Novgorod",
"T2N" : "Costa Rica",
"T1N" : "Switzerland",
"ST" : "Nizhny Novgorod Stadium",
"T1S" : "2",
"Date" : "6/27/2018",
"T2S" : "2"
},
{
"City" : "Kaliningrad",
"T2N" : "Serbia",
"T1N" : "Switzerland",
"ST" : "Kaliningrad Stadium",
"T1S" : "2",
"Date" : "6/22/2018",
"T2S" : "1"
},
{
"City" : "Rostov-on-Don",
"T2N" : "Brazil",
"T1N" : "Switzerland",
"ST" : "Rostov Arena",
"T1S" : "1",
"Date" : "6/17/2018",
"T2S" : "1"
},
{
"City" : "Saint Petersburg",
"T2N" : "Sweden",
"T1N" : "Switzerland",
"ST" : "Saint Petersburg Stadium",
"T1S" : "0",
"Date" : "7/3/2018",
"T2S" : "1"
}
],
"team" : "Switzerland"
}
How can I get the total of T1S and Total of T2S for each team?
I am trying the following query
db.test3.aggregate([{$match:{}},{$group:{_id:"$team", goalScored:{$sum:"Game.T1S"},goalConceaded:{$sum:"Game.T2S"}}}])
But I am getting the following output:
{ "_id" : "Tunisia", "goalScored" : 0, "goalConceaded" : 0 }
{ "_id" : "Uruguay", "goalScored" : 0, "goalConceaded" : 0 }
{ "_id" : "Egypt", "goalScored" : 0, "goalConceaded" : 0 }
In the above, the goalscored and goalconceded are always 0 which is not expected. Any help is appreciated.

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" } }
]);

Find records from 7 days ago from now in MongoDB

I've a collection containing objects represented like the one below:
/* 1 */
{
"_id" : ObjectId("551c6605e4c6ac495c923aab"),
"sender_id" : ObjectId("551c6605e4c6ac495c923aac"),
"rep_sender_id" : "38",
"sender_name" : "Melissa Aldridge",
"sender_title" : null,
"sender_photo_id" : "https://url/web/bundles/pdone/media/representative/photo/qlv_aldridge_melissa.jpg",
"sender_company" : "",
"recipient_id" : ObjectId("5356af6ae4c6acdc10450a23"),
"recipient_name" : "MAX GUCILATAR",
"recipient_title" : "",
"recipient_photo_id" : "webdrProfileIcon.png",
"message" : [
{
"message_link" : "20150401174059801537155",
"message_href" : "//url2/pdone/pharma_brandpage?id=20150401174059801537155&brand_id=11",
"message_href_text" : "Co-pay Card Savings Information"
}
],
"message_subject" : "has shared information about ADHD treatment option with you",
"sender_type" : "rep",
"message_type" : "share",
"supplemental" : "",
"reply_ok" : "no",
"mdate" : ISODate("2015-04-01T21:41:25.523Z"),
"date" : "Wed April 1, 2015",
"read" : "no",
"timeago" : "2015-04-01T17:41:25-04:00",
"reference_count" : 1,
"chain" : [],
"rep_header" : {
"paccode" : "PFIZER_EMAIL_SEG1",
"sync" : "",
"_st" : "ejR2NGQ0eDVrNWw1ajVqNXc0ZjN2NG4zNDQ0NDAzeDU2NDY0dTVsMjQ0YTVhMms1NjNuNDIzbDJzNG00YTU2MzU0NTNtNW0yMjQyNDIzbjJhMzA1YjNiNHQ1NTVkNGE0cDNsNHozdzIwM2o0",
"hcp_email_address" : "max.gucilatar#yahoo.com",
"email_target" : "tmb",
"inactive" : "no",
"share_date" : "4-1-2015",
"share_date_2" : ISODate("2015-04-01T09:41:25.000Z"),
"hcp_id" : "135787",
"rep_msg" : "Thank you for taking time to speak with me. Based on our discussion, I’ve attached additional materials about Quillivant XR™ (methylphenidate HCI) CII. I look forward to following up with you in the near future. Thank you.",
"session_id" : "2015040117405239775517",
"rep_id" : "38",
"hcp_fname" : "MAX",
"hcp_lname" : "GUCILATAR",
"address" : "51 OFFICE PARK DRIVE",
"city" : "JACKSONVILLE",
"zip" : "28546",
"program_path" : "https://url/web/reptool/set/",
"rep_fname" : "Melissa",
"rep_lname" : "Aldridge",
"rep_title" : null,
"rep_alias" : "Aldridge,Melissa",
"rep_photo_id" : "https://url/web/bundles/pdone/media/representative/photo/qlv_aldridge_melissa.jpg",
"email_subject" : "Melissa Aldridge has shared information about an ADHD treatment option with you.",
"mongo_id" : "5356af6ae4c6acdc10450a23"
},
"rep_shares" : [
{
"media_show_citation" : "no",
"server_path_to_media" : "https://url/web/bundles/pdone/media/qlv_veeva/brand/presentation/cost_cov-1.pdf",
"company_id" : 6,
"media_citation" : "",
"media_id" : 47,
"media_size" : "10",
"media_title" : "Co-pay%20Card%20Savings%20Information",
"media_description" : "Learn%20how%20eligible%20patients%20can%20save%20with%20the%20Quillivant%20XR%E2%84%A2%20%28methylphenidate%20HCl%29%20CII%20savings%20program.",
"media_type" : "pdf",
"media_icon" : "https://url/web/bundles/pdone/media/qlv_veeva/brand/presentation/cost_cov-1-icon.jpg",
"session_presentation_id" : "20150401174059801537155",
"media_units" : "Pages",
"brand_id" : 11,
"media_expiration_timestamp" : 457393259.2305399800000000
}
]
}
/* 2 */
{
"_id" : ObjectId("551c2b0ee4c6ac495c923a97"),
"sender_id" : ObjectId("551c2b0ee4c6ac495c923a98"),
"rep_sender_id" : "33",
"sender_name" : "Pamela Washington",
"sender_title" : null,
"sender_photo_id" : "https://url/web/bundles/pdone/media/representative/photo/Pamela_Washington.jpg",
"sender_company" : "",
"recipient_id" : ObjectId("53909cb7e4c6acaa18486fb2"),
"recipient_name" : "ALAN WEINTRAUB",
"recipient_title" : "",
"recipient_photo_id" : "webdrProfileIcon.png",
"message" : [
{
"message_link" : "201504011329211223504837",
"message_href" : "//url2/pdone/pharma_brandpage?id=201504011329211223504837&brand_id=11",
"message_href_text" : "Efficacy and Safety Information"
}
],
"message_subject" : "has shared information about ADHD treatment option with you",
"sender_type" : "rep",
"message_type" : "share",
"supplemental" : "",
"reply_ok" : "no",
"mdate" : ISODate("2015-04-01T17:29:50.718Z"),
"date" : "Wed April 1, 2015",
"read" : "no",
"timeago" : "2015-04-01T13:29:50-04:00",
"reference_count" : 1,
"chain" : [],
"rep_header" : {
"paccode" : "PFIZER_EMAIL_SEG1",
"sync" : "",
"_st" : "ejR2NGQ0eDVrNWw1ajVqNXc0ZjN2NG4zNDQ0NDAzeDU2NDY0dTVsMjQ0YTVhMms1NjNuNDIzbDJzNG00YTU2MzU0NTNtNW0yMjQyNDIzbjJhMzA1YjNiNHQ1NTVkNGE0cDNsNHozdzIwM2o0",
"hcp_email_address" : "alan.weintraub#developmentalcba.net",
"email_target" : "tmb",
"inactive" : "no",
"share_date" : "4-1-2015",
"share_date_2" : ISODate("2015-04-01T05:29:50.000Z"),
"hcp_id" : "141274",
"rep_msg" : "Thank you for taking time to speak with me. Based on our discussion, I’ve attached additional materials about Quillivant XR™ (methylphenidate HCI) CII. I look forward to following up with you in the near future. Thank you.",
"session_id" : "20150401132911346416403",
"rep_id" : "33",
"hcp_fname" : "ALAN",
"hcp_lname" : "WEINTRAUB",
"address" : "BEHAVIORAL ASSO., P.C.,STE 120",
"city" : "NORCROSS",
"zip" : "30092",
"program_path" : "https://url/web/reptool/set/",
"rep_fname" : "Pamela",
"rep_lname" : "Washington",
"rep_title" : null,
"rep_alias" : "Washington,Pamela C",
"rep_photo_id" : "https://url/web/bundles/pdone/media/representative/photo/Pamela_Washington.jpg",
"email_subject" : "Pamela Washington has shared information about an ADHD treatment option with you.",
"mongo_id" : "53909cb7e4c6acaa18486fb2"
},
"rep_shares" : [
{
"media_show_citation" : "no",
"server_path_to_media" : "https://url/web/bundles/pdone/media/qlv_veeva/brand/presentation/eff_ramp.pdf",
"company_id" : 6,
"media_citation" : "",
"media_id" : 46,
"media_size" : "10",
"media_title" : "Efficacy%20and%20Safety%20Information",
"media_description" : "An%20overview%20of%20the%20efficacy%20and%20Important%20Safety%20Information%20for%20Quillivant%20XR%E2%84%A2%20%28methylphenidate%20HCl%29%20CII%2C%20including%20clinical%20data%20that%20show%20improvement%20in%20just%2045%20minutes%2C%20with%20efficacy%20at%204%20hours%20%28primary%20endpoint%29%20that%20lasts%20to%2012%20hours%20post-dosing.",
"media_type" : "pdf",
"media_icon" : "https://url/web/bundles/pdone/media/qlv_veeva/brand/presentation/eff_ramp-icon.jpg",
"session_presentation_id" : "201504011329211223504837",
"media_units" : "Pages",
"brand_id" : 11,
"media_expiration_timestamp" : 457378161.7262100000000000
}
]
}
I need to find the records (documents) from the last 7 days based on rep_header.share_date or by rep_header.share_date_2. I'm playing with this code but doesn't work:
var lastWeek = new Date();
lastWeek.setDate(lastWeek.getDate() -7);
db.messages.find({ "rep_header.share_date_2.$date": { $gte: lastWeek}} );
Since I get no results (Fetched 0 record(s)) and as you can see the example document is from 01/04 what is wrong?
What about this:
db.messages.find({ "rep_header.share_date_2": { '$gte': lastWeek }} )