How to update a collection in mobgo db - mongodb

I am trying to update a collection value with _id 5af5968e17abd4901f4ecfdb.
Query executed:
db.genericcrosssell.config.update(
{_id: '5af5968e17abd4901f4ecfdb'},
{$set: {
"HOTEL_ISSUANCE_EMAIL:{"USE_QUOTATION":1,"CAR_RENTAL":0},
"HOTEL_MY_BOOKING":{"USE_QUOTATION":1,"CAR_RENTAL":0},
"HOTEL_PUSH_NOTIFICATION":{"USE_QUOTATION":0,"CAR_RENTAL":0},
"HOTEL_ISSUING_TRANSITION":{"USE_QUOTATION":0,"CAR_RENTAL":0},
"HOTEL_UPCOMING_TRIP":{"USE_QUOTATION":1,"CAR_RENTAL":0}
}}
)
Getting output as:
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
On executing following query, I am getting response.
db.genericcrosssell.config.find().pretty()
Output:
{
"_id" : ObjectId("5af5968e17abd4901f4ecfdb"),
"FLIGHT_ISSUANCE_EMAIL" : {
"USE_QUOTATION" : 1
},
"HOTEL_ISSUANCE_EMAIL" : {
"USE_QUOTATION" : 1,
"CAR_RENTAL" : 100
}
}
Quoted only 2 values here.

You must pass objectid while passing the value:
Try this:
db.genericcrosssell.config.update({_id: ObjectId('5af5968e17abd4901f4ecfdb')},
{$set: {"HOTEL_ISSUANCE_EMAIL":{"USE_QUOTATION":1,"CAR_RENTAL":0},"HOTEL_MY_BOOKING":{"USE_QUOTATION":1,"CAR_RENTAL":0},"HOTEL_PUSH_NOTIFICATION":{"USE_QUOTATION":0,"CAR_RENTAL":0},"HOTEL_ISSUING_TRANSITION":{"USE_QUOTATION":0,"CAR_RENTAL":0},"HOTEL_UPCOMING_TRIP":{"USE_QUOTATION":1,"CAR_RENTAL":0}}})

Related

how to change data on mongoDb

i have question
{
"_id" : ObjectId("6167c0c907fe8867d48970ce"),
"userMappingId" : "6167bf7307fe8867d48970cd",
"audioUrl" : "abcs2.com/kou.mp3",
"audioDuration" : 11,
"dayId" : 4,
"periodId" : "61652f30919d4616107a4896",
"ctime" : ISODate("2021-10-14T05:31:53.569Z"),
"_class" : "org.kou.xixi"
}
if i want to change ctime how i read the code ?
i have use this
> db.xabs.updateOne({ "_id" : "6167c0c907fe8867d48970ce" },{$set:{"ctime" : "2021-10-13T05:31:53.569Z"})
or use
db.xabs.updateOne({ "userMappingId" : "6167bf7307fe8867d48970cd" },{$set:{"ctime" : "2021-10-13T05:31:53.569Z"})
but my data still cannot change to the new one
Change your _id into ObjectId.
_id: ObjectId("6167c0c907fe8867d48970ce")
db.collection.update({
"_id": ObjectId("6167c0c907fe8867d48970ce")
},
{
$set: {
"ctime": "2021-10-13T05:31:53.569Z"
}
})
As I can see you are finding the record with the id you need to cast the string to ObjectId and second you need to also cast the timestamp using ISODate like following:
db.xabs.updateOne(
{
_id: ObjectId("6167c0c907fe8867d48970ce")
},
{
$set: {
ctime: ISODate("2021-10-13T05:31:53.569Z")
}
}
);
db. xabs.update({ "_id": new ObjectId("6167c0c907fe8867d48970ce")},{ $set: {"ctime": ISODate("2021-10-13T05:31:53.569Z")}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
after i use this code i can run it... it is from documentation for mongoDb Shell 2.6.10
thanks for you all for help me for this issue

How to update hours in data field in mongodb

I have set of mongo document, I need to convert/update the below values like ("workedDate" : ISODate("2020-07-01T00:00:00Z"))
"workedDate" : ISODate("2020-07-01T20:03:04Z"),
"workedDate" : ISODate("2020-07-01T19:59:07Z"),
"workedDate" : ISODate("2020-06-30T14:00:00Z"),
"workedDate" : ISODate("2020-07-01T19:49:29Z")
I have tried the below query:
db.timeentrys.update(
{ },
{
$set: {
workedDate:{$dateFromParts:{
year:{$year:"$workedDate"},
month:{$month:"$workedDate"},
day:{$dayOfMonth:"$workedDate"}
}}
}
}
)
Getting the below error :
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 52,
"errmsg" : "The dollar ($) prefixed field '$dateFromParts' in 'workedDate.$dateFromParts' is not valid for storage."
}
})
$dateFromParts is an aggregation expression. You can only use aggregation expressions in an update if you are using MongoDB 4.2, and provide a pipeline array as the second argument to update instead of an object.
Edit
In this use, just wrap the update object in [] to make it an array:
db.timeentrys.update(
{ },
[{$set: {
workedDate:{$dateFromParts:{
year:{$year:"$workedDate"},
month:{$month:"$workedDate"},
day:{$dayOfMonth:"$workedDate"}
}}
}}]
)

Mongo Db update failing while using $Set for a null valued field

I am trying to update an existing document which has a field with null value in it and I am getting the following error.
Document:
{
"_id" : ObjectId("582299f71e21dbf65027325e"),
"b" : "5555",
"f" : null
}
Query:
db.getCollection('temp').update({"b":"5555"},{"$set":{"f.1.b":1,"f.2.b":2}})
Error:
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "cannot use the part (f of f.1.b) to traverse the element ({f: null})"
}
})
Anyone can tell me why it was not updating the value in the document.
Thank you.
Because there are no f.1.b and f.2.b object found to set
In this case you can try this
Prepare a proper object and try to set
Eg :
var temp = {
1 : {b: 1},
2: {b : 2}
}
db.getCollection('temp').update({"b":"5555"},{"$set":{f:temp}})

