How to optimise MongoDB search with indexing? - mongodb

These documents are in my MongoDB collection:
{
"_id" : ObjectId("57f5e1b5c7739d9805edfe85"),
"date" : ISODate("2016-10-01T00:00:00.000Z"),
"query" : "test1",
"page" : "page1/runway/ready-to-wear/ss17/2016/10/christian-dior/",
"clicks" : 17.0,
"impressions" : 161.0,
"ctr" : 0.10559006211180125,
"position" : 8.503105590062113
}
{
"_id" : ObjectId("57f5e1b5c7739d9805edfe86"),
"date" : ISODate("2016-10-01T00:00:00.000Z"),
"query" : "test1",
"page" : "page2/runway/ready-to-wear/ss17/2016/10/christian-dior/",
"clicks" : 17.0,
"impressions" : 161.0,
"ctr" : 0.10559006211180125,
"position" : 8.503105590062113
}
{
"_id" : ObjectId("57f5e1b5c7739d9805edfe87"),
"date" : ISODate("2016-10-01T00:00:00.000Z"),
"query" : "test2",
"page" : "page3/runway/ready-to-wear/ss17/2016/10/christian-dior/",
"clicks" : 17.0,
"impressions" : 161.0,
"ctr" : 0.10559006211180125,
"position" : 8.503105590062113
}
I need to filter and get some documents like these:
{
"_id" : ObjectId("57f5e1b5c7739d9805edfe85"),
"date" : ISODate("2016-10-01T00:00:00.000Z"),
"query" : "test1",
"page" : "page1/runway/ready-to-wear/ss17/2016/10/christian-dior/",
"clicks" : 17.0,
"impressions" : 161.0,
"ctr" : 0.10559006211180125,
"position" : 8.503105590062113
}
{
"_id" : ObjectId("57f5e1b5c7739d9805edfe86"),
"date" : ISODate("2016-10-01T00:00:00.000Z"),
"query" : "test1",
"page" : "page2/runway/ready-to-wear/ss17/2016/10/christian-dior/",
"clicks" : 17.0,
"impressions" : 161.0,
"ctr" : 0.10559006211180125,
"position" : 8.503105590062113
}
I created an index using query field and ran db.query using aggregate() method like this:
{
"aggregate" : "Mycollection Name" ,
"pipeline" : [
{ "$match" : { "$text" : { "$search" : "\"test1\""}}},
{ "$match" : { "date" : { "$gte" : { "$date" : "2016-10- 01T00:00:00.000Z"} ,"$lte" : { "$date" : "2016-10-01T00:00:00.000Z"}}}},
{ "$group" : { "_id" : "$page" , "clicks" : { "$sum" : "$clicks"} , "impressions" : { "$sum" : "$impressions"} , "ctr" : { "$avg" : "$ctr"} , "position" : { "$avg" : "$position"}}} ,
{ "$sort" : { "clicks" : -1}}
]
}
This query is working but it is not return exactly search.
How can I optimize this query?

Related

MongoDB query with multiple conditions and return first matching result

We have the below data in our collection, And only want to return/get the result that matches the first condition in the below query (First matching result without checking for other conditions). Is that possible?
Data
{
"_id" : ObjectId("638073e2ee838e2d2186624b"),
"partnerId" : ObjectId("60ad435d39f1600f7cce8f40"),
"currencyId" : ObjectId("5f7c7f2e13b5c5503cbfe64c"),
"precision" : NumberInt(3),
"version" : NumberLong(1),
"status" : "ACTIVE"
},
{
"_id" : ObjectId("638073e2ee838e2d2186624b"),
"currencyId" : ObjectId("5f7c7f2e13b5c5503cbfe64c"),
"precision" : NumberInt(3),
"version" : NumberLong(1),
"status" : "ACTIVE"
},
{
"_id" : ObjectId("637e0fdc270c9d45d487085a"),
"precision" : NumberInt(3),
"version" : NumberLong(1),
"status" : "ACTIVE"
}
Query:-
db.getCollection("INVENTORY_PARTNER_COST_PRECISION").find(
{
"$or" : [
{ "$and" : [{ "partnerId" : { "$oid" : "60ad435d39f1600f7cce8f40"}}, { "currencyId" : { "$oid" : "5f7c7f2e13b5c5503cbfe64c"}}]},
{ "$and" : [{ "partnerId" : null}, { "currencyId" : { "$oid" : "5f7c7f2e13b5c5503cbfe64c"}}]},
{ "$and" : [{ "partnerId" : null}, { "currencyId" : null}]}
]
}
)

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

