Count document satisfies criteria in MongoDB Compass - mongodb

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:

Related

Update query for Mongo document in Redash

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

Remove duplicates from MongoDB 4.0

I am trying to remove duplicates from MongoDB but all solutions find fail. Given the current JSON structure:
{
"_id": { "$oid": "5cee31bbca8a185b76a692db" },
"date": { "$date": "2018-10-07T19:11:38.000Z" },
"id": "1049014405130858496",
"username": "chrisoldcorn",
"text": "“The #UK can rest now. The Orange Buffoon is back in his xenophobic #WhiteHouse!” #news #politics #trump #populist #uspoli #ukpolitics #ukpoli #london #scotland #TrumpBaby #usa #america #canada #eu #europe #brexit #maga #msm #gop #elections #election2018 https://medium.com/#chrisoldcorn/trump-babys-uk-visit-a-reflection-1c2aa4ad942 …pic.twitter.com/Y6Yihs9g6K",
"retweets": 1,
"favorites": 0,
"mentions": "#chrisoldcorn",
"hashtags": "#UK #WhiteHouse #news #politics #trump #populist #uspoli #ukpolitics #ukpoli #london #scotland #TrumpBaby #usa #america #canada #eu #europe #brexit #maga #msm #gop #elections #election2018",
"geo": "",
"replies": 0,
"to": null,
"lan": "en"
}
I need to remove all duplicates based on field "id" in the file.
I have tried db.tweets.ensureIndex( { id:1 }, { unique:true, dropDups:true } ) but I am not sure this is the correct way. I obtain this output:
Can anyone help me?
It looks like you are running a MongoDB with version >3.0 and hence cannot remove dups by ensuring an index
According to the docs:
Changed in version 3.0: The dropDups option is no longer available.
The fastest way to do this would be to
Create a Dump
Drop the collection
Create the new Index
Restore the Dump
All duplicate documents will be dropped during the restore insert
The next best solution will be to run a script to collect all duplicate Ids and remove them

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?

search in mongodb embedded records

We have a mongodb document as given below, and we configured text index on messageTopic, messageTopicQuestion and answer fields, if i search with a text string then I expect only matched embedded records in the results not the entire document.
For example in below document if i search with word "private", then results should only return the first embedded document not both the records. How to retrieve only matched embedded documents and exclude unmatched ones.
{
"_id": ObjectId("586e8efdde81e56032000084"),
"messageTopic": "My Private",
"messageText": [{
"messageTopicQuestion": "agent private",
"answer": "agent private",
"_id": ObjectId("586e8efdde81e56032000085"),
"keywords": ["private"]
}, {
"messageTopicQuestion": "Greetings Checking",
"answer": "Heloo I am good What about u",
"_id": ObjectId("586fc80ccced739407000f4e"),
"keywords": ["Hi-Good", "Heloo"]
}],
"__v": 3
}
I am using below script
db.getCollection('messagetemplates').aggregate([{
$match: {
$text: {$search: 'private'},
visible: 'PUB'
}
},{ $sort: { score: { $meta: "textScore" } } }])
Appreciate help. Thanks.
I believe the question is a variation of this problem How to get a specific embedded document inside a MongoDB collection?
The issue is how to get the single embedded document and exclude the rest. My suggestion is to use db.collection.find() instead of aggregation.
Something in that sense
db.collection.find({ 'messageText.keyword': 'private' }, {'messageText.$': 1});
, as indicated by the answer above.
messageText.keyword can be replaced with whichever field you want to be searched.
I can confirm that the scenario works on my database.

Duplicating a Mongoose document with subdocs, duplicated id of subdocs are allowed?

To clarify: I have a document with a subdoc. I create a new document with the same data of the other one and it gets a new id. However, when I copy the subdoc array they do not get a new id.
Are subdocs id local to the parent doc? I.e. would the following be a problem?
[
{
"__v": 1,
"_id": "5214af03a9f53efa61000004",
"name": "Foo",
"subdocs": [
{
"thing": "value",
"_id": "5214af03a9f53efa61000006"
}
]
},
{
"__v": 0,
"name": "Foo",
"_id": "5214af03a9f53efa61000014",
"subdocs": [
{
"thing": "value",
"_id": "5214af03a9f53efa61000006"
}
]
}
]
There is a unique index on the _id field of documents stored directly in a collection, but not for embedded documents, nor is there any requirement that embedded documents have an _id field at all. The two documents you have provided are both valid to be stored in MongoDB in the same database (I'm interpreting your example as an array of two documents that are both stored directly in a collection together).