how to change the data of multiple array of object in mongodb - mongodb

data
{
"_id" : ObjectId("6225fa108c3186fb762d562c"),
"__v" : 0,
"createdAt" : ISODate("2022-03-07T12:26:56.538Z"),
"pm" : {
"trackOfBreakdown" : [
{
"r" : 1,
"c" : 1,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656016526.0,
"_id" : ObjectId("6225fa10e0e0a55548e9ee71")
},
{
"r" : 1,
"c" : 28,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656022307.0,
"_id" : ObjectId("6225fa16e0e0a55548e9ee74")
},
{
"r" : 4,
"c" : 1,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656332600.0,
"_id" : ObjectId("6225fb4ce0e0a55548e9ee89")
},
{
"r" : 1,
"c" : 10,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656337524.0,
"_id" : ObjectId("6225fb51e0e0a55548e9ee8c")
},
{
"r" : 1,
"c" : 11,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656342041.0,
"_id" : ObjectId("6225fb56e0e0a55548e9ee8f")
},
{
"r" : 1,
"c" : 32,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656356615.0,
"_id" : ObjectId("6225fb64e0e0a55548e9ee94")
},
{
"r" : 3,
"c" : 5,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656450293.0,
"_id" : ObjectId("6225fbc2e0e0a55548e9ee9e")
},
{
"r" : 3,
"c" : 10,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656455202.0,
"_id" : ObjectId("6225fbc7e0e0a55548e9eea1")
},
{
"r" : 4,
"c" : 17,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656458428.0,
"_id" : ObjectId("6225fbcae0e0a55548e9eea4")
},
{
"r" : 4,
"c" : 31,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656484350.0,
"_id" : ObjectId("6225fbe4e0e0a55548e9eea9")
},
{
"r" : 1,
"c" : 29,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656529662.0,
"_id" : ObjectId("6225fc11e0e0a55548e9eeae")
},
{
"r" : 1,
"c" : 14,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656770018.0,
"_id" : ObjectId("6225fd026c098920849389fb")
},
{
"r" : 1,
"c" : 16,
"val" : true,
"status" : "Pending",
"dateMonthYear" : 1646656772807.0,
"_id" : ObjectId("6225fd046c098920849389fe")
},
{
"r" : 1,
"c" : 19,
"val" : true,
"status" : "Pending",
"dateMonthYear" : 1646656775777.0,
"_id" : ObjectId("6225fd076c09892084938a01")
},
{
"r" : 1,
"c" : 34,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656778952.0,
"_id" : ObjectId("6225fd0a6c09892084938a04")
},
{
"r" : 4,
"c" : 21,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656850045.0,
"_id" : ObjectId("6225fd5233d2560ca0e3b79e")
},
{
"r" : 3,
"c" : 21,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646656852880.0,
"_id" : ObjectId("6225fd5433d2560ca0e3b7a1")
},
{
"r" : 3,
"c" : 24,
"val" : true,
"status" : "Pending",
"dateMonthYear" : 1646656855582.0,
"_id" : ObjectId("6225fd5733d2560ca0e3b7a4")
},
{
"r" : 3,
"c" : 29,
"val" : true,
"status" : "Pending",
"dateMonthYear" : 1646656997927.0,
"_id" : ObjectId("6225fde75128bd3ea08e66ee")
},
{
"r" : 3,
"c" : 31,
"val" : true,
"status" : "Pending",
"dateMonthYear" : 1646657002122.0,
"_id" : ObjectId("6225fdea5128bd3ea08e66f1")
},
{
"r" : 3,
"c" : 41,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646657004121.0,
"_id" : ObjectId("6225fdec5128bd3ea08e66f4")
},
{
"r" : 2,
"c" : 35,
"val" : true,
"status" : "Pending",
"dateMonthYear" : 1646657006449.0,
"_id" : ObjectId("6225fdee5128bd3ea08e66f7")
},
{
"r" : 2,
"c" : 28,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646657015240.0,
"_id" : ObjectId("6225fdf75128bd3ea08e66fe")
},
{
"r" : 3,
"c" : 16,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646657028648.0,
"_id" : ObjectId("6225fe075128bd3ea08e6703")
},
{
"r" : 2,
"c" : 24,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646835294639.0,
"_id" : ObjectId("6228b65e015cb12e74a36cdb")
},
{
"r" : 2,
"c" : 10,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646835504875.0,
"_id" : ObjectId("6228b730015cb12e74a36cf2")
},
{
"r" : 4,
"c" : 4,
"val" : false,
"status" : "Pending",
"dateMonthYear" : 1646997530576.0,
"_id" : ObjectId("622b301a10881d13806448d0")
},
{
"r" : 4,
"c" : 4,
"val" : true,
"status" : "Pending",
"dateMonthYear" : 1646997531969.0,
"_id" : ObjectId("622b301b10881d13806448d2")
},
{
"r" : 4,
"c" : 4,
"val" : true,
"status" : "Pending",
"dateMonthYear" : 1646997532149.0,
"_id" : ObjectId("622b301c10881d13806448d4")
},
{
"r" : 4,
"c" : 4,
"val" : true,
"status" : "Pending",
"dateMonthYear" : 1646997533186.0,
"_id" : ObjectId("622b301d10881d13806448d6")
}
],
"checkList" : [
{
"ch_id" : "621eff4e0ed5c751adaa42fb",
"val" : "Gopi",
"remarks" : "Manoj",
"dateMonthYear" : 1646591400000.0,
"eid" : "07",
"status" : "Pending",
"_id" : ObjectId("6225fa30e0e0a55548e9ee77")
},
{
"ch_id" : "621effab0ed5c751adaa42fd",
"val" : "Pradeep",
"remarks" : "Shetty",
"dateMonthYear" : 1646591400000.0,
"eid" : "07",
"status" : "Pending",
"_id" : ObjectId("6225fa30e0e0a55548e9ee79")
},
{
"ch_id" : "621effd30ed5c751adaa42ff",
"val" : "Arun",
"remarks" : "Sri",
"dateMonthYear" : 1646591400000.0,
"eid" : "07",
"status" : "Pending",
"_id" : ObjectId("6225fa30e0e0a55548e9ee7b")
},
{
"ch_id" : "621efff30ed5c751adaa4301",
"val" : "Manju",
"remarks" : "Maari",
"dateMonthYear" : 1646591400000.0,
"eid" : "07",
"status" : "Pending",
"_id" : ObjectId("6225fa30e0e0a55548e9ee7d")
}
],
"sparespm1" : [
{
"date" : "2022-03-04",
"partDescription" : "gopi",
"partNo" : "hai",
"qty" : "52",
"dateMonthYear" : 1646982057447.0,
"status" : "Pending",
"eid" : "07",
"cellId" : "1",
"_id" : ObjectId("622af3b6e9044d702c75b6fb")
},
{
"date" : "2022-03-01",
"partDescription" : "dddd",
"partNo" : "ww",
"qty" : "er",
"dateMonthYear" : 1646982069153.0,
"status" : "Pending",
"eid" : "07",
"cellId" : "1",
"_id" : ObjectId("622af3b6e9044d702c75b6fd")
},
{
"date" : "2022-03-30",
"partDescription" : "Manoj",
"partNo" : "Shetty",
"qty" : "Hi",
"dateMonthYear" : 1648606373653.0,
"status" : "Pending",
"eid" : "07",
"cellId" : "1",
"_id" : ObjectId("6243bca9e736110684220890")
}
],
"machineDetails" : [
{
"machineName" : "hvhh",
"machineNo" : "2563154",
"dateMonthYear" : 1646999495640.0,
"eid" : "07",
"cellId" : "1",
"_id" : ObjectId("622b37c910881d13806448ed")
}
]
},
"updatedAt" : ISODate("2022-03-30T02:12:57.036Z")
}
In the above collection, I'll be having only one single document. And within that document, I need to update all statuses within arrays as "Approved", with respect to greater than or equal to starting day of the year and lesser than or equal to ending day of the year(it should compare respective dateMonthYear key)
Thank You

