MongoDB How to search on Nested Arrays fo Documents - mongodb

Hi all I'm new to MongoDB and trying to perform a Query on a collection i created the collection looks like this:
{
"_id" : ObjectId("574c55312368e5aa90bbf27b"),
"id" : NumberInt(10000),
"BookieId" : NumberInt(1),
"EventBookieId" : null,
"LeagueId" : NumberInt(214),
"LeagueNameId" : NumberInt(4571),
"Team1Id" : NumberInt(435),
"Team1NameId" : null,
"Team2Id" : NumberInt(430),
"Team2NameId" : null,
"EventDateTime" : ISODate("2013-09-14T13:00:00.000+0000"),
"isNew" : true,
"CountryId" : NumberInt(36),
"isCountry" : true,
"isLeague" : true,
"Gameid" : null,
"Reversed" : false,
"eventId" : NumberInt(10000),
"bets" : [
{
"id" : NumberInt(1),
"EventConnectionId" : NumberInt(10000),
"isCorners" : false,
"DateTime" : ISODate("2013-09-10T08:06:43.333+0000"),
"IsLive" : false,
"Team1Score" : null,
"Team2Score" : null,
"Team1RedCards" : null,
"Team2RedCards" : null,
"odds" : [
]
},
{
"id" : NumberInt(2),
"EventConnectionId" : NumberInt(10000),
"isCorners" : false,
"DateTime" : ISODate("2013-09-10T18:50:26.840+0000"),
"IsLive" : false,
"Team1Score" : NumberInt(0),
"Team2Score" : NumberInt(0),
"Team1RedCards" : null,
"Team2RedCards" : null,
"odds" : [
{
"id" : NumberInt(62282718),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "21968173",
"ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 1.81,
"Odds2" : 2.1236,
"Odds3" : null,
"MaxBet" : NumberInt(2000),
"Bet" : "Hcap",
"BetGamePart" : NumberInt(0),
"Line" : -0.25,
"ID_IDENTITY" : NumberInt(1)
},
{
"id" : NumberInt(62282719),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "21968175",
"ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.04167,
"Odds2" : 1.86,
"Odds3" : null,
"MaxBet" : NumberInt(2000),
"Bet" : "Total",
"BetGamePart" : NumberInt(0),
"Line" : 2.0,
"ID_IDENTITY" : NumberInt(2)
},
{
"id" : NumberInt(62282720),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "21968177",
"ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.16,
"Odds2" : 3.45,
"Odds3" : 3.05,
"MaxBet" : NumberInt(1000),
"Bet" : "X12",
"BetGamePart" : NumberInt(0),
"Line" : null,
"ID_IDENTITY" : NumberInt(3)
},
{
"id" : NumberInt(62282721),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "21968179",
"ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.21951,
"Odds2" : 1.7,
"Odds3" : null,
"MaxBet" : NumberInt(1000),
"Bet" : "Hcap",
"BetGamePart" : NumberInt(1),
"Line" : -0.25,
"ID_IDENTITY" : NumberInt(4)
},
{
"id" : NumberInt(62282722),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "21968180",
"ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.78,
"Odds2" : 3.9,
"Odds3" : 1.99,
"MaxBet" : NumberInt(500),
"Bet" : "X12",
"BetGamePart" : NumberInt(1),
"Line" : null,
"ID_IDENTITY" : NumberInt(5)
},
{
"id" : NumberInt(62282723),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "21968181",
"ScrapingDateTime" : ISODate("2013-09-13T07:39:15.810+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 1.93,
"Odds2" : 1.95,
"Odds3" : null,
"MaxBet" : NumberInt(1000),
"Bet" : "Total",
"BetGamePart" : NumberInt(1),
"Line" : 0.75,
"ID_IDENTITY" : NumberInt(6)
},
{
"id" : NumberInt(62290838),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "14382209",
"ScrapingDateTime" : ISODate("2013-09-13T14:38:11.303+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 1.81,
"Odds2" : 2.1236,
"Odds3" : null,
"MaxBet" : NumberInt(2000),
"Bet" : "Hcap",
"BetGamePart" : NumberInt(0),
"Line" : -0.25,
"ID_IDENTITY" : NumberInt(7)
},
{
"id" : NumberInt(62290839),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "14382211",
"ScrapingDateTime" : ISODate("2013-09-13T08:27:39.600+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.05263,
"Odds2" : 1.85,
"Odds3" : null,
"MaxBet" : NumberInt(2000),
"Bet" : "Total",
"BetGamePart" : NumberInt(0),
"Line" : 2.0,
"ID_IDENTITY" : NumberInt(8)
},
{
"id" : NumberInt(62290840),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "14382213",
"ScrapingDateTime" : ISODate("2013-09-13T09:29:22.863+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.15,
"Odds2" : 3.45,
"Odds3" : 3.1,
"MaxBet" : NumberInt(1000),
"Bet" : "X12",
"BetGamePart" : NumberInt(0),
"Line" : null,
"ID_IDENTITY" : NumberInt(9)
},
{
"id" : NumberInt(62290841),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "14382215",
"**ScrapingDateTime" : ISODate**("2013-09-13T14:38:11.303+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.21951,
"Odds2" : 1.7,
"Odds3" : null,
"MaxBet" : NumberInt(1000),
"Bet" : "Hcap",
"BetGamePart" : NumberInt(1),
"Line" : -0.25,
"ID_IDENTITY" : NumberInt(10)
},
{
"id" : NumberInt(62290842),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "14382216",
"ScrapingDateTime" : ISODate("2013-09-13T14:38:11.303+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.78,
"Odds2" : 3.9,
"Odds3" : 1.99,
"MaxBet" : NumberInt(500),
"Bet" : "X12",
"BetGamePart" : NumberInt(1),
"Line" : null,
"ID_IDENTITY" : NumberInt(11)
},
{
"id" : NumberInt(62290843),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "14382217",
"ScrapingDateTime" : ISODate("2013-09-13T14:38:11.303+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 1.93,
"Odds2" : 1.95,
"Odds3" : null,
"MaxBet" : NumberInt(1000),
"Bet" : "Total",
"BetGamePart" : NumberInt(1),
"Line" : 0.75,
"ID_IDENTITY" : NumberInt(12)
},
{
"id" : NumberInt(62381139),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "20864356",
"ScrapingDateTime" : ISODate("2013-09-13T14:38:11.303+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.05263,
"Odds2" : 1.85,
"Odds3" : null,
"MaxBet" : NumberInt(2000),
"Bet" : "Total",
"BetGamePart" : NumberInt(0),
"Line" : 2.0,
"ID_IDENTITY" : NumberInt(13)
},
{
"id" : NumberInt(62381140),
"BetId" : NumberInt(2),
"BookieId" : NumberInt(1),
"BookieBetId" : "20864358",
"ScrapingDateTime" : ISODate("2013-09-13T14:38:11.303+0000"),
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.15,
"Odds2" : 3.45,
"Odds3" : 3.1,
"MaxBet" : NumberInt(1000),
"Bet" : "X12",
"BetGamePart" : NumberInt(0),
"Line" : null,
"ID_IDENTITY" : NumberInt(14)
}
]
}
]
and i would like to get all "Odds.ScrapingDateTime between dates, cant understand how to do it, any body can help?

this may help you..
db.test.find({'bets.odds.ScrapingDateTime':{'$gte':ISODate("2013-09-13T14:38:11.303Z"),
'$lt':ISODate("2013-09-14T14:38:11.303Z")}},{"bets.odds.ScrapingDateTime":1,_id:0}).pretty()

Related

Filter on a mongo document and project specific fields

I am quite a beginner in MongoDb. Tried a lot but stuck on one query.
Please find a sample mongo document on the given below:
{
"_id" : ObjectId("5dc158a60566e14c5190db72"),
"hotel_id" : NumberInt(45),
"plans" : [
{
"plan_type" : "AP",
"sub_plans" : [
{
"channels" : [
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : 20.0,
"ep_price_reduction" : null,
"salesforce_id" : "a0Y280000023LFwEAM",
"breakfast_start_time" : null,
"breakfast_end_time" : null,
"lunch_start_time" : null,
"lunch_end_time" : null,
"dinner_start_time" : null,
"dinner_end_time" : null,
"created_at" : NumberLong(1468577718000),
"updated_at" : NumberLong(1481074321000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : NumberInt(400),
"non_veg_price" : null,
"additional_non_veg_price" : null,
"discount" : null
}
]
},
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : 20.0,
"ep_price_reduction" : null,
"salesforce_id" : "a0W28000004GqL4",
"breakfast_start_time" : "7:30",
"breakfast_end_time" : "10:00",
"lunch_start_time" : "12:30",
"lunch_end_time" : "15:00",
"dinner_start_time" : "19:00",
"dinner_end_time" : "22:30",
"created_at" : NumberLong(1505516604000),
"updated_at" : NumberLong(1505516604000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : NumberInt(425),
"non_veg_price" : NumberInt(425),
"additional_non_veg_price" : 0.0,
"discount" : null
}
]
},
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : 20.0,
"ep_price_reduction" : null,
"salesforce_id" : "a0W28000004GqL4",
"breakfast_start_time" : "7:30",
"breakfast_end_time" : "10:00",
"lunch_start_time" : "12:30",
"lunch_end_time" : "15:00",
"dinner_start_time" : "19:00",
"dinner_end_time" : "22:30",
"created_at" : NumberLong(1505709978000),
"updated_at" : NumberLong(1542162045000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : NumberInt(425),
"non_veg_price" : NumberInt(425),
"additional_non_veg_price" : 0.0,
"discount" : null
}
]
}
],
"sub_plan_type" : "Standard",
"is_sub_plan_default" : false
}
]
},
{
"plan_type" : "EP",
"sub_plans" : [
{
"channels" : [
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : null,
"ep_price_reduction" : NumberInt(80),
"salesforce_id" : "a0Y280000023LG1EAM",
"breakfast_start_time" : null,
"breakfast_end_time" : null,
"lunch_start_time" : null,
"lunch_end_time" : null,
"dinner_start_time" : null,
"dinner_end_time" : null,
"created_at" : NumberLong(1468577718000),
"updated_at" : NumberLong(1481074321000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : null,
"non_veg_price" : null,
"additional_non_veg_price" : null,
"discount" : null
}
]
}
],
"sub_plan_type" : "Standard",
"is_sub_plan_default" : false
}
]
},
{
"plan_type" : "MAP",
"sub_plans" : [
{
"channels" : [
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : 20.0,
"ep_price_reduction" : null,
"salesforce_id" : "a0Y280000023LG6EAM",
"breakfast_start_time" : null,
"breakfast_end_time" : null,
"lunch_start_time" : null,
"lunch_end_time" : null,
"dinner_start_time" : null,
"dinner_end_time" : null,
"created_at" : NumberLong(1468577718000),
"updated_at" : NumberLong(1481074321000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : NumberInt(200),
"non_veg_price" : null,
"additional_non_veg_price" : null,
"discount" : null
}
]
},
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : 20.0,
"ep_price_reduction" : null,
"salesforce_id" : "a0W28000004GqL4",
"breakfast_start_time" : "7:30",
"breakfast_end_time" : "10:00",
"lunch_start_time" : "12:30",
"lunch_end_time" : "15:00",
"dinner_start_time" : "19:00",
"dinner_end_time" : "22:30",
"created_at" : NumberLong(1505709978000),
"updated_at" : NumberLong(1541476001000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : NumberInt(225),
"non_veg_price" : NumberInt(225),
"additional_non_veg_price" : 0.0,
"discount" : null
}
]
}
],
"sub_plan_type" : "Standard",
"is_sub_plan_default" : false
}
]
}
]
}
I want only selected fields if:
hotel id is 45, price of occupancy 1, is_active is false and status is Pending and plan type is in ['AP', 'EP'].
Desired output:
{
"_id" : ObjectId("5dc158a60566e14c5190db72"),
"hotel_id" : NumberInt(45),
"plans" : [
{
"plan_type" : "AP",
"sub_plans" : [
{
"channels" : [
{
"channel_name" : "default",
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"prices" : [
{
"veg_price" : NumberInt(425)
}
]
},
{
"channel_name" : "default",
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"prices" : [
{
"veg_price" : NumberInt(425)
}
]
}
],
"sub_plan_type" : "Standard",
"is_sub_plan_default" : false
}
]
},
{
"plan_type" : "EP",
"sub_plans" : [
{
"channels" : [
{
"channel_name" : "default",
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"prices" : [
{
"veg_price" : null,
}
]
}
],
"sub_plan_type" : "Standard",
"is_sub_plan_default" : false
}
]
},
]
}
My Query:
db.collection.find({
"hotel_id": 45,
"plans.plan_type": {$in : ["AP", "EP"]},
"plans.sub_plans.channels.prices.occupancy": 1,
"plans.sub_plans.channels.is_active": false,
"plans.sub_plans.channels.status": "Pending"
})
Note - I need to search in only one document where hotel id is 45.
Please help.
Thanks in advance.
You may be looking for a query like this.
db.collection.aggregate([
{
"$unwind": "$plans",
},
{
"$group": {
"_id": "$hotel_id",
"hotel_id": {
"$first": "$hotel_id"
},
"plans": {
"$push": {
"$cond": [
{
"$or": [
{
"$eq": [
"$plans.plan_type",
"AP"
]
},
{
"$eq": [
"$plans.plan_type",
"EP"
]
}
]
},
{
"plan_type": "$plans.plan_type",
"sub_plans": "$plans.sub_plans",
"sub_plan_type": "$plans.sub_plan_type",
"is_sub_plan_default": "$is_sub_plan_default"
},
null
]
}
}
}
},
{
"$project": {
"_id": 1,
"hotel_id": 1,
"plans": {
"$filter": {
input: "$plans",
as: "plan",
cond: {
$ne: [
"$$plan",
null
]
}
}
}
}
}
])
You need to apply aggregation.
db.collection_name.aggregate([
{
"$match":{
"hotel_id": 45,
"plans.plan_type": "AP",
"plans.sub_plans.channels.prices.occupancy": 1,
"plans.sub_plans.channels.is_active": false,
"plans.sub_plans.channels.status": "Pending"
}
},
{
"$project":{
"_id":"$_id",
"hotel_id":"$hotel_id",
"plans":"plans"
}
}
])