Query nested, 2 leves, array of objects in MongoDB

I am trying to query a coolection that has the following structure:
{
"_id" : ObjectId("58eed22d09865610c23453e3"),
"name" : "Maria das Dores",
"type" : "P",
"nickname" : "Dolores",
"notes" : "Notas\r\n\r\nCom quebra de página",
"updated_at" : ISODate("2017-04-13T01:19:41.000Z"),
"created_at" : ISODate("2017-04-13T01:19:41.000Z"),
"emails" : [
{
"value" : "maria#dores.com",
"default" : true,
"updated_at" : ISODate("2017-04-13T01:19:41.000Z"),
"created_at" : ISODate("2017-04-13T01:19:41.000Z"),
"_id" : ObjectId("58eed22d09865610c23453e4")
},
{
"value" : "maria#semdores.com",
"_id" : ObjectId("58eed23d09865605614005c4"),
"updated_at" : ISODate("2017-04-13T01:19:57.000Z"),
"created_at" : ISODate("2017-04-13T01:19:57.000Z")
}
],
"phones" : [
{
"value" : "(33) 8282383-2933",
"default" : false,
"updated_at" : ISODate("2017-04-25T12:11:14.000Z"),
"created_at" : ISODate("2017-04-13T01:19:41.000Z"),
"_id" : ObjectId("58eed22d09865610c23453e5")
},
{
"value" : "(85) 101010-1010101",
"default" : true,
"_id" : ObjectId("58ff3ce209865605681f40c2"),
"updated_at" : ISODate("2017-04-25T12:11:14.000Z"),
"created_at" : ISODate("2017-04-25T12:11:14.000Z")
},
{
"value" : "21343243343",
"_id" : ObjectId("58ff3d7d0986560b861c3b32"),
"updated_at" : ISODate("2017-04-25T12:13:49.000Z"),
"created_at" : ISODate("2017-04-25T12:13:49.000Z")
}
],
"copartner" : {
"enabled" : true,
"updated_at" : ISODate("2017-05-17T00:32:42.000Z"),
"created_at" : ISODate("2017-05-11T02:35:40.000Z"),
"_id" : ObjectId("5913cdfc09865664df031ec2"),
"applications" : {
"0" : {
"application_id" : "58e46443098656283d225b52",
"responsibility" : "DEV",
"percentage" : 1250,
"_id" : ObjectId("591a6f9d0986563c174cefd3"),
"updated_at" : ISODate("2017-05-16T03:18:53.000Z"),
"created_at" : ISODate("2017-05-16T03:18:53.000Z")
},
"1" : {
"application_id" : "58e46443098656283d225b52",
"responsibility" : "SALE",
"percentage" : 2000,
"_id" : ObjectId("591b9a2a09865605697fe3e3"),
"updated_at" : ISODate("2017-05-17T00:32:42.000Z"),
"created_at" : ISODate("2017-05-17T00:32:42.000Z")
}
}
}
}
I already test with the "dot" notation and using $elemMatch, but no one of them result in any match.
Dot notation query:
db.getCollection('persons').find({
"copartner.applications.application_id": "58e46443098656283d225b52"
})
$elemMatch query:
db.getCollection('persons').find({
"copartner.applications": { $elemMatch: { "applications_id": "58e46443098656283d225b52" } }
})
The dot notation will work with either one of these queries:
db.getCollection('persons').find({
"copartner.applications.0.application_id": "58e46443098656283d225b52"})
AND
db.getCollection('persons').find({
"copartner.applications.1.application_id": "58e46443098656283d225b52"})
"applications" is not an array, so $elemMatch is not applicable. "0" and "1" are nested fields instead of array elements. If you can, you're better off using an array for "applications".

Inconsistent query results with embedded documents on MongoDB