Use $[<identifier>]
db.collection.update({
_id: ObjectId("6225fa108c3186fb762d562c")
},
{
$set: {
"pm.trackOfBreakdown.$[x1].status": "Approved",
"pm.checkList.$[x2].status": "Approved",
"pm.sparespm1.$[x3].status": "Approved",
"pm.machineDetails.$[x4].status": "Approved"
}
},
{
arrayFilters: [
{
"x1.dateMonthYear": {
$gte: {
$toLong: {
$dateTrunc: {
date: "$$NOW",
unit: "year"
}
}
},
$lte: {
$toLong: {
$dateTrunc: {
date: {
$dateAdd: {
startDate: "$$NOW",
unit: "year",
amount: 1
}
},
unit: "year"
}
}
}
}
},
{
"x2.dateMonthYear": {
$gte: {
$toLong: {
$dateTrunc: {
date: "$$NOW",
unit: "year"
}
}
},
$lte: {
$toLong: {
$dateTrunc: {
date: {
$dateAdd: {
startDate: "$$NOW",
unit: "year",
amount: 1
}
},
unit: "year"
}
}
}
}
},
{
"x3.dateMonthYear": {
$gte: {
$toLong: {
$dateTrunc: {
date: "$$NOW",
unit: "year"
}
}
},
$lte: {
$toLong: {
$dateTrunc: {
date: {
$dateAdd: {
startDate: "$$NOW",
unit: "year",
amount: 1
}
},
unit: "year"
}
}
}
}
},
{
"x4.dateMonthYear": {
$gte: {
$toLong: {
$dateTrunc: {
date: "$$NOW",
unit: "year"
}
}
},
$lte: {
$toLong: {
$dateTrunc: {
date: {
$dateAdd: {
startDate: "$$NOW",
unit: "year",
amount: 1
}
},
unit: "year"
}
}
}
}
}
],
multi: true
})
mongoplayground

