update on nested mongodb document - mongodb

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?

Related

how to change the data of multiple array of object in 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

$map upto three nested array mongodb aggregation

I´m facing a challenge here. I have this collection here
{
"_id" : ObjectId("5e0ff6d424f9fc12bc3d9464"),
"name" : "Pizzaria Don Juan",
"active" : true,
"branches" : [
{
"location" : {
"type" : "Point",
"coordinates" : [ ]
},
"_id" : ObjectId("5e19cafc31d60216b8dbd649"),
"name" : "Parque da Mooca",
"address" : "Rua Dianópolis",
"addressNumber" : 1283,
"federalId" : "10.445.089/0001-44",
"complement" : "Ap 55",
"postalCode" : "03126-007",
"coveredArea" : 0,
"neighborhood" : "Parque da Mooca",
"deliveryTime" : 0,
"deliveryRate" : 0,
"standard" : false,
"city" : "Mococa",
"state" : "RJ",
"emails" : [ ],
"phones" : [ ],
"daysWeek" : [ ],
"socialMedias" : [ ],
"paymentTerms" : [ ],
"sections" : [ ]
},
{
"location" : {
"type" : "Point",
"coordinates" : [ ]
},
"_id" : ObjectId("5e19c9a531d60216b8dbd639"),
"name" : "Principal",
"address" : "Rua Nicolau Filizola",
"addressNumber" : null,
"federalId" : "10.445.089/0001-53",
"complement" : "",
"postalCode" : "05547-010",
"coveredArea" : 0,
"neighborhood" : "Jardim Rosa Maria",
"deliveryTime" : 0,
"deliveryRate" : 0,
"standard" : true,
"city" : "São Paulo",
"state" : "SP",
"emails" : [
{
"_id" : ObjectId("5e19ca9531d60216b8dbd643"),
"name" : "Contato",
"address" : "contato#pizzariadonjuan.com.br"
},
{
"_id" : ObjectId("5e19ca9531d60216b8dbd642"),
"name" : "Contato2",
"address" : "contato2#pizzariadonjuan.com.br"
}
],
"phones" : [
{
"_id" : ObjectId("5e19ca9531d60216b8dbd645"),
"name" : "Principal",
"number" : "(11) 99740-2216"
},
{
"_id" : ObjectId("5e19ca9531d60216b8dbd644"),
"name" : "Secundario",
"number" : "(11) 2562-2759"
}
],
"daysWeek" : [
{
"_id" : ObjectId("5e1cf99741c52d4a587a9162"),
"startsAt" : 64800000,
"endsAt" : 82800000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a124a17fd054900a1afb2")
},
{
"_id" : ObjectId("5e1cf99741c52d4a587a9161"),
"startsAt" : 0,
"endsAt" : 0,
"opens" : false,
"dayWeekId" : ObjectId("5e1a126817fd054900a1afb3")
},
{
"_id" : ObjectId("5e1cfbed41c52d4a587a9170"),
"startsAt" : 64980000,
"endsAt" : 82800000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a126e17fd054900a1afb4")
},
{
"_id" : ObjectId("5e1b8fac96516432845e364c"),
"startsAt" : 64980000,
"endsAt" : 82800000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a127517fd054900a1afb5")
},
{
"_id" : ObjectId("5e1cfbed41c52d4a587a916f"),
"startsAt" : 64980000,
"endsAt" : 82800000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a127a17fd054900a1afb6")
},
{
"_id" : ObjectId("5e1cfbed41c52d4a587a916e"),
"startsAt" : 64800000,
"endsAt" : 82800000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a23f8bf353f493c74e8ae")
},
{
"_id" : ObjectId("5e1cfbed41c52d4a587a916d"),
"startsAt" : 61380000,
"endsAt" : 83154000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a2407bf353f493c74e8af")
}
],
"socialMedias" : [
{
"_id" : ObjectId("5e1d082641c52d4a587a9191"),
"socialMediaId" : ObjectId("5e10089a3330ad05d4e1867d"),
"url" : "rewrwerwerwerwerwerwerwer"
}
],
"paymentTerms" : [
{
"_id" : ObjectId("5e1d143041c52d4a587a91b7"),
"paymentTermId" : ObjectId("5e1a2277bf353f493c74e8a7")
},
{
"_id" : ObjectId("5e1d143041c52d4a587a91b6"),
"paymentTermId" : ObjectId("5e1a228cbf353f493c74e8a8")
},
{
"_id" : ObjectId("5e1d143041c52d4a587a91b5"),
"paymentTermId" : ObjectId("5e1a229ebf353f493c74e8a9")
}
],
"sections" : [
{
"_id" : ObjectId("5e1e535441c52d4a587a9208"),
"name" : "Camisetas",
"products" : [
{
"_id" : ObjectId("5e1e662f044582129844ffd5"),
"name" : "DonJuan M",
"description" : "",
"quantityAvailable" : 0,
"image" : "",
"price" : 0,
"validFrom" : ISODate("2020-01-15T01:08:49.552Z"),
"validTo" : ISODate("2020-01-15T01:08:49.552Z"),
"enabled" : true
}
]
},
{
"_id" : ObjectId("5e20ec889c05f229a484ea3d"),
"name" : "Imãs",
"products" : [
{
"_id" : ObjectId("5e20ec889c05f229a484ea3e"),
"name" : "Imã",
"description" : "Imã",
"quantityAvailable" : 0,
"image" : "",
"price" : 0,
"validFrom" : ISODate("0001-01-01T00:00:00Z"),
"validTo" : ISODate("9999-12-31T00:00:00Z"),
"enabled" : true
}
]
}
]
}
],
"users" : [
{
"_id" : ObjectId("5e10fc2adc147a373c312144")
},
{
"_id" : ObjectId("5e11ff8003eb832ef84342a6")
}
],
"socialMedias" : [
{
"_id" : ObjectId("5e165672a2204b49c892db74"),
"socialMediaId" : ObjectId("5e10089a3330ad05d4e1867d"),
"url" : "uuuutt"
},
{
"_id" : ObjectId("5e15385fb3a0aa1004ac3598"),
"socialMediaId" : ObjectId("5e1009043330ad05d4e1867f"),
"url" : "jkkjkjkjkjk"
}
],
"sections" : [
{
"_id" : ObjectId("5e15313b2e985e16ec4e7413"),
"name" : "Bebidas",
"products" : [
{
"_id" : ObjectId("5e1e6381044582129844ffc2"),
"name" : "Coca Cola Zero 2 Litros",
"description" : "",
"quantityAvailable" : 0,
"image" : "",
"price" : 18.39,
"validFrom" : ISODate("1970-01-01T00:00:00Z"),
"validTo" : ISODate("1970-01-01T00:00:00Z"),
"enabled" : true
},
{
"_id" : ObjectId("5e1e6381044582129844ffc3"),
"name" : "Coca Cola 2 Litros",
"description" : "",
"quantityAvailable" : 0,
"image" : "",
"price" : 21.42,
"validFrom" : ISODate("1970-01-01T00:00:00Z"),
"validTo" : ISODate("1970-01-01T00:00:00Z"),
"enabled" : true
},
{
"_id" : ObjectId("5e1e662f044582129844ffda"),
"name" : "Cerveja Heineken Lata 350ml",
"description" : "Cerveja Heineken Lata 350ml",
"quantityAvailable" : 0,
"image" : "volkswagen-polo.jpg",
"price" : 1.55,
"validFrom" : ISODate("2020-01-01T00:00:00Z"),
"validTo" : ISODate("1970-01-01T00:00:00Z"),
"enabled" : true
}
]
},
{
"_id" : ObjectId("5e20e8de9c05f229a484ea27"),
"name" : "Esfihas",
"products" : [
{
"_id" : ObjectId("5e20e8de9c05f229a484ea28"),
"name" : "Esfiha de carne",
"description" : "Esfiha de carne",
"quantityAvailable" : 0,
"image" : "",
"price" : 5,
"validFrom" : ISODate("2020-01-01T00:00:00Z"),
"validTo" : null,
"enabled" : true
}
]
}
],
"__v" : 0
}
{
"_id" : ObjectId("5e0ffd23991918424c8d7c3b"),
"name" : "Pizza Ruth",
"active" : true,
"users" : [ ],
"socialMedias" : [ ],
"branches" : [ ],
"sections" : [ ],
"__v" : 0
}
{
"_id" : ObjectId("5e0ffd3d991918424c8d7c3c"),
"name" : "Feijão de Corda",
"active" : true,
"users" : [ ],
"socialMedias" : [ ],
"branches" : [ ],
"sections" : [ ],
"__v" : 0
}
The fields validFrom and validTo (Date fields) from the collection products nested in branches.sections need to be converted to the format yyyy-mm-dd. I can do that with this aggregation pipeline:
{ $unwind: { path: "$branches.sections", preserveNullAndEmptyArrays: true } },
{
"$addFields": {
"branches.sections.products": {
$map: {
input: "$branches.sections.products",
as: "product",
in: {
'_id': "$$product._id",
'name': "$$product.name",
'description': "$$product.description",
'quantityAvailable': "$$product.quantityAvailable",
'image': "$$product.image",
'imageUrl': "$$product.imageUrl",
'price': "$$product.price",
'validFrom' : {"$dateToString": { "date": "$$product.validFrom", "format": "%Y-%d-%m" }},
'validTo' : {"$dateToString": { "date": "$$product.validTo", "format": "%Y-%d-%m" }},
'enabled': "$$product.enabled",
}
}
}
}
}
I can successfully convert those date fields, but I need now to "re-unwind" the array products, in order to be just like before the unwind.
Any clue in how to proceed? Or even a different way to format those dates without having to unwind? Tried dozens of ways of $group, but without any success.
You just need to $map over each nested array to drill upload the validTo and validFrom field
.aggregate([
{ "$addFields": {
"branches": {
"$map": {
"input": "$branches",
"as": "branch",
"in": {
"$mergeObjects": [
"$$branch",
{ "section": {
"$map": {
"input": "$$branch.sections",
"as": "section",
"in": {
"$mergeObjects": [
"$$section",
{ "product": {
"$map": {
"input": "$$section.products",
"as": "product",
"in": {
"$mergeObjects": [
"$$product",
{
"validFrom": { "$dateToString": { "date": "$$product.validFrom", "format": "%Y-%d-%m" }},
"validTo": { "$dateToString": { "date": "$$product.validTo", "format": "%Y-%d-%m" }}
}
]
}
}
}}
]
}
}
}}
]
}
}
}
}}
])
MongoPlayground