MongoDB: updating the value of an inner BSON

I have a document with a schema in mongodb that looks like this:
{
"_id" : ObjectId("572f88424de8c74a69d4558c"),
"storecode" : "ABC",
"credit" : true,
"group" : [
{
"group_name" : "Frequent_Buyer",
"time" : NumberLong("1462732865712"),
}
],
}
I want to add on the _id part for the first object in the array under group so it looks like this:
{
"_id" : ObjectId("572f88424de8c74a69d4558c"),
"storecode" : "ABC",
"credit" : true,
"group" : [
{
"group_name" : "Frequent_Buyer",
"time" : NumberLong("1462732865712"),
"_id" : "573216fee4430577cf35e885"
}
],
}
When I try this code it fails:
db.customer.update({ "_id": ObjectId("572f88424de8c74a69d4558c") },{ "$set": { "group.$._id": "573216fee4430577cf35e885"} })
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: groups.$._id"
}
})
However, if I adjust the code slightly and add on an extra criteria for querying, it works:
db.customer.update({ "_id": ObjectId("572f88424de8c74a69d4558c"), "group.groupname": "Frequent_Buyer" },{ "$set": { "group.$._id": "573216fee4430577cf35e885"} })
Results:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Why did the first command not work but the second command work?
This is the expected result. To use the positional $ update operator, the array field must appear as part of the query document as mentioned in the documentation.
When used with update operations, e.g. db.collection.update() and db.collection.findAndModify(),
the positional $ operator acts as a placeholder for the first element that matches the query document, and
the array field must appear as part of the query document.
For example If you don't want to filter your documents using group_name, simply add group: { "$exists": true } or "group.0": { "$exists": true } to your query criteria. You query will then look like this:
db.customer.updateOne(
{
"_id": ObjectId("572f88424de8c74a69d4558c"),
"group.0": { "$exists": true}
},
{ "$set": { "group.$._id": "573216fee4430577cf35e885" } }
)
Last and not least, you should be using updateOne or updateMany because update is deprecated in official language driver.

Updating a nested mongodb object fails

I have this mongodb data i have stored in this way
db.orders.insert( { _id: ObjectId().str, name: "admin", status: "online",catalog : [
{
"objectid" : ObjectId().str,
"message" : "sold",
"status" : "open"
}
]})
and i am trying to update it in this manner db.orders.update({"_id":"5703b86df3d607cb5fa75ff3"},{$set: {"catalog.message": "added to cart"}})
and this is the error message i am getting
> db.orders.update({"_id":"5703b86df3d607cb5fa75ff3"},{$set: {"catalog.message": "added to cart"}})
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "cannot use the part (catalog of catalog.message) to traverse the element ({catalog: [ { objectid: \"5703b86df3d607cb5fa75ff4\", message: \"sold\", status: \"open\" } ]})"
}
})
How can i update this record?.
According to MongoDB's Examples you should provide the index of the Object in the array which you want to update. Since you want to update the first object i.e. object in the array at index 0 use this :
db.orders.update(
{"_id":"5703b86df3d607cb5fa75ff3"},
{$set: {"catalog.0.message": "added to cart"}});