Related

MongoDB Redash - FieldPath field names may not contain '.'

I am new to mongodb. I am trying to take the daily count. I want count of those element where the fields I_C or L2_C exist and for the last 3 months.
I have written this code, but it is showing error -
"FieldPath field names may not contain '.'".
What would be the correct way to go for this?
Here is the schema of the data I am trying to query on.
{
"_id" : ObjectId("5b83af839bc195a7cfbabefb"),
"firstName" : null,
"lastName" : null,
"realm" : null,
"username" : null,
"password" : "$2a$10$WVTWLevvoJXcMs/x9o/qMOEV56owh2ppjZXJt4pSR8PIDuBeRehwW",
"credentials" : null,
"challenges" : null,
"email" : "ritesh.rnagpure#gmail.com",
"emailVerified" : null,
"verificationToken" : null,
"status" : null,
"created" : null,
"lastUpdated" : ISODate("2019-08-28T12:15:07.808Z"),
"dateCreated" : ISODate("2018-08-27T08:00:02.595Z"),
"source" : "web",
"isFromAPUrl" : false,
"personal" : {
"phone" : "9975891779",
"referralcode" : "",
"iccode" : "221930",
"isiccode" : "yes",
"referrerEmail" : "zohaibrulz#gmail.com",
"referrerName" : "Momin Zohaib Hasanali",
"residency" : {
"type" : "Indian",
"status" : "Status_Resident_Individual",
"nationality" : "Nationality_Indian",
"nationalityOther" : ""
},
"isNameFromIT" : true,
"name" : "RITESH RAJENDRA NAGPURE",
"birthdate" : {
"date" : "7",
"month" : "4",
"year" : "1993"
},
"previousPan" : "AJEPN0774B",
"previousDOB" : "07/04/1993",
"address" : {
"correspondence" : {
"line1" : "Flat No-404, G Wing, Sai Satyam Residency,",
"line2" : "Near Don Bosco School, Kalyan West, Opposite Raunak city, Kalyan, Thane",
"city" : "Kalyan",
"state" : "Maharashtra",
"pincode" : "421301",
"proof" : ""
},
"permanent" : {
"line1" : "Flat No-404, G Wing, Sai Satyam Residency,",
"line2" : "Near Don Bosco School, Kalyan West, Opposite Raunak city, Kalyan, Thane",
"city" : "Kalyan",
"state" : "Maharashtra",
"pincode" : "421301",
"proof" : "",
"proofOtherText" : ""
},
"sameAsCorrespondence" : true
},
"isaddressFromQR" : false,
"isaadhar" : false,
"iskra" : false,
"fatherOrSpouseName" : "RAJENDRA",
"ipvdetails" : {
"ipvrequire" : false,
"ipvstatus" : "done"
},
"aadharNumber" : "875847348082",
"mothername" : "SAVITA",
"gender" : "male",
"polexposed" : "no",
"maritalStatus" : "single",
"incomeType" : "annual",
"income" : "below_1L",
"occupation" : "Private Sector",
"tradingExperience" : "< 1 Year",
"promocode" : "CASHBACK",
"requireddoc" : {
"ispanrequire" : true,
"isuidrequire" : true,
"ischequerequire" : true,
"isphotorequire" : true,
"iscoraddressrequire" : false,
"issignaturerequire" : true,
"isaddressrequire" : false,
"isbankstatementrequire" : true,
"isoptionaldoc" : false
},
"uploadeddoc" : {
"ispan" : true,
"isuid" : true,
"isaddress" : false,
"isphoto" : true,
"iscoraddress" : false,
"ischeque" : true,
"issignature" : true,
"isoptionaldoc" : false,
"isbankstatement" : true,
"issignaturebmp" : true
},
"docname" : {
"uid" : "5b83af839bc195a7cfbabefb_uid.pdf",
"pan" : "5b83af839bc195a7cfbabefb_pan.jpg",
"photo" : "5b83af839bc195a7cfbabefb_photo.jpg",
"address" : "",
"coraddress" : "",
"cheque" : "5b83af839bc195a7cfbabefb_cheque.pdf",
"bankstatement" : "5b83af839bc195a7cfbabefb_bankstm.pdf",
"signature" : "5b83af839bc195a7cfbabefb_sign.jpg",
"other" : "",
"optionaldoc" : ""
},
"doctype" : {
"pan" : "pan",
"uid" : "uid",
"add" : "uid",
"cheque" : "bankstatement",
"bankstm" : "bankstatement",
"photo" : "photo"
},
"isaddressmodified" : true,
"isAllEsigned" : true,
"isEsignMerged" : true,
"iskycFile" : true,
"isKycEsigned" : true,
"isApplicationCompleted" : true,
"applicationStatus" : "completed",
"isPanVerified" : true,
"panStatus" : "E",
"panName" : "RITESH RAJENDRA NAGPURE",
"rejectionReasonList" : [],
"makerAssigned" : false,
"checkerAssigned" : true,
"checkerOfficer" : "robot",
"checkerTime" : ISODate("2019-05-29T11:30:33.004Z")
},
"txn" : {
"L1_L2" : ISODate("2018-08-27T11:06:35.251Z"),
"L1_L2_officer" : "prajakta.rane",
"L2_R" : ISODate("2018-08-27T10:43:19.063Z"),
"L2_R_officer" : "avilon.pereira",
"R_L1" : ISODate("2018-08-27T10:48:51.377Z"),
"L2_I" : ISODate("2018-08-27T11:43:12.718Z"),
"L2_I_officer" : "rajkumar.kesari",
"I_C" : ISODate("2018-08-27T12:58:52.343Z"),
"I_C_officer" : "suraj.pandey"
},
"sales" : {
"openStateOfficer" : "bhaskar.geera",
"aadhaar" : false,
"assignStep" : "registration",
"assignTo" : "bhaskar.geera",
"callTime" : "2018-08-27T11:35:15.446Z",
"calledMessage" : "Ringing",
"calledStatus" : "Ringing_Not_Picked",
"iscalled" : true,
"callCount" : 5
},
"utm_params" : {},
"landingPage" : "",
"suiteID" : "b9245931-a66f-3647-11b8-5b83af881208",
"_scopeMeta" : {},
"panPageVisited" : true,
"kra" : {
"pan" : "AJEPN0774B",
"dob" : "07/04/1993",
"name" : "",
"fatherOrSpouseName" : "",
"agency" : "",
"email" : "",
"mobile" : "",
"address" : {
"correspondence" : {
"line1" : "",
"line2" : "",
"city" : "",
"state" : "",
"pincode" : "",
"proof" : ""
},
"permanent" : {
"line1" : "",
"line2" : "",
"city" : "",
"state" : "",
"pincode" : "",
"proof" : ""
}
},
"gender" : "",
"maritalStatus" : "",
"appStatus" : "005",
"appUpdateStatus" : "005",
"authMode" : "0"
},
"aadhar" : {},
"overlayOpenData" : {
"showSegmentsOverlay" : false,
"showBrokerageOverlay" : false,
"showReviewOverlay" : true,
"showVerifyOverlay" : true
},
"currentStep" : "review/verify",
"namePageVisited" : true,
"options" : {
"BSE_EQ" : true,
"NSE_EQ" : true,
"BCD_FO" : false,
"NCD_FO" : true,
"NSE_FO" : true,
"MCX" : true,
"MF" : true
},
"pricingPlan" : "BASIC-D0",
"bank" : {
"primary" : {
"ifsc" : "SBIN0017460",
"name" : "STATE BANK OF INDIA",
"address" : "MADHUBAN TIRTHDHAM COMPLEX,SHOP NO 4,ADHARWADI ROAD,KALYAN WEST,DISTT.THANE.MAHARASHTRA 421301",
"branch" : "ADHARWADI ROAD BRANCH",
"city" : "THANE",
"state" : "MAHARASHTRA",
"country" : "INDIA",
"micr" : "",
"pincode" : "421301",
"type" : "savings",
"account" : "31527410599"
}
},
"kycSplitStart" : 63,
"kycDocsLength" : 4,
"eSignProperties" : {
"signername" : "Ritesh Rajendra Nagpure"
},
"formSubmittedDate" : ISODate("2018-08-27T10:48:51.377Z"),
"ucccode" : "177762",
"crmPaymentSynced" : true,
"passwordResetCode" : "",
"lastPaymentDate" : ISODate("2018-10-26T09:55:24.149Z"),
"registrationWorkflowId" : 0,
"currentWorkflowId" : 0,
"isPaymentFromLedger" : false,
"lastLoginDate" : ISODate("2019-08-28T12:14:58.569Z"),
"viewedPreSales" : true
}
I have written this query -
"collection": "user",
"aggregate": [
{
"$match": {
"$or" : [
{
"$and" : [
{"txn.I_C" : {"$exists": true}}, {"txn.I_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
},
{
"$and" : [
{"txn.L2_C" : {"$exists": true}}, {"txn.L2_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
}
]}
},
{ "$addFields" : {"date" :{"$cond": { "if": {"txn.I_C" : {"$exists" : true}}, "then": "txn.I_C", "else": "txn.L2_C"}}}},
{
"$project" : {
"_id" : 0,
"datePartDay": {
"$concat": [
{
"$substr": [
{
"$year": "$date"
},
0,
4
]
},
"-",
{
"$substr": [
{
"$month": "$date"
},
0,
2
]
},
"-",
{
"$substr": [
{
"$dayOfMonth": "$date"
},
0,
2
]
}
]
}
}
},
{
"$group": {
"_id": "$datePartDay",
"count": {
"$sum": 1
}
}
}
]
}
Try this way, please:
"collection": "user",
"aggregate": [
{
"$match": {
"$or" : [
{
"$and" : [
{"txn.I_C" : {"$exists": true}}, {"txn.I_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
},
{
"$and" : [
{"txn.L2_C" : {"$exists": true}}, {"txn.L2_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
}
]}
},
{ "$addFields" : {"date" :{"$cond": {"if": {"$ne" : ["$txn.I_C", undefined]}, "then": "$txn.I_C", "else": "$txn.L2_C"}}}},
{
"$project" : {
"_id" : 0,
"datePartDay": {
"$concat": [
{
"$substr": [
{
"$year": "$date"
},
0,
4
]
},
"-",
{
"$substr": [
{
"$month": "$date"
},
0,
2
]
},
"-",
{
"$substr": [
{
"$dayOfMonth": "$date"
},
0,
2
]
}
]
}
}
},
{
"$group": {
"_id": "$datePartDay",
"count": {
"$sum": 1
}
}
}
]
}

MongoDB shell mongoimport not importing document from json file

Can someone help me figure out what the problem is with this cos i have been trying to get it work but all efforts avail abortive
mongoimport --db test --collection products --drop --file"C:\Program Files\MongoDB\Server\scripts\products.json"
running the above on my shell keeps given me the below error
2018-01-13T19:55:55.019+0100 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:14
======================Update=========================
{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "warranty_years" : 1, "available" : true }
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "warranty_years" : 1, "available" : false }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bf9" }, "name" : "AC3 Series Charger", "type" : [ "accessory", "charger" ], "price" : 19, "warranty_years" : 0.25, "for" : [ "ac3", "ac7", "ac9" ] }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfa" }, "name" : "AC3 Case Green", "type" : [ "accessory", "case" ], "color" : "green", "price" : 12, "warranty_years" : 0 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfb" }, "name" : "Phone Extended Warranty", "type" : "warranty", "price" : 38, "warranty_years" : 2, "for" : [ "ac3", "ac7", "ac9", "qp7", "qp8", "qp9" ] }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfc" }, "name" : "AC3 Case Black", "type" : [ "accessory", "case" ], "color" : "black", "price" : 12.5, "warranty_years" : 0.25, "available" : false, "for" : "ac3" }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfd" }, "name" : "AC3 Case Red", "type" : [ "accessory", "case" ], "color" : "red", "price" : 12, "warranty_years" : 0.25, "available" : true, "for" : "ac3" }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfe" }, "name" : "Phone Service Basic Plan", "type" : "service", "monthly_price" : 40, "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } }, "term_years" : 2 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bff" }, "name" : "Phone Service Core Plan", "type" : "service", "monthly_price" : 60, "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "term_years" : 1 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15c00" }, "name" : "Phone Service Family Plan", "type" : "service", "monthly_price" : 90, "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "sales_tax" : true, "term_years" : 2 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15c01" }, "name" : "Cable TV Basic Service Package", "type" : "tv", "monthly_price" : 50, "term_years" : 2, "cancel_penalty" : 25, "sales_tax" : true, "additional_tarriffs" : [ { "kind" : "federal tarriff", "amount" : { "percent_of_service" : 0.06 } }, { "kind" : "misc tarriff", "amount" : 2.25 } ] }

