Update query for Mongo document in Redash - mongodb

I am trying to look for a query to update a record in Mongo document using Redash. I came up with the following query but it does not work. Can anyone please help? What I know about Redash is that it uses Pymongo.
{
"collection": "Application",
"update": [{
"$match": {
"ID": "2001"
},
"$set": {
"ID": "2020"
}
}]
}

Never used Redash, but in pymongo the construct would be:
db = MongoClient()['mydatabase'] # Set the database connection
db.Application.update_one({"ID": "2001"}, {"$set": {"ID": "2020"}}) # Perform the update

Related

Count document satisfies criteria in MongoDB Compass

Below one of the document inside my test.users collection.
{
"_id": {
"$oid": "62ef8c502935226353a97efb"
},
"workspaces": [
{
"$oid": "62ef8c722935227aaca97eff"
}
],
"emailConfirmed": true,
"ipInfo": {
"ip": "191.23.43.218",
"region": "Krasnodarskiy",
"country": "RU",
"timezone": "Europe/Moscow"
},
"__v": 0
}
I want to count the number of country of RU in test.users collection in MongoDB compass, how should I do it? I only saw filter, project, sort, collation under Documents as follows in Mongo DB compass, is there a way I can directly enter a mongoDB query?
You can use the mongosh shell present below, or you can directly check the count below FIND button after running your query, text is like Displaying document x of y:

Mongodb Liqbase script findOneAndUpdate for json type values

I have code that uses existing collection that I want to update name field value which is present in taskMap key how i run with run liqubase changeset for that .
sample code :
"changes": [
{
"collectionName": "bulk_import_job_spec"
},
{
"findOneAndUpdate": {
{"_id": 101},
[{"$set": {"task_map": {"import": {"_id": 1, "name": "Category Association"}}}}]
},
{
"returnNewDocument": true
}
]
your questions is quite vague. Please provide some more details so that we can understand your issue.

mongo db query to update document containing map object

I have below object in mongodb.
db.agedClientPosition.find().pretty(); {
"_id": ObjectId("596b0f02abc9b006c7de33fb"),
"_class": "org.maruti.mfm.data.entity.AgedClientPosition",
"ageBucket": {
"03-Jul-2017": 3482.896,
"05-Jul-2017": 2403.1168,
"10-Jul-2017": 299.4155
},
"divPayoutBucket": {
},
"createdOn": ISODate("2017-07-16T07:00:18.769Z"),
"modifiedOn": ISODate("2017-07-16T07:01:29.221Z"),
"client": DBRef("client", ObjectId("59544747e6de8f0328c9e8bb")),
"folio": DBRef("folio", ObjectId("5954475fe6de8f0328c9e8bc")),
"scheme": DBRef("scheme", ObjectId("5953c0c0e6de8f024753e3e1"))
}
and for all the ageBucket I want to increment date by 1 day.
Can I achieve this using mondodb query?

How to use aggregate with ReactiveMongo

So, I need to sort my collection on MongoDB by fields that are on an array of objects.
I have
"columns": [{
"kind": "FirstKind",
"descriptor": "Description1",
"data": "Data to be sorted"
},{
"kind": "SecondKind",
"descriptor": "Description2",
"data": "Data to be sorted"
}]
What I want to achieve is selecting "FirstKind" and "Description1" or "SecondKind" and "Description2" and sort the collection by the field data. I found a solution to do that on MongoDB, by doing:
db.getCollection('results').aggregate(
[{
"$match": {
"$and": [{
"columns.kind": "FirstKind"
}, {
"columns.descriptor": "Name"
}]
}
},{
"$sort": {
"columns.data": -1
}
},{
"$limit": 20
}]
)
My problem now is how to translate that to ReactiveMongo on Scala. I've been trying to understand this documentation: http://reactivemongo.org/releases/0.11/documentation/advanced-topics/aggregation.html but I'm really confused about it. Has anyone ever used aggregate with ReactiveMongo on Scala? Thanks!
I agree, it is not the most straightforward thing in ReactiveMongo.
I have tried to translate your code into the ReactiveMongo aggregation query syntaxt below. However I haven't run it so you might need to tweak it a little.
val dao = reactiveMongoApi.db.collection[BSONCollection]("results")
import dao.BatchCommands.AggregationFramework._
dao.aggregate(
Match(BSONDocument(
"columns.kind" -> "FirstKind",
"columns.descriptor" -> "Name")),
List(
Sort(Descending("limit")),
Limit(20)))

Updating embedded document property in Mongodb

I have a document that looks like this:
{
"_id": 3,
"Slug": "slug",
"Title": "title",
"Authors": [
{
"Slug": "slug",
"Name": "name"
}
]
}
I want to update all Authors.Name based on Authors.Slug.
I tried this but it didn't work:
.update({"Authors.Slug":"slug"}, {$set: {"Authors.Name":"zzz"}});
What am I doing wrong here?
.update(Authors:{$elemMatch:{Slug:"slug"}}, {$set: {'Authors.$.Name':"zzz"}});
You can use update with array filters:
https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#positional-update-arrayfilters
Probably something like this:
yourcollection.update(
{},
{
"$set": {
"Authors.$[element].Name": "zzz"
}
},
{
"multi": true,
"arrayFilters": [
{ "element.Slug": "slug" }
]
}
)
Ps.: it will not work in Robo3T as explained here: Mongodb 3.6.0-rc3 array filters not working?
However, you can try on a mongo shell with version >= 3.6.
yes, Rock's solution is working, P.S Notes is really helpful when trying Robo31..
If we want to update all
db.collection_name.update({}, {$set: {"Authors.$[].Name": "zzz"}})
If we want to update with matching object in an array
db.collection_name.update({}, {$set: {"Authors.$[i].Name": "zzz"}}, {arrayFilters: [{"i.Slug": "slug"}]})
Ref: https://jira.mongodb.org/browse/SERVER-1243