MongoDB search by DateTime type not working - mongodb

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)

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

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 How to search on Nested Arrays fo Documents

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()

insert documents in MongoDB multiple nested elements

I am looking to insert another document in a multiple nested document. Do you have any other ideas of how to solve it? Thanks
{
"_id" : ObjectId("576bddaa08b8f7dc20000033"),
"Name" : "Business",
"Image" : "fa fa-dollar",
"Description" : "Money, Capitalism, Business",
"NoForums" : 0,
"NoTopics" : 0,
"NoComments" : 0,
"NoUsers" : 0,
"CoverImage" : "https://www.rocklandtrust.com/assets/content/txh9vvwg/2014/08/06/business%20savings.jpg",
"Children" : [
{
"_id" : ObjectId("57715e0608b8f7441b00002d"),
"Name" : "Business School",
"URLName" : "Business-School",
"Image" : "http://www.small-business-website.net/wp-content/uploads/2016/04/How-to-achieve-a-successful-business-sale-128x128.jpg",
"Description" : "Everything about Business Schools",
"NoForums" : 0,
"NoTopics" : 0,
"NoComments" : 0,
"NoUsers" : 0,
"CoverImage" : "http://www.incae.edu/sites/default/files/styles/slideshow-un-medio/public/primerdiamba2010_018.jpg?itok=LfZmxw14",
"Children" : []
},
{
"_id" : ObjectId("57715f0d08b8f7942c00002b"),
"Name" : "Journals",
"URLName" : "Journals",
"Image" : "http://www.alliedacademies.org/images/accounting.png",
"Description" : "Journals and Diaries about Businesses",
"NoForums" : 0,
"NoTopics" : 0,
"NoComments" : 0,
"NoUsers" : 0,
"CoverImage" : "http://www.advantagebusinessvaluations.com/wp-content/uploads/2015/08/slide3.jpg",
"Children" : []
},
{
"_id" : ObjectId("57740ae208b8f7f41a000039"),
"Name" : "Stock Exchange",
"URLName" : "Stock-Exchange",
"Image" : "https://38.media.tumblr.com/avatar_bd29383b5657_128.png",
"Description" : "Stock Exchange, US Stock",
"NoForums" : 0,
"NoTopics" : 0,
"NoComments" : 0,
"NoUsers" : 0,
"CoverImage" : "http://c.fastcompany.net/multisite_files/coexist/imagecache/1280/poster/2013/04/1681873-poster-1280-stock-chart.jpg",
"Children" : [
{
"_id" : ObjectId("5774464608b8f7f41a00004a"),
"Name" : "Stock Software",
"URLName" : "Stock-Software",
"Image" : "http://s1.evcdn.com/images/block/I0-001/026/278/060-2.jpeg_/1-day-introduction-forex-stock-market-trading-60.jpeg",
"Description" : "Stock Excba ge Software",
"NoForums" : 0,
"NoTopics" : 0,
"NoComments" : 0,
"NoUsers" : 0,
"CoverImage" : "http://media.therakyatpost.com/wp-content/uploads/2015/04/stock-exchange-record-bigstock-900x470.jpg",
"Children" : []
},
{
"_id" : ObjectId("5774e2cb08b8f76c1a00002e"),
"Name" : "Meta Trader",
"URLName" : "Meta-Trader",
"Image" : "http://www.file-extensions.org/imgs/app-icon/128/6904/metatrader-icon.png",
"Description" : "MetaTrader",
"NoForums" : 0,
"NoTopics" : 0,
"NoComments" : 0,
"NoUsers" : 0,
"CoverImage" : "http://www.roboforex.com/files/filemanager/image/site/metatrader.png",
"Children" : []
},
{
"_id" : ObjectId("5774eb2708b8f76c1a000043"),
"Name" : "Meta Trader",
"URLName" : "Meta-Trader",
"Image" : "http://www.file-extensions.org/imgs/app-icon/128/6904/metatrader-icon.png",
"Description" : "MetaTrader",
"NoForums" : 0,
"NoTopics" : 0,
"NoComments" : 0,
"NoUsers" : 0,
"CoverImage" : "http://www.roboforex.com/files/filemanager/image/site/metatrader.png",
"Children" : []
},
{
"_id" : ObjectId("5774eb8208b8f76c1a000044"),
"Name" : "Meta Trader",
"URLName" : "Meta-Trader",
"Image" : "http://www.file-extensions.org/imgs/app-icon/128/6904/metatrader-icon.png",
"Description" : "MetaTrader",
"NoForums" : 0,
"NoTopics" : 0,
"NoComments" : 0,
"NoUsers" : 0,
"CoverImage" : "http://www.roboforex.com/files/filemanager/image/site/metatrader.png",
"Children" : []
},
{
"_id" : ObjectId("5774eb8f08b8f76c1a000045"),
"Name" : "Meta Trader",
"URLName" : "Meta-Trader",
"Image" : "http://www.file-extensions.org/imgs/app-icon/128/6904/metatrader-icon.png",
"Description" : "MetaTrader",
"NoForums" : 0,
"NoTopics" : 0,
"NoComments" : 0,
"NoUsers" : 0,
"CoverImage" : "http://www.roboforex.com/files/filemanager/image/site/metatrader.png",
"Children" : []
}
]
}
],
"URLName" : "Business"
}
How can I insert another document in Business.Children.Children.(Stock Software).Children
Use the positional operator to push values.
https://docs.mongodb.com/manual/reference/operator/update/positional/
db.collectioname.update(
{
"_id" : ObjectId("576bddaa08b8f7dc20000033"),
"Children._id" : ObjectId("57715e0608b8f7441b00002d")
},
{
$push: {
"Children.$.Children": {
"Name" : "Business School",
"URLName" : "Business-School",
"Image" : "http://www.small-business-website.net/wp-content/uploads/2016/04/How-to-achieve-a-successful-business-sale-128x128.jpg",
"Description" : "Everything about Business Schools",
"NoForums" : 0,
"NoTopics" : 0,
"NoComments" : 0,
"NoUsers" : 0
}
}
}
)
Note : Beware Mongo does not not allow nested documents to update.It is a known issue. It allows only to a single level.
https://jira.mongodb.org/browse/SERVER-831