Need Help Join Collection

Collection Inventory sample data:
{
"_id" : "89011704252315531324",
"sku" : "A2015-01-000",
"type" : "package",
"status" : "active",
"lng" : "-72.789153",
"lat" : "44.173515",
"acq" : "28",
"gtime" : ISODate("2017-01-11T22:27:48.000Z"),
"qlng" : "-72.796501",
"qlat" : "44.214783",
"qtime" : ISODate("2016-11-27T18:21:10.000Z"),
"timestamp" : ISODate("2017-01-12T14:43:29.000Z"),
"modified" : Date(-62135596800000),
"battery" : "60",
"wearables" : [
{
"_id" : "0009003C100228234E45",
"type" : "wearable",
"status" : "active",
"battery" : "50",
"timestamp" : ISODate("2017-01-12T11:43:33.000Z")
},
{
"_id" : "004A003F200B36634E45",
"type" : "cradle",
"status" : "active",
"battery" : "64",
"timestamp" : ISODate("2017-01-11T22:27:26.000Z")
},
{
"_id" : "11223344556600000B55",
"type" : "falldetect",
"status" : "active",
"battery" : "64",
"timestamp" : ISODate("2017-01-12T08:43:29.000Z")
}
],
"company" : "ConnectAmericaProduction",
"companies" : [],
"remoteIp" : "172.31.45.196:53864",
"subscriber" : "5783e20aa2c89f346e000006",
"ring" : "90",
"speaker" : "90",
"mic" : "55",
"version" : "4352",
"cradle" : "OFF",
"ctime" : ISODate("2017-01-11T23:13:59.000Z"),
"csqtime" : Date(-62135596800000)
}
collection calllog sample data
{
"_id" : "89011704252315531324",
"cdr" : [
{
"direction" : "Outgoing",
"duration" : 46,
"timestamp" : ISODate("2016-11-23T03:25:06.000Z"),
"number" : "",
"name" : "Call Center",
"lng" : "-71.208061",
"lat" : "42.330265",
"acq" : "",
"timezone" : {
"dstOffset" : 0.0,
"rawOffset" : 0.0,
"status" : "",
"timeZoneId" : "",
"timeZoneName" : ""
}
},
{
"direction" : "Incoming",
"duration" : 51,
"timestamp" : ISODate("2016-11-23T03:26:02.000Z"),
"number" : "",
"name" : "Call Center",
"lng" : "-71.205727",
"lat" : "42.333347",
"acq" : "",
"timezone" : {
"dstOffset" : 0.0,
"rawOffset" : 0.0,
"status" : "",
"timeZoneId" : "",
"timeZoneName" : ""
}
},
{
"direction" : "Outgoing",
"duration" : 49,
"timestamp" : ISODate("2016-11-27T18:21:04.000Z"),
"number" : "",
"name" : "Call Center",
"lng" : "-72.796501",
"lat" : "44.214783",
"acq" : "",
"timezone" : {
"dstOffset" : 0.0,
"rawOffset" : -18000.0,
"status" : "OK",
"timeZoneId" : "America/New_York",
"timeZoneName" : "Eastern Standard Time"
}
}
]
}
after run this aggrgrate function
db.calllog.aggregate([{$unwind: "$cdr"}, {$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as: "wearables" }}, { "$project": { "cdr.direction": 1, "cdr.duration": 1,"cdr.date": 1,"wearables.type": 1, "wearables.status": 1, "wearables.battery": 1} }])
Result:
{ "_id" : "89011704252315531324", "cdr" : { "direction" : "Outgoing", "duration" : 46 }, "wearables" : [ { "type" : "package", "status" : "active", "battery" : "60" } ] }
{ "_id" : "89011704252315531324", "cdr" : { "direction" : "Incoming", "duration" : 51 }, "wearables" : [ { "type" : "package", "status" : "active", "battery" : "60" } ] }
{ "_id" : "89011704252315531324", "cdr" : { "direction" : "Outgoing", "duration" : 49 }, "wearables" : [ { "type" : "package", "status" : "active", "battery" : "60" } ] }
needed help can not get query to shows wearables type such as wearable, cradle, falldetect
thank
Did you try to do wearables.wearables.type? When you call wearables.type, you are actually getting the type of the Inventory. If you need the type of the wearable that is inside the inventory. You need to put inventory.wearables.type. The initial problem is that you're calling the inventory "wearables" and making a confusion out of it.
I would do the following:
db.calllog.aggregate([{$unwind: "$cdr"},
{$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as: "inventory" }},
{$unwind: "$inventory.wearables"},
{ "$project": {
"cdr.direction": 1,
"cdr.duration": 1,
"cdr.date": 1,
"inventory.type": 1,
"inventory.status": 1,
"inventory.battery": 1,
"inventory.wearables.type":1
}}])

