Is there any reason why this code wouldn't update my database? - database

Trying to run an update call on mongoDB, but when I try and do so, said database isn't updated. Any help here?
The database doc we are trying to edit is in the form of:
{ "_id" : ObjectId("56d5f7eb604eb380b0d8d8dd"), "student_id" : 1, "scores" : [ { "type" : "exam", "score" : 79.51115675153915 }, { "type" : "quiz", "score" : 93.5960500282434 }, { "type" : "homework", "score" : 10.051101066555079 }, { "type" : "homework", "score" : 17.8164864139428 } ], "class_id" : 176 }
And the command being run is:
db.grades.update
(
{
$and:
[
{"class_id": 176},
{"student_id": 1}
]
},
{
$push:
{
scores:
{
"type": "extra credit",
"score": 10
}
}
}
)
Any help is appreciated!

Related

Query in getting value of second attribute after giving value of first attribute inside array in Mongo db

I am a newbie in Mongo .
I have a requirement to build a query from a complex document inside a collection.
{ "_id" : "http://nsgfds.com",
"has Title" : [
{
"title" : {
"language" : "eng",
"value" : "eng title"
},
"origin" : "https://Ignore",
"score" : 7.5
},
{
"title" : {
"language" : "chi",
"value" : "Chinese title"
},
"origin" : "https://Ignore",
"score" : 7.5
},
{
"title" : {
"language" : "vie",
"value" : "Vietnamese title"
},
"origin" : "https://Ignore",
"score" : 7.5
},
{
"title" : {
"language" : "asm",
"value" : "Assamese title"
},
"origin" : "https://Ignore",
"score" : 7.5
}
]
}
My query wants to return value of "value" field when "language" and "_id" values given
suppose when I give "_id" and "language" the query should return only value of the "value" field based on "_id" and "language".
My query:
db.collectionname.aggregate([
{$match: {"_id": "http://nsgfds.com"}},
{
$project: {
hasTitle: {
$filter: {
input: "$hasTitle",
as:"item",
cond: { $eq: ["$title.language:eng", "$$item.value"]}
}
}
}
}
]).pretty();
Expected output:
{
"_id" : "http://nsgfds.com",
"hasTitle" : [{"title" : {"value" : "Wiley Title English title"}]
};
Actual Output:
{
"_id" : "http://nsgfds.com",
"hasTitle" : [
{
"title" : {
"language" : "eng",
"value" : "Wiley Title English title"
},
"origin" : "https://Ignore",
"score" : 7.5
},
{
"title" : {
"language" : "chi",
"value" : "chinese title"
},
"origin" : "https://Ignore",
"score" : 7.5
},
{
"title" : {
"language" : "vie",
"value" : "Vietnamese title"
},
"origin" : "https://Ignore",
"score" : 7.5
},
{
"title" : {
"language" : "asm",
"value" : "Assamese title"
},
"origin" : "https://Ignore",
"score" : 7.5
}
]
}
The error comes from your condition, where you should compare an element of the list ($$item) with a string (eng, asm, shi ...)
{
$eq: [
"$$item.title.language",
"eng"
]
}
But this will give you :
[
{
"_id": "http://nsgfds.com",
"hasTitle": [
{
"origin": "https://Ignore",
"score": 7.5,
"title": {
"language": "eng",
"value": "eng title"
}
}
]
}
]
To get your desired output, you will need another $project
{
$project: {
"hasTitle.title.value": "$hasTitle.title.value"
}
}
result :
[
{
"_id": "http://nsgfds.com",
"hasTitle": [
{
"title": {
"value": [
"eng title"
]
}
}
]
}
]

Navigate thought a lots of array and update value of object with mongo query

Hey guys I'm trying to update a value that is in an array in MongoDB, am trying to use the mongo queries but is not working, am following the next documentation from Mongo doc
this one is the array:
{
"_id" : "605e3d9b9ef219de662113d0",
"distribution" : [
{
"floor" : 1,
"rooms" : [
{
"number" : 301,
"beds" : [
{
"number" : 818,
"status" : "Vacante Sucia"
},
{
"number" : 819,
"status" : "Vacante Sucia"
}
],
"gender" : "M"
},
{
"number" : 302,
"beds" : [
{
"number" : 820,
"status" : "Vacante Sucia"
},
{
"number" : 821,
"status" : "Vacante Sucia"
}
],
"gender" : "M"
},
{
"number" : 303,
"beds" : [
{
"number" : 822,
"status" : "Vacante Sucia"
},
{
"number" : 823,
"status" : "Vacante Sucia"
}
],
"gender" : "M"
}
]
}
],
"name" : "Meteorologia",
"code" : "METEO"
}
this one is the query that is using in mongoDB to update the status from the bed 801, room 301, floor 1:
in the arrayFilters i specified the index 0 to get the first element of the arrays
db.getCollection('establishments_copy').findAndModify({query: { code: "METEO"}, update: { $set: { "distribution.$[i].rooms.$[i].beds.$[i].status": "TEST"}}, arrayFilters: [{"i.rooms": 0, "i.beds": 0, "i.status": 0}]})
they are returning me the collection but without changes, is possible to navigate validating not for the index just with the values.
for example using the next query:
db.getCollection('establishments_copy').findAndModify({query: { code: 'METEO', distribution: { $elemMatch: { floor: 1, 'rooms.number': 301, 'rooms.beds.number': 818}}}, update: { $set: { '...': 'CHANGED'}}})
thanks!
You just need to create separate condition as per sub document's field name,
f for floor field in distribution array
r for number field in rooms array
b for number field in beds array
db.getCollection('establishments_copy').findAndModify({
query: { code: "METEO"},
update: {
$set: {
"distribution.$[f].rooms.$[r].beds.$[b].status": "TEST"
}
},
arrayFilters: [
{ "f.floor": 1 },
{ "r.number": 301 },
{ "b.number": 818 }
]
})
Playground

Need updating a document through MongoDB

This is the Document
{
"_id" : ObjectId("5e945816f935623478eb6cfc"),
"EmpID" : "00102",
"Name" : "Alanna Buggy",
"WeeklySalary" : "426",
"Holidays" : "25 days",
"Supervisor" : [
{
"Bonus" : "15.6%"
}
],
"Trainee" : [
{
"Duration" : "Fully Trained"
}
]
}
I'm trying to update the "bonus" in the "supervisor array but not having any luck, the code I am trying is
db.Employee.updateOne(
{ "Name": "Alanna Buggy" },
{ $set:
{"Supervisor.0.Bonus": "22.5%}
})
and
db.Employee.update(
{"Name" : "Alanna Buggy"},
{$set:
{"Supervisor.0.Bonus":22.5%"}
}})
anyone can help me with this <3 ?
I think your problem is related with quotes
{"Supervisor.0.Bonus": "22.5%}
and
{"Supervisor.0.Bonus":22.5%"}
This sample works
db.getCollection('Employee').updateOne(
{Name: "Alanna Buggy"},
{$set: { "Supervisor.0.Bonus" : "22.5%" }}
)

MongoDB Querying Nested Arrays

I'm having some trouble with querying a Mongo Collection.
I have a Collection like this:
{
"_id" : "555bd34329de3cf232434ef2",
"cars" : [
{
"0" : {
"parts" : [
{
"name" : "x1",
"price" : 12
},
{
"name" : "x2",
"price" : 14
}
]
},
"1" : {
"parts" : [
{
"name" : "y1",
"price" : 8
},
{
"name" : "y2",
"price" : 12
}
]
}
}
]
}
I'd like to return just the following:
"parts" : [
{
"name" : "x1",
"price" : 12
},
{
"name" : "x2",
"price" : 14
}
]
In other words, I need to figure out how to query the Collection by two parameters at the same time:
where the ID matches "555bd34329de3cf232434ef2"
where the "name" of the part matches "x1"
Does anyone know how to do this kind of nested query?
Assuming a document structure like this:
{
"_id" : ObjectId("555bd34329de3cf232434ef2"),
"cars" : [
{
"parts" : [
{
"name" : "x1",
"price" : 12
},
{
"name" : "x2",
"price" : 14
}
]
},
{
"parts" : [
{
"name" : "y1",
"price" : 8
},
{
"name" : "y2",
"price" : 12
}
]
}
]
}
you can run the following query:
db.collection.find({ "_id": ObjectId("555bd34329de3cf232434ef2"), "cars.parts.name" : "x1" }, { "_id": 0, "cars.$": 1 })
which will get you pretty close to where you want to be:
{
"cars" : [
{
"parts" : [
{
"name" : "x1",
"price" : 12
},
{
"name" : "x2",
"price" : 14
}
]
}
]
}
You could get closer using the aggregation framework if that's not good enough...

Mongodb filtering deeply nested arrays with aggregation framework

Thank you in advance for looking at this...
My document:
{
"_id" : { "$oid" : "550b2873e9dd90068070c31b" },
"dateCreated" : { "$date" : 1426794611867 },
"sections" : [
{
"_id" : { "$oid" : "550b2881e9dd90068070c31d" },
"index" : 0,
"name" : "Section 2",
"slides" : [
{
"_id" : { "$oid" : "550b288ce9dd90068070c321" },
"index" : 0,
"status" : "Unpublished"
},
{
"_id" : { "$oid" : "55105b87e9dd90068033ba4a" },
"index" : 1,
"status" : "Published"
}
]
},
{
"_id" : { "$oid" : "550b287ae9dd90068070c31c" },
"index" : 1,
"name" : "Section 1",
"slides" : [
{
"_id": { "$oid": "550b2888e9dd90068070c31f" },
"index" : 0,
"status": "Unpublished"
},
{
"_id" : { "$oid" : "550b288be9dd90068070c320" },
"index" : 1,
"status" : "Unpublished"
}
]
}
]
}
and the desired result where we return only sections with at least one published slide containing all the published slides
{
"_id" : { "$oid" : "550b2873e9dd90068070c31b" },
"dateCreated" : { "$date" : 1426794611867 },
"sections" : [
{
"_id" : { "$oid" : "550b2881e9dd90068070c31d" },
"index" : 0,
"name" : "Section 2",
"slides" : [
{
"_id" : { "$oid" : "55105b87e9dd90068033ba4a" },
"index" : 1,
"status" : "Published"
}
]
}
]
}
So far I have this:
col.aggregate
([
{$match : {'name': name}},
{$unwind:'$sections'},
{$unwind:'$sections.slides'},
{$match:{'$sections.slides.status': "Published"}},
{$group:{_id:'$_id', slides:{$push:'$slides'}}}
])
I am having a hard time wrapping my head around the grouping, specifically with nesting each slides array in its parent section array. Also, I would like to omit any empty sections.
I have never used the aggregate method before but I believe this is the correct approach. The mongo docs are a bit sparse when it comes to deeply nested arrays.
Read about $redact stage of MongoDB's pipeline.
At first iteration I did this:
.aggregate({
$redact: {
$cond: {
if: { $eq: ["$status", "Unpublished"] },
then: "$$PRUNE",
else: "$$DESCEND"
}
}
}, {
$redact: {
$cond: {
if: { $eq: ["$slides", []] },
then: "$$PRUNE",
else: "$$DESCEND"
}
}
})
You can try to rewrite it in more elegant way. I'm pretty sure there is a way to make it with single $redact stage, but you have to skim over the manual and find proper operators. Hope it'll help. Good luck.

Resources