How to update array element in mongodb document?

This query is updating successfully:
db.product.updateMany({"Rno":{$in:["111","222","333"]}},{$set:{"subjwmarks.maths":99}});
Before updating:
{
"_id" : ObjectId("5a572886964d881dab9f1f55"),
"Rno" : "111",
"stuname" : "sravan",
"subjwmarks" : [
{
"maths" : 88.0,
"english" : 99.0,
"Clang" : 97.0
}
],
"total" : 284.0,
"grade" : "A",
"organization" : "Tata"
};
After updating the query returns this value:
{
"_id" : ObjectId("5a572886964d881dab9f1f55"),
"Rno" : "111",
"stuname" : "sravan",
"subjwmarks" : [
{
"maths" : 99,
}
],
"total" : 284.0,
"grade" : "A",
"organization" : "Tata"
};
Value should be:
{
"_id" : ObjectId("5a572886964d881dab9f1f55"),
"Rno" : "111",
"stuname" : "sravan",
"subjwmarks" : [
{
"maths" : 99,
"english" : 99.0,
"Clang" : 97.0
}
],
"total" : 284.0,
"grade" : "A",
"organization" : "Tata"
}
But other elements of that array were deleted.
Can anyone suggest how to fix this problem?
db.product.updateMany({"Rno":{$in:["111","222","333"]}, subjwmarks:{ $exists: true}},{$set:{"subjwmarks.$.maths":100}});
https://docs.mongodb.com/manual/reference/operator/update/positional/
Note: subjwmarks must be in query part