update on nested mongodb document

well i am looking for solution where i can update deeply nested mongodb document,
I tried with $ sign but again it has an limitation that it can be used only once which doesn't help the cause. So, is there any way by which i can do this.
my documents looks like
{
"_id" : ObjectId("56fa1a0d8c54754a27ab7a50"),
"name" : "A",
"location" : [
{
"state" : "MH",
"city" : [
{
"cityID" : 11,
"cityName" : "nashik"
},
{
"cityID" : 12,
"cityName" : "mumbai"
},
{
"cityID" : 13,
"cityName" : "pune"
}
]
},
{
"state" : "GJ",
"city" : [
{
"cityID" : 21,
"cityName" : "gandhiNagar"
},
{
"cityID" : 22,
"cityName" : "surat"
},
{
"cityID" : 23,
"cityName" : "ahemdabad"
}
]
}
]
}
,
{
"_id" : ObjectId("56fa1ab88c54754a27ab7a51"),
"name" : "B",
"location" : [
{
"state" : "MH",
"city" : [
{
"cityID" : 11,
"cityName" : "nashik"
},
{
"cityID" : 12,
"cityName" : "mumbai"
},
{
"cityID" : 13,
"cityName" : "pune"
}
]
},
{
"state" : "GJ",
"city" : [
{
"cityID" : 21,
"cityName" : "gandhiNagar"
},
{
"cityID" : 22,
"cityName" : "surat"
},
{
"cityID" : 23,
"cityName" : "ahemdabad"
}
]
}
]
}
,
{
"_id" : ObjectId("56fa1ac98c54754a27ab7a52"),
"name" : "A",
"location" : [
{
"state" : "MH",
"city" : [
{
"cityID" : 11,
"cityName" : "nashik"
},
{
"cityID" : 12,
"cityName" : "mumbai"
},
{
"cityID" : 13,
"cityName" : "pune"
}
]
},
{
"state" : "GJ",
"city" : [
{
"cityID" : 21,
"cityName" : "gandhiNagar"
},
{
"cityID" : 22,
"cityName" : "surat"
},
{
"cityID" : 23,
"cityName" : "ahemdabad"
}
]
}
]
}
So now i want to update the field cityName where name:"A" and cityID:23
for this i have written one query but it has static index,
db.LevelTest.update({"name":"A","location.city.cityID":23},{$set:{"location.$.city.2.cityName":"newCity"}},false,true)
So is there any way that I can write a generic query instead of providing static index of an array element, or any other approach,
or changing the structure is the only solution for this?

