I am new to MongoDB. I am not able to create a collection. It gives a sentence in the mongo shell - Display all 169 possibilities? (y or n). The code is -
db.Lead.insert(
{ LeadID: 1,
MasterAccountID: 100,
LeadName: 'Sarah',
LeadEmailID : 'sarah#hmail.com',
LeadPhoneNumber : '2132155445',
Details : [{ StateID: 1,
TaskID : 1,
Assigned By : 1001,
TimeStamp : '10:00:00',
StatusID : 1 }
]
}
)
Not sure what the issue is. Please help me out with the same.
Regards.
Apart from the fact there is a space in Assigned By everything looks good.
I am able to insert it properly.
> db.Lead.find().pretty()
{
"_id" : ObjectId("517ebe75278e0557fd167eb7"),
"LeadID" : 1,
"MasterAccountID" : 100,
"LeadName" : "Sarah",
"LeadEmailID" : "sarah#hmail.com",
"LeadPhoneNumber" : "2132155445",
"Details" : [
{
"StateID" : 1,
"TaskID" : 1,
"AssignedBy" : 1001,
"TimeStamp" : "10:00:00",
"StatusID" : 1
}
]
}
Related
BED_MAST this is my one collection bed_mast contains WARD_ID and want to perform join to my other collection with is WARD_MAST given below.
{
"_id" : ObjectId("5e53c95a26b0e5ad0fb46376"),
"Bed_id" : "bd-10",
"WARD_ID" : "4",
"OCCUPIED" : "0",
"BED_TYPE" : "single AC"
}
{
"_id" : ObjectId("5e53c95a26b0e5ad0fb46377"),
"Bed_id" : "bd-11",
"WARD_ID" : "1",
"OCCUPIED" : "0",
"BED_TYPE" : "single Non AC"
}
WARD_MAST this is my WARD_MAST having ward_id. but while I am putting lookup I am not getting any data.
{
"_id" : ObjectId("5e53c95b26b0e5ad0fb46544"),
"patient_id" : null,
"ward_id" : 1,
"total_beds" : 55,
"ward_name" : "Ward 1"
}
{
"_id" : ObjectId("5e53c95d26b0e5ad0fb46545"),
"patient_id" : null,
"ward_id" : 2,
"total_beds" : 63,
"ward_name" : "Ward 2"
}
MY query is
db.BED_MAST.aggregate([{$lookup:{'from':"WARD_MAST",'localField':"WARD_ID",'foreignField':"ward_id",'as':"lookup_value"}}]).pretty()
output: I have confirmed the data by running this query to MySQL there it is working fine
{
"_id" : ObjectId("5e53c95b26b0e5ad0fb46388"),
"Bed_id" : "bd-28",
"WARD_ID" : "6",
"OCCUPIED" : "0",
"BED_TYPE" : "NICU",
"lookup_value" : [ ]
}
SAMPLE VALUES DATA IS GIVEN ALL DATA IS NOT POSSIBLE TO GIVE. I know it was asked 1000 times but not able to resolve this question. tried to solve with lookup. but it showing blank space. Is anything I am missing.
The problem is BED_MAST collection's WARD_ID has string values and WARD_MAST collection's ward_id has Number values.
I have a collection of 6-7 million event records. I have another collection of ~100,000 hourly weather records spanning the same timeframe as the event records. I am using an $aggregate pipeline with $lookup to merge in relevant weather data for each event in the event collection.
THE PROBLEM: I have been running this on the full EVENT dataset for more than 8 HOURS, with no result. I have a deadline and I'm wondering if I will get a result...ever.
PLEASE HELP
Here is a sample event record:
{
"_id" : ObjectId("5dedae8111cd89b173b00910"),
"EventType" : "P",
"Jurisdiction" : "ABCD",
"Year" : 2006,
"JulianDay" : 91,
"CallReceipt" : ISODate("2006-04-01T00:00:37Z"),
"EventClosed" : ISODate("2006-04-01T00:05:25Z"),
"FinalType" : "EFGHI",
"EventWindowStart" : ISODate("2006-04-01T00:00:00Z"),
"EventWindowEnd" : ISODate("2006-04-01T01:00:00Z")
}
Here is a weather record:
{
"_id" : ObjectId("5dc3cd909fc78c0c78a336da"),
"DATE" : ISODate("2012-01-01T00:02:00Z"),
"REPORT_TYPE" : "FM-16",
"SOURCE" : 7,
"HourlyAltimeterSetting" : "30.06",
"HourlyDewPointTemperature" : "36",
"HourlyDryBulbTemperature" : "37",
"HourlyPresentWeatherType" : "BR:1 ||",
"HourlyRelativeHumidity" : 93,
"HourlySkyConditions" : "SCT:04 7 BKN:07 15 OVC:08 33",
"HourlyStationPressure" : "29.46",
"HourlyVisibility" : "5.00",
"HourlyWetBulbTemperature" : 37,
"HourlyWindDirection" : "260",
"HourlyWindSpeed" : 5,
"REM" : "MET10101/01/12 00:02:02 SPECI KROC 010502Z 26004KT 5SM BR SCT00
7 BKN015 OVC033 03/02 A3006 RMK AO2 RTX (MP)",
"REPORT_MODE" : "hourly"
}
Here is my code, typed directly into the mongo shell:
db.EVENTS.aggregate([
{
$lookup:
{
from: "WEATHER",
let : { start : "$EventWindowStart", end: "$EventWindowEnd"},
pipeline : [
{ $match :
{ $expr:
{ $and:
[
{$gte: ["$DATE", "$$start"]},
{$lte: ["$DATE", "$$end"]}
]
}
}
},
{$project: {
_id : 0,
HourlyDryBulbTemperature : 1,
HourlyPrecipitation : 1,
HourlyVisibility : 1,
WindSpeed: 1
}
}
],
as: "HourlyWeatherData"
}
},
{$out: "MERGED" }
])
On a small test subset I get the desired output. So the code works, as far as I can tell...
Sample output:
{
"_id" : ObjectId("5dedae8111cd89b173b00910"),
"EventType" : "P",
"Jurisdiction" : "ABCD",
"Year" : 2006,
"JulianDay" : 91,
"CallReceipt" : ISODate("2006-04-01T00:00:37Z"),
"EventClosed" : ISODate("2006-04-01T00:05:25Z"),
"FinalType" : "EFGHI",
"EventWindowStart" : ISODate("2006-04-01T00:00:00Z"),
"EventWindowEnd" : ISODate("2006-04-01T01:00:00Z"),
"HourlyWeatherData" : [
{
"HourlyDryBulbTemperature" : "59",
"HourlyPrecipitation" : "0.00",
"HourlyVisibility" : "10.00"
},
{
"HourlyDryBulbTemperature" : "59",
"HourlyVisibility" : "9.94"
}
]
}
PS: I do have ascending indexes on the event window fields in EVENTS, and an ascending and descending index on the DATE in WEATHER.
I'm new to mongodb and I've been trying to figure this out but I haven't so far. I think I'm close. I have records similar to this.
{
"_id" : ObjectId("5bfdf4385a37e507cff0ff62"),
"Search_word" : "job01",
"hadoop" : 0,
"hive" : 0,
"javascript" : 0,
"mongodb" : 0,
"sql" : 1,
"java" : 0,
"sas" : 0,
"powerbi" : 0,
"python" : 1,
"pig" : 0,
"scala" : 0
}
I'm trying to create groups by search word and count the 1s in all the other values. Ive never used mongodb before.
The end result would look like this
job01, sql : 100, hive: 205, etc...
job02, sql : 121, hive 10, etc...
In python its literally like this
skill_data = df.groupby(by='Search_word').sum()
I tried doing something like this just to get sql count where it is 1
db.data_final.aggregate(
{"$group" :
{_id : {Search_word:"$Search_word", sql: {"$eq":["$sql",1]} }, count : { $sum : 1} } } )
but it gives me 2 counts one for false where sql not equal 1 and a count that I want for sql that is 1. how can I get rid of the false ? This is what I get now:
{ "_id" : { "Search_word" : "job01", "sql" : true }, "count" : 124 } // this is what i want only and do it for all other values in one query
{ "_id" : { "Search_word" : "job01", "sql" : false}, "count" : 279 }
Any help is appreciated
You could try this since the value of each field is either 0 or 1.
db.collection.aggregate({$group: {_id: "$Search_word", sql: {$sum: "$sql"}}});
I am trying to write a query in mongo that will create a new table, loop through my data set, and insert the TopExecutiveTitle into the new table. I also would like it to keep count of each position and only insert a position into the table when it is new.
This is what I have so far. This code loops through my table and inserts the TopExectuiveTitle into a new table. However, it does not group them together and keep count. How do I write my query so that it will?
db.car.find().forEach( function (x) {
db.TopExecutiveTable.insert({Topexecutivetitle: x.Topexecutivetitle})
});
Here is a sample of a document in my database.
{
"_id" : ObjectId("5a22c8e562c2e489c5df70fa"),
"2016rank" : 1,
"Dealershipgroupname" : "AutoNation Inc.?",
"Address" : "200 S.W. 1st Ave.",
"City/State/Zip" : "Fort Lauderdale, FL 33301",
"Phone" : "(954) 769-7000",
"Companywebsite" : "www.autonation.com",
"Topexecutive" : "Mike Jackson",
"Topexecutivetitle" : "chairman & CEO",
"Totalnewretailunits" : "337,622",
"Totalusedunits" : "225,713",
"Totalfleetunits" : 3,
"Totalwholesaleunits" : "82,342",
"Total_units" : "649,415",
"Total_number_of _dealerships" : 260,
"Grouprevenuealldepartments*" : "$21,609,000,000",
"2015rank" : 1
}
The result I would like is something like this
"Topexecutivetitle" : "chairman & CEO"
"Count" : 3
"Topexecutivetitle" : "president"
"Count" : 7
}
To do this you need to use the aggregate function of mongo, something like this:
db.car.aggregate([
{
$group:{
_id:"$Topexecutivetitle",
count:{$sum:1}
}
},
{
$project:{
Topexecutivetitle:"$_id",
count:1,
_id:0
}
},
{
$out:"result"
}])
This will give you your desired output and store it into a new collection "result":
{
"_id" : "president",
"count" : 1.0
},
{
"_id" : "chairman & CEO",
"count" : 3.0
}
I have some data that looks like this:
[
{
"_id" : ObjectId("4e2f2af16f1e7e4c2000000a"),
"advertisers" : [
{
"created_at" : ISODate("2011-07-26T21:02:19Z"),
"category" : "Infinity Pro Spin Air Brush",
"updated_at" : ISODate("2011-07-26T21:02:19Z"),
"lowered_name" : "conair",
"twitter_name" : "",
"facebook_page_url" : "",
"website_url" : "",
"user_ids" : [ ],
"blog_url" : "",
},
and I was thinking that a query like this would give the id of the advertiser:
var start = new Date(2011, 1, 1);
> var end = new Date(2011, 12, 12);
> db.agencies.find( { "created_at" : {$gte : start , $lt : end} } , { _id : 1 , program_ids : 1 , advertisers { name : 1 } } ).limit(1).toArray();
But my query didn't work. Any idea how I can add the fields inside the nested elements to my list of fields I want to get?
Thanks!
Use dot notation (e.g. advertisers.name) to query and retrieve fields from nested objects:
db.agencies.find({
"advertisers.created_at": {
$gte: start,
$lt: end
}
},
{
_id: 1,
program_ids: 1,
"advertisers.name": 1
}
}).limit(1).toArray();
Reference: Retrieving a Subset of Fields
and Dot Notation
db.agencies.find(
{ "advertisers.created_at" : {$gte : start , $lt : end} } ,
{ program_ids : 1 , advertisers.name : 1 }
).limit(1).pretty();
There is one thing called dot notation that MongoDB provides that allows you to look inside arrays of elements. Using it is as simple as adding a dot for each array you want to enter.
In your case
"_id" : ObjectId("4e2f2af16f1e7e4c2000000a"),
"advertisers" : [
{
"created_at" : ISODate("2011-07-26T21:02:19Z"),
"category" : "Infinity Pro Spin Air Brush",
"updated_at" : ISODate("2011-07-26T21:02:19Z"),
"lowered_name" : "conair",
"twitter_name" : "",
"facebook_page_url" : "",
"website_url" : "",
"user_ids" : [ ],
"blog_url" : "",
},
{ ... }
If you want to go inside the array of advertisers to look for the property created_at inside each one of them, you can simply write the query with the property {'advertisers.created_at': query} like follows
db.agencies.find( { 'advertisers.created_at' : { {$gte : start , $lt : end} ... }