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
Related
{ "_id" : ObjectId("6068b4d1ba926fd240c5216e"), "name" : "Rohit", "branch" : "CSE", "joiningYear" : 2018, "language" : [ "C#", "Python", "Java" ], "personal" : { "contactinfo" : 0, "state" : "Delhi", "age" : 24, "semestermarks" : [ 70, 73.3, 76.5, 78.9 ] }, "salary" : 1000 }
{ "_id" : ObjectId("6068b4d1ba926fd240c5216f"), "name" : "Amit", "branch" : "ECE", "joiningYear" : 2017, "language" : [ "Python", "C#" ], "personal" : { "contactinfo" : 234556789, "state" : "UP", "age" : 25, "semestermarks" : [ 80, 80.1, 98, 70 ] }, "salary" : 10000 }
{ "_id" : ObjectId("6068b4d1ba926fd240c52170"), "name" : "Sumit", "branch" : "CSE", "joiningYear" : 2017, "language" : [ "Java", "Perl" ], "personal" : { "contactinfo" : 2300056789, "state" : "MP", "age" : 24, "semestermarks" : [ 89, 80.1, 78, 71 ] } }
{ "_id" : ObjectId("606ac8d9a4cfca3993d52732"), "name" : "Mohit", "branch" : "EEE", "joiningYear" : 2019, "language" : [ "Python", "PhP", "Java" ], "personal" : { "contactinfo" : 489395205, "state" : "Rajasthan", "age" : 23, "semesterMarks" : [ 75.6, 74, 86, 50 ] }, "salary" : 20000 }
{ "_id" : ObjectId("606ac8d9a4cfca3993d52733"), "name" : "Krant", "branch" : "ECE", "joiningYear" : 2017, "language" : [ "C++", "Java", "R" ], "personal" : { "contactinfo" : 489395455, "state" : "Gujrat", "age" : 26, "semesterMarks" : [ 55.6, 64, 46, 50 ] }, "salary" : 10000 }
{ "_id" : ObjectId("606ac8d9a4cfca3993d52734"), "name" : "Jitesh", "branch" : "CSE", "joiningYear" : 2018, "language" : [ "C#", "PhP", "Java" ], "personal" : { "contactinfo" : 666795205, "state" : "Tamil Nadu", "age" : 27, "semesterMarks" : [ 65.6, 84, 71, 58 ] }, "salary" : 25000 }
{ "_id" : ObjectId("606ac8d9a4cfca3993d52735"), "name" : "Sino", "branch" : "EEE", "joiningYear" : 2019, "language" : [ "Python", "PhP", "Php" ], "personal" : { "contactinfo" : 999995205, "state" : "Kerla", "age" : 27, "semesterMarks" : [ 85.6, 64, 86.4, 70 ] }, "salary" : 30000 }
db.college.find().sort({semesterMarks:1})
db.college.find().sort({semesterMarks:-1})
semesterMarks is inside personal
https://docs.mongodb.com/manual/reference/method/cursor.sort/
db.collection.find({}).sort({"personal.semesterMarks":1})
I have object in my procedures collection.
I want to update item_status of item_id 5996c80fca423ce1228f7690 which is available in preferences.items.
I want to get all records who has mentioned item_id so that we can update all occurrences.
{
"_id" : ObjectId("5996d0a1ca423ce1228f777a"),
"status" : "Active",
"procedure_name" : "ATHA",
"procedure_for" : "Admin",
"created_by" : "5940c3e8734d1d79866338cf",
"created_on" : ISODate("2017-10-19T18:44:22.702+0000"),
"speciality_id" : "5751131e3a1253845560a984",
"speciality" : "Orthopdics",
"master_template_id" : "",
"hospital_id" : "",
"surgeon_nurse_id" : "",
"procedure_type" : "Published",
"published_on" : ISODate("2017-10-19T18:44:22.702+0000"),
"surgical_sequence" : [
],
"preferences" : [
{
"category_id" : "5971fae84658f5241d8a5b70",
"category" : "Instruments",
"_id" : ObjectId("59e8f2861c999f292a837304"),
"items" : [
{
"item_id" : "5996c80fca423ce1228f7690",
"item_name" : "Battery",
"item_description" : "",
"image_name" : "BATTERY.png",
"icon_image" : "",
"side_view_image" : "",
"_id" : ObjectId("59e8f2861c999f292a837306"),
"attributes" : [
],
"subcategory" : [
{
"name" : "Power tool",
"subcategory_id" : "5996c80eca423ce1228f7549",
"parent_id" : "",
"_id" : ObjectId("5996c80fca423ce1228f7709")
},
{
"name" : "Battery",
"subcategory_id" : "5996c80eca423ce1228f750e",
"parent_id" : "5996c80eca423ce1228f7549",
"_id" : ObjectId("5996c80fca423ce1228f7708")
}
]
}
]
}
],
"__v" : NumberInt(0),
"modified_by" : "5940c3e8734d1d79866338cf",
"modified_on" : ISODate("2017-10-19T18:44:22.702+0000")
}
I'm assuming you are talking about updating status field since I don't find any field with the name item_status. If that's the case, the below query should work:
> db.procedures.updateMany({"preferences.$.items.$.item_id": "5996c80fca423ce1228f7690"}, {"$set": {"status": "new_status"}})
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?
I 'm trying to return a list of unique titles by the same user/author. I used
posts.find(
{'student':user , 'class_number':0}
).sort('created', -1).limit(num).toArray(function(err, items) {
which works well as long as there is a 'class_number':0 which there may not always be. So I tried using distinct but I'm not sure how I can provide a query for unique titles. Is there a better way to do this? Here's some data:
posts.distinct(
{'student':user , 'title':?}
)
.sort('created', -1).limit(num).toArray(function(err, items) {
Data:
{
"_id" : ObjectId("53aa0093e99034914b000003"),
"ans" : 1,
"author" : "niko",
"class_number" : 1,
"comments" : [ ],
"copy" : false,
"created" : 1403650195222,
"current" : "",
"memorized" : 1,
"permalink" : "newtest_106677414836872",
"rem" : 257,
"student" : "niko",
"tags" : [ ],
"title" : "newtest"
}
{
"_id" : ObjectId("53aa0093e99034914b000004"),
"ans" : 1,
"author" : "niko",
"class_number" : 2,
"comments" : [ ],
"copy" : false,
"created" : 1403650195253,
"current" : "",
"memorized" : 1,
"permalink" : "newtest_91237262691445",
"rem" : 603,
"student" : "niko",
"tags" : [ ],
"title" : "newtest"
}
{
"_id" : ObjectId("53aa0093e99034914b000002"),
"ans" : 2,
"author" : "niko",
"class_number" : 0,
"comments" : [ ],
"copy" : false,
"created" : 1403650195217,
"current" : "",
"memorized" : 1,
"permalink" : "newtest_5614600780868",
"rem" : 391,
"student" : "niko",
"tags" : [ ],
"title" : "newtest"
}
{
"title" : "rockin",
"author" : "niko",
"student" : "niko",
"current" : "",
"permalink" : "rockin_44917561926464",
"tags" : [ ],
"comments" : [ ],
"created" : 1403673810202,
"memorized" : 1,
"ans" : 1,
"rem" : 1,
"copy" : false,
"class_number" : 0,
"_id" : ObjectId("53aa5cd2ff9163968f000002")
}
{
"title" : "rockin",
"author" : "niko",
"student" : "niko",
"current" : "",
"permalink" : "rockin_68780016699996",
"tags" : [ ],
"comments" : [ ],
"created" : 1403673810204,
"memorized" : 1,
"ans" : 1,
"rem" : 1,
"copy" : false,
"class_number" : 1,
"_id" : ObjectId("53aa5cd2ff9163968f000003")
}
If you are looking for matching the "student" to the "author" then your best bet for hitting this all in one go is the aggregation framework:
db.collection.aggregate([
{ "$group": {
"_id": {
"user": {
"$cond": [
{ "$eq": [ "$student", "$author" ] },
"$student",
false
]
},
"title": "$title"
}
}},
{ "$match": { "_id.user": { "$ne": false } }}
])
So the $group brings all the "distinct" combinations of "user" and "title" together while conditionally evaluating under the $cond operator whether those fields are matching or not.
The $match just filters out those distinct "titles" where the user match evaluated to false.
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.