Get all array subdocuments with a conditional in MongoDB

I have a collection with lot of documents like this:
{
"Items" : [],
"Technicians" : [],
"_id" : ObjectId("537b5ea4c61b1d1743f4341f"),
"budgets" : [
{
"concepts" : [
{
"position" : 0,
"description" : "A",
"price" : "1",
"qty" : "11",
"total" : 11
},
{
"position" : 1,
"description" : "A",
"price" : "2",
"qty" : "22",
"total" : 44
},
{
"position" : 2,
"description" : "A",
"price" : "3",
"qty" : "33",
"total" : 99
},
{
"position" : 3,
"description" : "A",
"price" : "4",
"qty" : "44",
"total" : 176
}
],
"date" : "2014-05-21T10:20:48.696Z",
"id" : 9989,
"status" : "joooder",
"total" : 500
},
{
"id" : 260,
"date" : "2014-05-22T11:12:40.260Z",
"concepts" : [
{
"position" : 0,
"description" : "Nueva",
"price" : "1",
"qty" : "1",
"total" : 1
}
],
"total" : 1,
"status" : "pending"
},
{
"id" : 111,
"date" : "2014-05-22T13:36:28.111Z",
"concepts" : [
{
"position" : 0,
"description" : "Blabla",
"price" : "1",
"qty" : "11",
"total" : 11
}
],
"total" : 11,
"status" : "pending"
}
]
}
Now, I want to get the budgets, but only the budgets if status is quals to pending, but I need to find in all documents of collection.
I tried this:
db.orders.aggregate(
{ $unwind : "$budgets" },
{ $match : {
"budgets.status": "pending"
}});
But this isn't ok...
Edit two:
With this:
db.orders.aggregate(
{ $project : {
budgets : 1
}},
{ $match : {
"budgets.status": "pending"
}});
I get this:
{
"result" : [
{
"_id" : ObjectId("537b5ea4c61b1d1743f4341f"),
"budgets" : [
{
"concepts" : [
{
"position" : 0,
"description" : "A",
"price" : "1",
"qty" : "11",
"total" : 11
},
{
"position" : 1,
"description" : "A",
"price" : "2",
"qty" : "22",
"total" : 44
},
{
"position" : 2,
"description" : "A",
"price" : "3",
"qty" : "33",
"total" : 99
},
{
"position" : 3,
"description" : "A",
"price" : "4",
"qty" : "44",
"total" : 176
}
],
"date" : "2014-05-21T10:20:48.696Z",
"id" : 9989,
"status" : "joooder",
"total" : 500
},
{
"id" : 260,
"date" : "2014-05-22T11:12:40.260Z",
"concepts" : [
{
"position" : 0,
"description" : "Nueva",
"price" : "1",
"qty" : "1",
"total" : 1
}
],
"total" : 1,
"status" : "pending"
},
{
"id" : 111,
"date" : "2014-05-22T13:36:28.111Z",
"concepts" : [
{
"position" : 0,
"description" : "Blabla",
"price" : "1",
"qty" : "11",
"total" : 11
}
],
"total" : 11,
"status" : "pending"
}
]
}
],
"ok" : 1
}
The first status item isn't pending.