how to change data on mongoDb - 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

Related

How to update a collection in mobgo db

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

What are the efficient query for mongodb if value exist on array then don't update and return the error that id already exist

I have an entry stored on my collection like this:
{
"_id" : ObjectId("5d416c595f19962ff0680dbc"),
"data" : {
"a" : 6,
"b" : [
"5c35f04c4e92b8337885d9a6"
]
},
"image" : "123.jpg",
"hyperlinks" : "google.com",
"expirydate" : ISODate("2019-08-27T06:10:35.074Z"),
"createdate" : ISODate("2019-07-31T10:24:25.311Z"),
"lastmodified" : ISODate("2019-07-31T10:24:25.311Z"),
"__v" : 0
},
{
"_id" : ObjectId("5d416c595f19962ff0680dbd"),
"data" : {
"a" : 90,
"b" : [
"5c35f04c4e92b8337885d9a7"
]
},
"image" : "456.jpg",
"hyperlinks" : "google.com",
"expirydate" : ISODate("2019-08-27T06:10:35.074Z"),
"createdate" : ISODate("2019-07-31T10:24:25.311Z"),
"lastmodified" : ISODate("2019-07-31T10:24:25.311Z"),
"__v" : 0
}
I have to write the query for push userid on b array which is under data object and increment the a counter which is also under data object.
For that, I wrote the Code i.e
db.collection.updateOne({_id: ObjectId("5d416c595f19962ff0680dbd")},
{$inc: {'data.a': 1}, $push: {'data.b': '124sdff54f5s4fg5'}}
)
I also want to check that if that id exist on array then return the response that following id exist, so for that I wrote extra query which will check and if id exist then return the error response that following id exist,
My question is that any single query will do this? Like I don't want to write Two Queries for single task.
Any help is really appreciated for that
You can add one more check in the update query on "data.b". Following would be the query:
db.collection.updateOne(
{
_id: ObjectId("5d416c595f19962ff0680dbd"),
"data.b":{
$ne: "124sdff54f5s4fg5"
}
},
{
$inc: {'data.a': 1},
$push: {'data.b': '124sdff54f5s4fg5'}
}
)
For duplicate entry, you would get the following response:
{ "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0 }
If matched count is 0, you can show the error that the id already exists.
You can use the operator $addToSet to check if the element already exits in the array.
db.collection.updateOne({_id: ObjectId("5d416c595f19962ff0680dbd")},
{$inc: {'data.a': 1}, $addToSet: {'data.b': '124sdff54f5s4fg5'}}
)

How to set keys in mongoDB aggregation?

The idea is to go from a collection of documents like this:
{
"_id" : ObjectId("58ff4fa372ac97344d5672c2"),
"direction" : 1,
"post" : ObjectId("58ff4ea572ac97344d5672c1"),
"user" : ObjectId("586b84239ae9590ab66bd3ad")
}
{
"_id" : ObjectId("58ff4c9f2952d7341d4afc0c"),
"direction" : -1,
"post" : ObjectId("58fc15a3fb3bed0fd54bfd95"),
"user" : ObjectId("586b84239ae9590ab66bd3ad")
}
To this:
[
//post: direction
"58ff4ea572ac97344d5672c1": 1,
"58fc15a3fb3bed0fd54bfd95": -1
]
I can't seem to find anything in the MongoDB Aggregation docs that allows you to set the key name using the value of another field.
I'm expecting this code to work, but I can see why it doesn't. It thinks that "$post" refers to a MongoDB expression.
db.votes.aggregate([
{$group: {
_id: null,
entries: {
$addToSet: {
"$post": "$direction"
}
}
}}
])

Different upsert behavior on Cosmos DB vs MongoDB

I'm running into an issue with Cosmos DB where the behavior of a query with {upsert: true} and $setOnInsert where the insert values are applied every time regardless of if the operation was an insert or an update.
The results of the following example query when ran against Cosmos DB and MongoDB show a difference in the final value of defaultQty.
db.products.remove({})
// WriteResult({ "nRemoved" : 1 })
db.products.insert({ _id: 1, item: "apple", price: 0.05, defaultQty: 50})
// WriteResult({ "nInserted" : 1 })
db.products.find({})
// { "_id" : 1, "item" : "apple", "price" : 0.05, "defaultQty" : 50 }
sleep(100)
db.products.update(
{ _id: 1 },
{ $set: { price: 0.10 }, $setOnInsert: { defaultQty: 100 }},
{ upsert: true }
)
// WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.products.find({})
// { "_id" : 1, "item" : "apple", "price" : 0.1, "defaultQty" : 100 }
Here is a screen shot of the comparison results side-by-side in Studio 3T.
Has anyone experienced this?
Thanks!
This issue is now fixed awaiting deployment. You can track progress here https://feedback.azure.com/forums/599059-azure-cosmos-db-mongodb-api/suggestions/20017141-bug-fix-during-upsert-operation-setoninsert-is-b
We Will post update once deployment is completed.
thanks!
Yes, this is a known issue that will be fixed shortly in Azure Cosmos DB.

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.