Fetch data from mongodb in three different field according to condition

i have a User collection having structure as below
{
"_id" : ObjectId("596845524e20b8328d61b01f"),
"created_at" : ISODate("2017-07-10T04:15:14.327Z"),
"status" : 1,
"updated_at" : ISODate("2017-08-22T07:29:44.000Z"),
"is_activated" : 0,
"totalrating" : 0,
"is_admin" : 0,
"basic_detail" : {
"first_name" : "c",
"last_name" : "f",
"password" : "$2a$10$.WG",
"user_type" : 1,
"full_name" : "c f"
}
},
{
"_id" : ObjectId("596845524e20b8328d61b01d"),
"created_at" : ISODate("2017-07-10T04:15:14.327Z"),
"status" : 1,
"updated_at" : ISODate("2017-08-22T07:29:44.000Z"),
"is_activated" : 0,
"totalrating" : 0,
"is_admin" : 0,
"basic_detail" : {
"first_name" : "c",
"last_name" : "f",
"user_type" : 2,
"full_name" : "c f"
}
},
{
"_id" : ObjectId("596845524e20b8328d61b01g"),
"created_at" : ISODate("2017-07-10T04:15:14.327Z"),
"status" : 1,
"updated_at" : ISODate("2017-08-22T07:29:44.000Z"),
"is_activated" : 0,
"totalrating" : 0,
"is_admin" : 0,
"basic_detail" : {
"first_name" : "c",
"last_name" : "f",
"user_type" : 3,
"full_name" : "c f"
}
}
i want to fetch data through aggreagte query and want data like below
{
"totaluser":3,
"totalho":1,//all those record having user_type=1,
"totaltr":2,//all those record having user_type=2,
"totalcf":3,//all those record having user_type=3
}
trying to do same with the help of group but not able to do please help I am using mongodb 3.4
[{ $facet:
{
totaluser: [ { $count: "totaluser" } ],
totalho: [ {$match:{"basic_detail.user_type":1}},{ $count: "totalho" }],
//other fields
}
},
{
$addFields:{
"totaluser": {
$let:{
vars:{totaluserObj:{ $arrayElemAt: [ "$totaluser", 0 ] }},
in:"$$totaluserObj.totaluser"
}
},
"totalho": {
$let:{
vars:{totalhoObj:{ $arrayElemAt: [ "$totalho", 0 ] }},
in:"$$totalhoObj.totalho"
}
}
}
}]

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.