I have collection where each document has user related details.
userid:1001,abc:1
userid:1001,abc:9
userid:1002,abc:1
userid:1001,abc:3
Something like this.
I wanted to get count of userId which has maximum occurances.
I need to do this on Mongo shell.
Any ideas?
Check the aggregation framework of mongo. For the answer you can use $group operator to group by userid and sorting them based count. Something like this:
Suppose collection name is test:
db.test.aggregate([{"$group":{"_id":"$userid","occurence":{"$sum":1}}},{$sort:{"occurence":-1}}])
Related
i am confuse.
here is the example:
MongoDB Enterprise > db.employee.find()
result:
{"_id":1002,"name":"Jack","address":{"previous":"Cresent Street","current":"234,Bald Hill Street","unit":"MongoDB" } }
I try this:
db.employee.find({address:{previous: "Cresent Street"}})
result: nothing returns
Next a try this:
db.employee.find({"address.previous": "Cresent Street"})
result:
{"_id":1002,"name":"Jack","address":{"previous":"Cresent Street","current":"234,Bald Hill Street","unit":"MongoDB"}}
The question is wath is wrong with this?
i use
MongoDB shell version v4.2.7 installed
cmd db.version() 4.2.6
debian 10.4
thanks for your replies.
When you Query on Embedded/Nested Documents using dotted field notation
{"address.previous": "Cresent Street"}
means find a document that containd an address field that contains a document whose previous field is equal to "Cresent Street".
When you provide a subdocument like
{address:{previous: "Cresent Street"}}
this means to find a document that contains an address field whose content is exactly the document {previous: "Cresent Street"}, with no additional fields. If you provide multiple fields in the subdocument, field order also matters.
Both of these queries are useful in specific scenarios, pick the one that does what you need in your situaion.
I need a mongodb query something like
db.getCollection("xyz").find({"_id" : {$regex : {$in : [xxxx/*]}}})
My Use case is -- I have a list of Strings such as
[xyz/12/poi, abc/98/mnb, ytn/65/tdx, ...]
The ids that are there in the collection(test) are something like
xyz/12/poi/2019061304.
I will get the values like xyz/12/poi from the input list, the other part of the id being yyyymmddhh format.
So, I need to go to the collection and find all the documents matching the input list with the ID of the documents in the test collection.
I can retrieve the documents individually but that does not seem to be a feasible option as the size of the input list is more than 10000.
Can you guys suggest a more feasible solution. Thanks in advance.
I tried using $in with $regex. But it seems mongodb does not support that. I have also tried pattern matching but even that is not feasible for me. Can you please suggest an alternative to using $in with $regex in mongodb.
Expected result could be an aggragate query/a normal query so that we hit the database only once and get the desired output rather than hitting the db for 10000 odd times.
I want to compare two fields of the same collection (Mysql query example "SELECT * FROM table AS t WHERE t.field1 > t.filed2;") in mongodb with cakephp. I cannot use '$where' and aggregate of mongodb as I am also using other operators of mongodb like $or, $and and etc. And also I am using find of mongodb.
Ex: Collection have two fields integer fields per_day_budget and today_spent and I want to get the list of records where today_spent is less than or equal to per_day_budget. I hope this will you to better understand my query.
Kindly suggest solution for the same.
You can try:
db.collection.find({ this.today_spent : {$lte : this.per_day_budget}});
I have a Rails 3 app using MongoDB, with Mongoid as the ORM. I'd like to query for a specific field within a collection.
To query for all records of a particular collection I use User.all.to_a, as an equivalent to User.all in ActiveRecord.
Now I'd like to query for all records within a collection, but only output a particular field. In this case I'd like to see all User names. How do I do this?
I'm sure I've stared right at this in the Mongoid documentation and am just missing something...
I couldn't locate it in the new documentation for mongoid, but here is a quick link to only pointing to old 2.x.x documentation.
Basically you need to do:
User.all.only(:name).to_a
I need to group and sort by date_published some documents stored on mongodb using pymongo.
the group part went just fine :) but when I'm addding .sort() to the query it keeps failing no matter what I tried :(
here is my query:
db.activities.group(keyf_code,cond,{},reduce_code)
I want to sort by a field called "published" (timestamp)
tried to do
db.activities.group(keyf_code,cond,{},reduce_code).sort({"published": -1})
and many more variations without any success
ideas anyone?
You can't currently do sort with group in MongoDB. You can use MapReduce instead which does support a sort option. There is also an enhancement request to support group with sort here.
Although MongoDB doesn't do what you want, you can always use Python to do the sorting:
result = db.activities.group(keyf_code,cond,{},reduce_code)
result = sorted(result, key=itemgetter("published"), reverse=True)