I've got a collection called payments with an example of its document shown below:
{
"_id" : ObjectId("579b5ee817e3aaac2f0aebc1"),
"updatedAt" : ISODate("2016-07-29T11:04:01.209-03:00"),
"createdAt" : ISODate("2016-07-29T10:49:28.113-03:00"),
"createdBy" : ObjectId("5763f56010cd7b03008147d4"),
"contract" : ObjectId("578cb907f1575f0300d84d09"),
"recurrence" : [
{
"when" : ISODate("2016-05-29T11:03:45.606-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2d"),
"transaction" : {
"createdAt" : ISODate("2016-05-29T11:03:45.608-03:00"),
"tid" : "9999999999999999B01A",
"status" : 4,
"code" : "00",
"message" : "Transação autorizada"
},
"status" : "PAGO"
},
{
"when" : ISODate("2016-06-29T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2c"),
"transaction" : {
"createdAt" : ISODate("2016-06-29T11:03:45.608-03:00"),
"tid" : "9999999999999999B01A",
"status" : 4,
"code" : "00",
"message" : "Transação autorizada"
},
"status" : "PAGO"
},
{
"when" : ISODate("2016-07-29T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2b"),
"status" : "ERRO",
"transaction" : {
"code" : "56",
"createdAt" : ISODate("2016-07-29T11:04:01.196-03:00"),
"message" : "Autorização negada",
"status" : 5,
"tid" : "1006993069000730B88A"
}
},
{
"when" : ISODate("2016-07-30T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e2a"),
"status" : "PENDENTE"
},
{
"when" : ISODate("2016-07-31T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e29"),
"status" : "PENDENTE"
},
{
"when" : ISODate("2016-08-01T11:03:45.608-03:00"),
"_id" : ObjectId("579b6241ea945e3631f64e28"),
"status" : "PENDENTE"
}
],
"status" : "PAGO",
"conditions" : {
"originalValue" : 7406.64,
"totalValue" : 7400,
"upfrontValue" : 1500,
"upfrontInstallments" : 3,
"balanceInstallments" : 9
},
"__v" : 0,
"transaction" : {
"code" : "00",
"createdAt" : ISODate("2016-07-29T10:49:46.610-03:00"),
"message" : "Transação autorizada",
"status" : 6,
"tid" : "1006993069000730AF5A"
}
}
If I run the query below, I get the desired document shown above:
db.payments.find({ "recurrence.transaction.tid": "1006993069000730B88A" })
However, if I run this other query, MongoDB returns my entire collection (presumably because it didn't match the subdocument's id):
db.payments.find({ "recurrence._id": ObjectId("579b6241ea945e3631f64e2b") })
Both queries should return the same result! I also checked some other questions including this one so unless I'm going crazy I'm doing the same thing. Not sure why the inconsistent results though.
Tryout this:
db.payments.find({ recurrence : { $elemMatch: { "transaction.tid": "1006993069000730B88A"} } }).pretty()

MongoDB : geospatial index not getting added

I have a mongo collection where each document has a 'loc' array as follows:
> db.trucks.findOne()
{
"_id" : ObjectId("52afe2a9e8de3f311ec675ee"),
"objectid" : "427856",
"fooditems" : "Cupcakes",
"facilitytype" : "Truck",
"loc" : [
37.7901490737255,
-122.398658184604
],
"priorpermit" : "0",
"location" : {
"latitude" : "37.7901490874965",
"needs_recoding" : false,
"longitude" : "-122.398658184594"
},
"lot" : "055",
"cnn" : "101000",
"status" : "REQUESTED",
"schedule" : "http://bsm.sfdpw.org/PermitsTracker/reports/report.aspx?title=schedule&report=rptSchedule&params=permit=13MFF-0068&ExportPDF=1&Filename=13MFF-0068_schedule.pdf",
"locationdescription" : "01ST ST: STEVENSON ST to JESSIE ST (21 - 56)",
"latitude" : "37.7901490737255",
"blocklot" : "3708055",
"address" : "50 01ST ST",
"received" : "Mar 14 2013 3:34PM",
"applicant" : "Cupkates Bakery, LLC",
"longitude" : "-122.398658184604",
"expirationdate" : "2013-03-15T00:00:00",
"permit" : "13MFF-0068",
"y" : "2115738.283",
"x" : "6013063.33",
"block" : "3708"
}
When I try to index on 'loc', it doesn't get added:
> db.trucks.ensureIndex( { loc : "2d" } )
> db.trucks.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "food.trucks",
"name" : "_id_"
}
]
What am I doing wrong?
Shouldn't be db.trucks.ensureIndex( { loc : "2d" } )? You are creating indexes on some other collection.