Need updating a document through MongoDB - arrays

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

Related

Update value of key in Object in nested array of objects in MongoDB

I am trying to update data of "array1.array2._id": ObjectId("627a6fab60dc3c523b396af1") and Set Name to John But it's updating in all array2's first element's name to John.
db.getCollection('tests')
.updateOne({ "array1.array2._id": ObjectId("627a6fab60dc3c523b396af1") },{ $set: { "array1.$[].array2.$.name" : "John" } })
{
"_id" : ObjectId("627a6fab60dc3c523b396aec"),
"array1" : [
{
"array2" : [
{
"_id" : ObjectId("627a6fab60dc3c523b396af1"),
"name" : "test"
},
{
"_id" : ObjectId("627a6fab60dc3c523b396af2"),
"name" : "ABC"
}
],
"_id" : ObjectId("627a6fab60dc3c523b396aed")
},
{
"array2" : [
{
"_id" : ObjectId("627a6fab60dc3c523b396af3"),
"name" : "XYZ"
},
{
"_id" : ObjectId("627a6fab60dc3c523b396af4"),
"name" : "Testing"
}
],
"_id" : ObjectId("627a6fab60dc3c523b396aee")
}
]
}
Based on this great answer by #R2D2, you can do:
db.collection.update({
"array1.array2._id": ObjectId("627a6fab60dc3c523b396af1")
},
{
$set: {
"array1.$[].array2.$[y].name": "John"
}
},
{
arrayFilters: [
{
"y._id": ObjectId("627a6fab60dc3c523b396af1")
}
]
})
As you can see on this playground example

Is there any reason why this code wouldn't update my 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!

Update an element of an Array in a Document in MongoDB with Mongoose

I have a MongoDB database whith a collection with this structure:
{
"_id" : ObjectId("5b670eefe94672265ca59428"),
"duration" : {
"start" : ISODate("2018-09-01T00:00:00.000Z"),
"end" : ISODate("2018-09-01T00:00:00.000Z")
},
"title" : "Example title.",
"description" : "<p>Hi example!</p>",
"slug" : "title",
"insertDate" : ISODate("2018-08-05T14:51:27.194Z"),
"webData" : [
{
"_id" : ObjectId("5bb1f082931c536950ade361"),
"webCode" : "be_mx",
"categories" : [
{
"_id" : ObjectId("3sdf43f34543et35tret435"),
"name" : "Category 1",
"webCode" : "be_mx",
"slug" : "category-1"
},{
"_id" : ObjectId("3sdf43f34543et35tretert"),
"name" : "Category 2",
"webCode" : "be_mx",
"slug" : "category-2"
}
]
}
],
"__v" : 6,
"lastEditionDate" : ISODate("2018-10-01T10:01:38.889Z")
}
I want to update all documents from this collection that has a categorie with a _id = "3sdf43f34543et35tret435" (in this example the "Category 1"), and I want to update this element category setting, for example, the slug to "category-3".
I tried to do that with this code:
Collection.update({
"webData.categories": {
$elemMatch: {
_id: ObjectId("3sdf43f34543et35tret435")
}
}
}, {
$set: {
webData: {
"categories.$.slug": "category-3"
}
}
}, {
multi: true
}, (err, res) => {
if (err) { console.log(err); }
console.log(res);
});
When I execute that, all the documents that have this category are edited but it's wrong because all the related categories are deleted.
Can you help me with this operation?
Thank you!
EDITED:
When I execute in my DB this query:
db.getCollection("scholarships").update({},
{ '$set': { 'webData.[].categories.$[category].slug': "nana" } },
{ multi: true, arrayFilters: [{ 'category._id': ObjectId("5b719d821f3f1131ec4524f6") }] })
Using in this time an arrayFilter, I receive this error:
The path 'webData.[].categories' must exist in the document in order to apply array updates."

Not able to write the query in mongo for array element match?

I am working on project on which I had to make recommendation system for users in website.I am new to Mongodb.
I want to retrieve the names/id of users who have "frnds.type"=1 in below code.
{
"_id" : ObjectId("56a9fcc15b4e12369150d6ef"),
"name" : "Udit",
"venue" : {
"state" : "Rajasthan",
"city" : "Jaipur",
"ll" : [
"26.9000",
"75.8000"
]
},
"lsv" : [
0.14,
0.18,
0.24,
0.17,
0.05,
0.17,
0.05
],
"username" : "udit",
"frnds" : [
{
"id" : "amit",
"type" : 1
},
{
"id" : "nakul",
"type" : 0
},
{
"id" : "verma",
"type" : 1
}
]
}
I have written one query but it is giving wrong results
db.users.find({"username":"udit"},{"frnds":{"$elemMatch":{"type":1}}}).pretty()
I want result in this manner :
[
{
"id":"amit",
"type":1
},
{
"id":"verma",
"type":1
}
]
Try with Aggregation Framework as below.
db.users.aggregate([{
$match: {username: 'udit'},
{$unwind: '$frnds'},
{$match: {'frnds.type': 1}},
{$group: {frnds: {$push: "$frnds"}}
}]);

How to update field in document sub-array in mongo

In a database with these documents
{
"_id" : ObjectId("5448f241e47010336375bb21"),
"lid" : "Ridali",
"items" : [
{
"product" : "dogfood", ,
"done" : false
}
],
}
{
"_id" : ObjectId("5448fc15e47010336375bb23"),
"lid" : "Qitula",
"items" : [
{
"product" : "measure kitchen cab",
"done" : false,
"tags" : [
"11x30"
]
},
{
"product" : "radiators",
"done" : true
},
{
"product" : "dogfood",
"done" : true
}
],
}
How would I update dogfood to catfood for lid:"Qitula" ? I have tried variations of
db.lists.update({lid: "Qitula","items: {$elemMatch: {product: "dogfood"}}},{$set: {"items.product": "catfood"}})
without success.
db.lists.update({lid: "Qitula","items.product": "dogfood" }, { $set : { "items.$.product" : "catfood" }})
Check documentation - http://docs.mongodb.org/manual/reference/operator/update/positional/#up.S
To update subdocument you need to use the positional $ operator
db.lists.update({
lid: "Qitula",
items: {$elemMatch: {product: "dogfood"}}
},
{$set: {"items.$.product": "catfood"}
})

Resources