Is there a way to use $Set in inside a $map in a projection

I m trying to update a value inside a map in projection. How can i update a field inside the map? I m trying to do calculation on chargeAmount which will be updated in the loop.
I m using MongoDB shell version v4.0.6 in ubuntu 18.04
{
$project : {
"initialAmount": "$chargeAmount",
"taxedData":{
$map:
{
input: "$rateTaxLevel",
as: "taxIds",
in: {
$map:
{
input: "$taxValidity",
as: "levels",
in: {
//$add: [ "$$taxIds.taxid", "$$levels.taxid" ]
taxingAmount:
{
$cond: [ { $eq: [ "$$taxIds.taxid", "$$levels.taxid" ] },
{ $divide: [ "initialAmount",{ $add: [ 1, "$$levels.tax_validityval" ]} ]
//"$$levels.tax_validityval"
} ,"$$REMOVE"
]
},
"chargeAmount" : "$taxingAmount"
}
}
}
}
}
}
}
data set which used in the project stage
{
"_id" : ObjectId("5caae957beca0f79bddc46d9"),
"uuid" : "urn:uuid:dbc692c7-898c-42c0-ae4d-73f03ed39434:0001PAYMENT93",
"responseTime" : NumberLong(1552589459734),
"direction" : "nb",
"api" : "payment",
"apiID" : "payment_v1",
"applicationName" : "buddhitest",
"applicationId" : NumberInt(93),
"serviceProvider" : "buddhinipun#carbon.super",
"serviceProviderId" : "buddhinipun#carbon.super",
"operatorName" : "OPERATOR1",
"operatorId" : NumberInt(1),
"operation" : "Charge",
"operatorRef" : "ABC-11111113589301",
"chargeAmount" : NumberInt(100),
"category" : "111",
"subcategory" : "222",
"merchant" : "111",
"responseCount" : "1",
"rateCard" : "common_percentage",
"price" : -9.1,
"year" : NumberInt(2019),
"month" : NumberInt(3),
"day" : NumberInt(14),
"hour" : NumberInt(19),
"status" : NumberInt(0),
"statusInfo" : "ok",
"userType" : " Postpaid",
"subRateNB" : [
{
"_id" : ObjectId("5cac45a037c54a0f4ce2b717"),
"sub_rate_nbid" : NumberInt(17),
"api_operationid" : NumberInt(1),
"api_version" : "v1",
"applicationid" : NumberInt(93),
"rate_defid" : NumberInt(30),
"sub_rate_nbactdate" : null,
"sub_rate_nbdisdate" : null,
"createdby" : null,
"createddate" : ISODate("2019-01-16T18:31:18.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2019-01-16T18:31:18.000+0000")
}
],
"rateTaxLevel" : [
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a7"),
"rate_taxesid" : NumberInt(177),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(40),
"level" : NumberInt(1)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a8"),
"rate_taxesid" : NumberInt(178),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(41),
"level" : NumberInt(2)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a9"),
"rate_taxesid" : NumberInt(179),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(1),
"level" : NumberInt(3)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7aa"),
"rate_taxesid" : NumberInt(180),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(66),
"level" : NumberInt(4)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7ab"),
"rate_taxesid" : NumberInt(181),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(68),
"level" : NumberInt(5)
}
],
"taxValidity" : [
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7be"),
"idtax_validityid" : NumberInt(1),
"tax_validityactdate" : ISODate("2018-10-09T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2020-10-09T18:30:00.000+0000"),
"tax_validityval" : 0.02,
"taxid" : NumberInt(1),
"createdby" : null,
"createddate" : ISODate("2018-10-26T13:05:58.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-26T13:05:58.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d5"),
"idtax_validityid" : NumberInt(27),
"tax_validityactdate" : ISODate("2018-10-29T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2035-11-10T18:30:00.000+0000"),
"tax_validityval" : 0.5,
"taxid" : NumberInt(40),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:31:40.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:31:40.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d6"),
"idtax_validityid" : NumberInt(28),
"tax_validityactdate" : ISODate("2018-10-31T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-11-07T18:30:00.000+0000"),
"tax_validityval" : 2.0,
"taxid" : NumberInt(41),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:42:20.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:42:20.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ec"),
"idtax_validityid" : NumberInt(50),
"tax_validityactdate" : ISODate("2018-11-11T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-11-30T18:30:00.000+0000"),
"tax_validityval" : 0.23,
"taxid" : NumberInt(66),
"createdby" : null,
"createddate" : ISODate("2018-11-12T20:22:47.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-12T20:22:47.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ee"),
"idtax_validityid" : NumberInt(52),
"tax_validityactdate" : ISODate("2018-11-30T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-12-07T18:30:00.000+0000"),
"tax_validityval" : 0.01,
"taxid" : NumberInt(68),
"createdby" : null,
"createddate" : ISODate("2018-11-26T15:02:51.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-26T15:02:51.000+0000")
}
]
}
// ----------------------------------------------
{
"_id" : ObjectId("5caae957beca0f79bddc46da"),
"uuid" : "urn:uuid:a566d97b-8032-47d2-aef3-ad5807af3742:0001PAYMENT93",
"responseTime" : NumberLong(1552895806695),
"direction" : "nb",
"api" : "payment",
"apiID" : "payment_v1",
"applicationName" : "buddhitest",
"applicationId" : NumberInt(93),
"serviceProvider" : "buddhinipun#carbon.super",
"serviceProviderId" : "buddhinipun#carbon.super",
"operatorName" : "OPERATOR1",
"operatorId" : NumberInt(1),
"operation" : "Charge",
"operatorRef" : "ABC-123",
"chargeAmount" : NumberInt(10),
"category" : "",
"subcategory" : "",
"merchant" : "",
"responseCount" : "1",
"rateCard" : "common_percentage",
"price" : -0.91,
"year" : NumberInt(2019),
"month" : NumberInt(3),
"day" : NumberInt(18),
"hour" : NumberInt(8),
"status" : NumberInt(0),
"statusInfo" : "ok",
"userType" : " Postpaid",
"subRateNB" : [
{
"_id" : ObjectId("5cac45a037c54a0f4ce2b717"),
"sub_rate_nbid" : NumberInt(17),
"api_operationid" : NumberInt(1),
"api_version" : "v1",
"applicationid" : NumberInt(93),
"rate_defid" : NumberInt(30),
"sub_rate_nbactdate" : null,
"sub_rate_nbdisdate" : null,
"createdby" : null,
"createddate" : ISODate("2019-01-16T18:31:18.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2019-01-16T18:31:18.000+0000")
}
],
"rateTaxLevel" : [
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a7"),
"rate_taxesid" : NumberInt(177),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(40),
"level" : NumberInt(1)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a8"),
"rate_taxesid" : NumberInt(178),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(41),
"level" : NumberInt(2)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a9"),
"rate_taxesid" : NumberInt(179),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(1),
"level" : NumberInt(3)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7aa"),
"rate_taxesid" : NumberInt(180),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(66),
"level" : NumberInt(4)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7ab"),
"rate_taxesid" : NumberInt(181),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(68),
"level" : NumberInt(5)
}
],
"taxValidity" : [
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7be"),
"idtax_validityid" : NumberInt(1),
"tax_validityactdate" : ISODate("2018-10-09T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2020-10-09T18:30:00.000+0000"),
"tax_validityval" : 0.02,
"taxid" : NumberInt(1),
"createdby" : null,
"createddate" : ISODate("2018-10-26T13:05:58.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-26T13:05:58.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d5"),
"idtax_validityid" : NumberInt(27),
"tax_validityactdate" : ISODate("2018-10-29T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2035-11-10T18:30:00.000+0000"),
"tax_validityval" : 0.5,
"taxid" : NumberInt(40),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:31:40.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:31:40.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d6"),
"idtax_validityid" : NumberInt(28),
"tax_validityactdate" : ISODate("2018-10-31T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-11-07T18:30:00.000+0000"),
"tax_validityval" : 2.0,
"taxid" : NumberInt(41),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:42:20.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:42:20.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ec"),
"idtax_validityid" : NumberInt(50),
"tax_validityactdate" : ISODate("2018-11-11T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-11-30T18:30:00.000+0000"),
"tax_validityval" : 0.23,
"taxid" : NumberInt(66),
"createdby" : null,
"createddate" : ISODate("2018-11-12T20:22:47.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-12T20:22:47.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ee"),
"idtax_validityid" : NumberInt(52),
"tax_validityactdate" : ISODate("2018-11-30T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-12-07T18:30:00.000+0000"),
"tax_validityval" : 0.01,
"taxid" : NumberInt(68),
"createdby" : null,
"createddate" : ISODate("2018-11-26T15:02:51.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-26T15:02:51.000+0000")
}
]
}
// ----------------------------------------------
I got this result using java script function
"------------------------------------"
"l1Tax% : 1.02"
"l1Tax : 98.0392156862745"
"l2Tax% : 1.5"
"l2Tax : 65.359477124183"
"l3Tax% : 3"
"l3Tax : 21.78649237472767"
"l4Tax% : 1.23"
"l4Tax : 17.712595426607862"
"l5Tax% : 1.01"
"l5Tax : 17.537223194661248"
"------------------------------------"
"l1Tax% : 1.02"
"l1Tax : 9.803921568627452"
"l2Tax% : 1.5"
"l2Tax : 6.535947712418301"
"l3Tax% : 3"
"l3Tax : 2.1786492374727673"
"l4Tax% : 1.23"
"l4Tax : 1.7712595426607864"
"l5Tax% : 1.01"
"l5Tax : 1.7537223194661251"
using the below java script function, I was able to get the desired output but there is significant performance drop in the java script approach.
forEach(function (doc) {
var taxableamount = doc.chargeAmount;
var taxedAmmount =0;
printjson("------------------------------------");
for (index = 0; index < doc.rateTaxLevel.length; index++) {
for (index = 0; index < doc.taxValidity.length; index++) {
taxedAmmount = taxableamount / (1+doc.taxValidity[index].tax_validityval);
taxableamount = taxedAmmount;
printjson("l"+doc.rateTaxLevel[index].level+"Tax% : "+(1+doc.taxValidity[index].tax_validityval));
printjson("l"+doc.rateTaxLevel[index].level+"Tax : "+taxableamount);
}
}
}

How to retrieve document from collection if the ObjectId is null?

I have two collections FormSubmissions and SubmissionLogs
FormSubmissions schema has following record
{
"_id" : ObjectId("5b56bc8b9b16f0172828f951"),
"LanguageId" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
"FolderName" : "89cd1929-bff9-9a6c-a60b-08bec5966d13",
"IsSync" : false,
"UpdatedBy" : 0,
"IsCompleted" : true,
"IsDeleted" : false,
"EmployeeId" : ObjectId("5ac5b80b19e1011e64766698"),
"Longitude" : null,
"Latitude" : null,
"DeviceId" : "null",
"AppInfoId" : null,
"__v" : 0
}{
"_id" : ObjectId("5b56c06a9b16f0172828f956"),
"LanguageId" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
"FolderName" : "05f79cc4-ae85-7bab-4882-9dac416063f9",
"IsSync" : false,
"UpdatedBy" : 0,
"IsCompleted" : true,
"IsDeleted" : false,
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"Longitude" : null,
"Latitude" : null,
"DeviceId" : "null",
"AppInfoId" : null,
"__v" : 0
}{
"_id" : ObjectId("5b56ef414efb640d104105f9"),
"LanguageId" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
"FolderName" : "d7219aff-3b8d-2931-8ca7-b790e0d1b1f4",
"IsSync" : false,
"UpdatedBy" : 0,
"IsCompleted" : true,
"IsDeleted" : false,
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"Longitude" : null,
"Latitude" : null,
"DeviceId" : "null",
"AppInfoId" : null,
"__v" : 0
}{
"_id" : ObjectId("5b56ef4d4efb640d104105fd"),
"LanguageId" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
"FolderName" : "ca213664-976e-8c5d-6d3c-272931ad2dfa",
"IsSync" : false,
"UpdatedBy" : 0,
"IsCompleted" : true,
"IsDeleted" : false,
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"Longitude" : null,
"Latitude" : null,
"DeviceId" : "null",
"AppInfoId" : null,
"__v" : 0
}{
"_id" : ObjectId("5b595c2999c26d061ceff3d1"),
"LanguageId" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
"FolderName" : "cde6767f-ebf2-6892-97d8-d8c9339b389d",
"IsSync" : false,
"UpdatedBy" : 0,
"IsCompleted" : true,
"IsDeleted" : false,
"EmployeeId" : null,
"Longitude" : null,
"Latitude" : null,
"DeviceId" : "null",
"AppInfoId" : null,
"__v" : 0
}
SubmissionLogs schema has following record
{
"_id" : ObjectId("5b56bc8c9b16f0172828f954"),
"FormId" : ObjectId("5b56bbb19b16f0172828f948"),
"SubmissionId" : ObjectId("5b56bc8b9b16f0172828f951"),
"EmployeeId" : ObjectId("5ac5b80b19e1011e64766698"),
"UpdatedOn" : ISODate("2018-07-24T05:43:40.290Z"),
"CreatedOn" : ISODate("2018-07-24T05:43:40.290Z"),
"__v" : 0
}{
"_id" : ObjectId("5b56c06b9b16f0172828f959"),
"FormId" : ObjectId("5b56bbb19b16f0172828f948"),
"SubmissionId" : ObjectId("5b56c06a9b16f0172828f956"),
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"UpdatedOn" : ISODate("2018-07-24T06:00:11.449Z"),
"CreatedOn" : ISODate("2018-07-24T06:00:11.449Z"),
"__v" : 0
}{
"_id" : ObjectId("5b56ef414efb640d104105fc"),
"FormId" : ObjectId("5b56bbb19b16f0172828f948"),
"SubmissionId" : ObjectId("5b56ef414efb640d104105f9"),
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"UpdatedOn" : ISODate("2018-07-24T09:20:01.930Z"),
"CreatedOn" : ISODate("2018-07-24T09:20:01.930Z"),
"__v" : 0
}{
"_id" : ObjectId("5b56ef4d4efb640d10410600"),
"FormId" : ObjectId("5b56bbb19b16f0172828f948"),
"SubmissionId" : ObjectId("5b56ef4d4efb640d104105fd"),
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"UpdatedOn" : ISODate("2018-07-24T09:20:13.948Z"),
"CreatedOn" : ISODate("2018-07-24T09:20:13.948Z"),
"__v" : 0
}{
"_id" : ObjectId("5b595c2999c26d061ceff3d4"),
"FormId" : ObjectId("5b56bbb19b16f0172828f948"),
"SubmissionId" : ObjectId("5b595c2999c26d061ceff3d1"),
"EmployeeId" : null,
"UpdatedOn" : ISODate("2018-07-26T05:29:13.991Z"),
"CreatedOn" : ISODate("2018-07-26T05:29:13.991Z"),
"__v" : 0
}
I want only that SubmissionLogs which i'm passing EmployeeId (in array-null also)
for that i'm doing this,
db.FormSubmissions.aggregate(
[{
$match : {
$and : [{
FormId : ObjectId("5b56bbb19b16f0172828f948")
}, {
IsDeleted : false
}
]
}
}, {
$lookup : {
from : "SubmissionLogs",
localField : "_id",
foreignField : "SubmissionId",
as : "SubmissionLogs"
}
}, {
$addFields : {
SubmissionLog : {
$filter : {
input : "$SubmissionLogs",
as : "submissionLogs_field",
cond : {
$setIsSubset : [
["$$submissionLogs_field.EmployeeId"],
["5b56bc429b16f0172828f94d", null]
]
}
}
}
}
}, {
$project : {
SubmissionLog : "$SubmissionLog"
}
}
]).pretty()
I want only that records which have ["5b56bc429b16f0172828f94d", null] EmployeeId in SubmissionLogs.I also want that records if EmployeeId has null.
It's working fine $group $push $in
db.FormSubmissions.aggregate([
{
$match : { IsDeleted : false}
},
{
$lookup : {
from : "SubmissionLogs",
localField : "_id",
foreignField : "SubmissionId",
as : "SubmissionLogs"
}
},
{
"$unwind" : "$SubmissionLogs"
},
{
"$match" : {"SubmissionLogs.EmployeeId" : {"$in" : [ObjectId("5b56bc429b16f0172828f94d"), null]}}
},
{"$group" : {"_id" : "$_id",SubmissionLog : {"$push" : "$SubmissionLogs"}}}
])

How to merge records in mongodb with aggregate

I have a collection named "timestamp" and each time_in, time_out, break_start, and break_end has separate record.
What i want is to merge records with the same date.
From this:
Unmerged Data:
{
"_id" : ObjectId("5a94f3e461613268eaecc59c"),
"staff_id" : NumberInt(173),
"time_in" : ISODate("2018-02-27T00:40:00.000+0000"),
"time_out" : null,
"break_start" : null,
"break_end" : null,
"break_type" : null,
"train_delay" : true,
"createdAt" : ISODate("2018-02-27T00:40:00.000+0000"),
"updatedAt" : ISODate("2018-02-27T00:40:00.000+0000"),
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5a94f411e5e2d568f1dae527"),
"staff_id" : NumberInt(173),
"time_in" : null,
"time_out" : null,
"break_start" : ISODate("2018-02-27T04:00:00.000+0000"),
"break_end" : null,
"break_type" : "60m",
"train_delay" : false,
"createdAt" : ISODate("2018-02-27T04:00:00.000+0000"),
"updatedAt" : ISODate("2018-02-27T04:00:00.000+0000"),
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5a94f41ae5e2d568f1dae528"),
"staff_id" : NumberInt(173),
"time_in" : null,
"time_out" : null,
"break_start" : null,
"break_end" : ISODate("2018-02-27T04:59:00.000+0000"),
"break_type" : null,
"train_delay" : false,
"createdAt" : ISODate("2018-02-27T04:59:00.000+0000"),
"updatedAt" : ISODate("2018-02-27T04:59:00.000+0000"),
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5a94f421e5e2d568f1dae529"),
"staff_id" : NumberInt(173),
"time_in" : null,
"time_out" : ISODate("2018-02-27T10:05:00.000+0000"),
"break_start" : null,
"break_end" : null,
"break_type" : null,
"train_delay" : false,
"createdAt" : ISODate("2018-02-27T10:05:00.000+0000"),
"updatedAt" : ISODate("2018-02-27T10:05:00.000+0000"),
"__v" : NumberInt(0)
}
Here's the image of:
Not yet merge records.
Into this:
Merged Data:
{
"_id" : ObjectId("5a94f3e461613268eaecc59c"),
"staff_id" : NumberInt(173),
"time_in" : {
"_id" : ObjectId("5a94f3e461613268eaecc59c"),
"staff_id" : NumberInt(173),
"time_in" : ISODate("2018-02-27T00:40:00.000+0000"),
"time_out" : ISODate("2018-02-27T10:05:00.000+0000"),
"break_start" : ISODate("2018-02-27T04:00:00.000+0000"),
"break_end" : ISODate("2018-02-27T04:59:00.000+0000"),
"break_type" : null,
"train_delay" : true,
"createdAt" : ISODate("2018-02-27T00:40:00.000+0000"),
"updatedAt" : ISODate("2018-02-27T00:40:00.000+0000"),
"__v" : NumberInt(0)
}
Here's the image of:
Merged records

MongoDB search by DateTime type not working

Hi Im new to Mongo DB and trying to start with it, i created a collection and inserted this document into it
{
"_id" : ObjectId("574ad7c42368e58f8c07a47d"),
"id" : 10000,
"BookieId" : 1,
"EventBookieId" : null,
"LeagueId" : 214,
"LeagueNameId" : 4571,
"Team1Id" : 435,
"Team1NameId" : null,
"Team2Id" : 430,
"Team2NameId" : null,
"EventDateTime" : "2013-09-14T14:00:00",
"isNew" : true,
"CountryId" : 36,
"isCountry" : true,
"isLeague" : true,
"Gameid" : null,
"Reversed" : false,
"eventId" : 10000,
"bets" : [
{
"id" : 1,
"EventConnectionId" : 10000,
"isCorners" : false,
"DateTime" : "2013-09-10T09:06:43.333",
"IsLive" : false,
"Team1Score" : null,
"Team2Score" : null,
"Team1RedCards" : null,
"Team2RedCards" : null,
"odds" : []
},
{
"id" : 2,
"EventConnectionId" : 10000,
"isCorners" : false,
"DateTime" : "2013-09-10T19:50:26.84",
"IsLive" : false,
"Team1Score" : 0,
"Team2Score" : 0,
"Team1RedCards" : null,
"Team2RedCards" : null,
"odds" : [
{
"id" : 62282718,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "21968173",
"ScrapingDateTime" : "2013-09-13T08:39:15.81",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 1.81,
"Odds2" : 2.1236,
"Odds3" : null,
"MaxBet" : 2000,
"Bet" : "Hcap",
"BetGamePart" : 0,
"Line" : -0.25,
"ID_IDENTITY" : 1
},
{
"id" : 62282719,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "21968175",
"ScrapingDateTime" : "2013-09-13T08:39:15.81",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.04167,
"Odds2" : 1.86,
"Odds3" : null,
"MaxBet" : 2000,
"Bet" : "Total",
"BetGamePart" : 0,
"Line" : 2.0,
"ID_IDENTITY" : 2
},
{
"id" : 62282720,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "21968177",
"ScrapingDateTime" : "2013-09-13T08:39:15.81",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.16,
"Odds2" : 3.45,
"Odds3" : 3.05,
"MaxBet" : 1000,
"Bet" : "X12",
"BetGamePart" : 0,
"Line" : null,
"ID_IDENTITY" : 3
},
{
"id" : 62282721,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "21968179",
"ScrapingDateTime" : "2013-09-13T08:39:15.81",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.21951,
"Odds2" : 1.7,
"Odds3" : null,
"MaxBet" : 1000,
"Bet" : "Hcap",
"BetGamePart" : 1,
"Line" : -0.25,
"ID_IDENTITY" : 4
},
{
"id" : 62282722,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "21968180",
"ScrapingDateTime" : "2013-09-13T08:39:15.81",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.78,
"Odds2" : 3.9,
"Odds3" : 1.99,
"MaxBet" : 500,
"Bet" : "X12",
"BetGamePart" : 1,
"Line" : null,
"ID_IDENTITY" : 5
},
{
"id" : 62282723,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "21968181",
"ScrapingDateTime" : "2013-09-13T08:39:15.81",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 1.93,
"Odds2" : 1.95,
"Odds3" : null,
"MaxBet" : 1000,
"Bet" : "Total",
"BetGamePart" : 1,
"Line" : 0.75,
"ID_IDENTITY" : 6
},
{
"id" : 62290838,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "14382209",
"ScrapingDateTime" : "2013-09-13T15:38:11.303",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 1.81,
"Odds2" : 2.1236,
"Odds3" : null,
"MaxBet" : 2000,
"Bet" : "Hcap",
"BetGamePart" : 0,
"Line" : -0.25,
"ID_IDENTITY" : 7
},
{
"id" : 62290839,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "14382211",
"ScrapingDateTime" : "2013-09-13T09:27:39.6",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.05263,
"Odds2" : 1.85,
"Odds3" : null,
"MaxBet" : 2000,
"Bet" : "Total",
"BetGamePart" : 0,
"Line" : 2.0,
"ID_IDENTITY" : 8
},
{
"id" : 62290840,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "14382213",
"ScrapingDateTime" : "2013-09-13T10:29:22.863",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.15,
"Odds2" : 3.45,
"Odds3" : 3.1,
"MaxBet" : 1000,
"Bet" : "X12",
"BetGamePart" : 0,
"Line" : null,
"ID_IDENTITY" : 9
},
{
"id" : 62290841,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "14382215",
"ScrapingDateTime" : "2013-09-13T15:38:11.303",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.21951,
"Odds2" : 1.7,
"Odds3" : null,
"MaxBet" : 1000,
"Bet" : "Hcap",
"BetGamePart" : 1,
"Line" : -0.25,
"ID_IDENTITY" : 10
},
{
"id" : 62290842,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "14382216",
"ScrapingDateTime" : "2013-09-13T15:38:11.303",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.78,
"Odds2" : 3.9,
"Odds3" : 1.99,
"MaxBet" : 500,
"Bet" : "X12",
"BetGamePart" : 1,
"Line" : null,
"ID_IDENTITY" : 11
},
{
"id" : 62290843,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "14382217",
"ScrapingDateTime" : "2013-09-13T15:38:11.303",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 1.93,
"Odds2" : 1.95,
"Odds3" : null,
"MaxBet" : 1000,
"Bet" : "Total",
"BetGamePart" : 1,
"Line" : 0.75,
"ID_IDENTITY" : 12
},
{
"id" : 62381139,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "20864356",
"ScrapingDateTime" : "2013-09-13T15:38:11.303",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.05263,
"Odds2" : 1.85,
"Odds3" : null,
"MaxBet" : 2000,
"Bet" : "Total",
"BetGamePart" : 0,
"Line" : 2.0,
"ID_IDENTITY" : 13
},
{
"id" : 62381140,
"BetId" : 2,
"BookieId" : 1,
"BookieBetId" : "20864358",
"ScrapingDateTime" : "2013-09-13T15:38:11.303",
"MinuteInGame" : null,
"GamePart" : null,
"Odds1" : 2.15,
"Odds2" : 3.45,
"Odds3" : 3.1,
"MaxBet" : 1000,
"Bet" : "X12",
"BetGamePart" : 0,
"Line" : null,
"ID_IDENTITY" : 14
}
]
}
]
}
now I'm trying to find by date with this Query
db.EventsBetsOdds.find( { "bets": { $elemMatch: { "DataTime": "2013-09-10T09:06:43.333" } } } );
Fetched 0 record(s) in 400ms
is what i get, what am i doing wrong?
i tried to put an index on this field, indexing finished
Your mongo query should be like this:
db.testes.find({
'bets.DateTime': '2013-09-10T09:06:43.333'
}, {
'bets': {
$elemMatch: {
'DateTime': '2013-09-10T09:06:43.333'
}
}
})
Or something like this:
db.testes.find({
'bets.DateTime': '2013-09-10T09:06:43.333'
}, {
'bets.$': 1
})
Helpful links:
$ (projection operator)
$elemMatch - Array of Embedded Documents
Array Field Limitations
$filter (aggregation)