MongoDB efficient way to search for objectId field with null using Index?

I am trying to find an efficient way to search for items in which specific field is null.
In the MongoDB, I have folder schema which has parent field of its parent folder's ObjectId and parent is indexed. For root folders, parent fields are null.
When I try to find all the root folders with parent:null, explain displays indexOnly: false
db.folders.find({parent: null}, {parent: 1, _id: 0}).explain()
{
"cursor" : "BtreeCursor parent_1",
"isMultiKey" : false,
"n" : 126,
"nscannedObjects" : 126,
"nscanned" : 126,
"nscannedObjectsAllPlans" : 126,
"nscannedAllPlans" : 126,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 1,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"parent" : [
[
null,
null
]
]
},
"server" : "c268.candidate.36:10268",
"filterSet" : false,
"stats" : {
"type" : "PROJECTION",
"works" : 128,
"yields" : 1,
"unyields" : 1,
"invalidates" : 0,
"advanced" : 126,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"children" : [
{
"type" : "KEEP_MUTATIONS",
"works" : 128,
"yields" : 1,
"unyields" : 1,
"invalidates" : 0,
"advanced" : 126,
"needTime" : 1,
"needFetch" : 0,
"isEOF" : 1,
"children" : [
{
"type" : "FETCH",
"works" : 128,
"yields" : 1,
"unyields" : 1,
"invalidates" : 0,
"advanced" : 126,
"needTime" : 1,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 126,
"children" : [
{
"type" : "IXSCAN",
"works" : 127,
"yields" : 1,
"unyields" : 1,
"invalidates" : 0,
"advanced" : 126,
"needTime" : 1,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ parent: 1.0 }",
"isMultiKey" : 0,
"boundsVerbose" : "field #0['parent']: [null, null]",
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 126,
"children" : []
}
]
}
]
}
]
}
}
I tried to use $type to find any ones which parent folder is not ObjectId. But still indexOnly:false.
db.folders.find({parent: {$ne: {$type: 7}}}, {parent: 1, _id: 0}).explain()
"indexOnly": false
Is there a way to search null value only using index? If not, is there a better value to store instead of null to be able to search with index?
Additional
example of root folder
{
"_id" : ObjectId("55a04a2d754971030059b7ad"),
"active" : true,
"modified" : ISODate("2016-02-05T22:30:08.053Z"),
"created" : ISODate("2015-07-10T22:41:49.009Z"),
"user" : ObjectId("54d3ae187a738c0300f59e61"),
"name" : "2nd Grade",
"parent" : null,
"clientModified" : ISODate("2016-02-05T22:30:07.872Z"),
"userCreated" : ISODate("2015-07-10T22:41:48.328Z"),
"ancestors" : [],
"__v" : 2
}
example of child folder
{
"_id" : ObjectId("56d0b4edb6f05e03009bcabc"),
"active" : true,
"modified" : ISODate("2016-02-26T20:26:21.328Z"),
"created" : ISODate("2016-02-26T20:26:21.328Z"),
"user" : ObjectId("54d3ae187a738c0300f59e61"),
"name" : "music",
"parent" : ObjectId("55a04a2d754971030059b7ad"),
"clientModified" : ISODate("2016-02-26T20:26:20.398Z"),
"userCreated" : ISODate("2016-02-26T20:26:20.398Z"),
"ancestors" : [
ObjectId("55a04a2d754971030059b7ad")
],
"__v" : 0
}
db.folders.getIndexes()
{
"0" : {
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "app29099188.folders"
},
"1" : {
"v" : 1,
"key" : {
"user" : 1,
"_fts" : "text",
"_ftsx" : 1
},
"name" : "user_1_name_text",
"ns" : "app29099188.folders",
"background" : true,
"safe" : null,
"weights" : {
"name" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 2
},
"2" : {
"v" : 1,
"key" : {
"user" : 1,
"parent" : 1
},
"name" : "user_1_parent_1",
"ns" : "app29099188.folders",
"background" : true,
"safe" : null
},
"3" : {
"v" : 1,
"key" : {
"parent" : 1.0000000000000000
},
"name" : "parent_1",
"ns" : "app29099188.folders"
}
}
After comment - update:
The way to eliminate docScan is to have a value in parent field. It can be zeroed objectId or just "/" as a root.
db.satoko.insert({"test":"sdsf", parent: "/"})
db.satoko.insert({"test":"sds33f", parent: "/"})
db.satoko.insert({"parent":ObjectId("56d8b2879bd059e7247a6096"), "test":"sdsf"})
explain results:
db.satoko.find({parent:{$eq:"/"} }, {parent: 1, _id: 0}).explain("allPlansExec
ution")
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.satoko",
"indexFilterSet" : false,
"parsedQuery" : {
"parent" : {
"$eq" : "/"
}
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"parent" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"parent" : 1
},
"indexName" : "parent_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"parent" : [
"[\"/\", \"/\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2,
"executionTimeMillis" : 0,
"totalKeysExamined" : 2,
"totalDocsExamined" : 0,
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"transformBy" : {
"parent" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"parent" : 1
},
"indexName" : "parent_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"parent" : [
"[\"/\", \"/\"]"
]
},
"keysExamined" : 2,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
},
"allPlansExecution" : [ ]
},
"serverInfo" : {
"host" : "greg",
"port" : 27017,
"version" : "3.2.3",
"gitVersion" : "b326ba837cf6f49d65c2f85e1b70f6f31ece7937"
},
"ok" : 1
}