I want to query something with SQL's like query:
SELECT * FROM users WHERE name LIKE '%m%'
How to do I achieve the same in MongoDB? I can't find an operation for like in the documentation of codeigniter.
db.users.find({"name": /m/}))
Should be everything you need.
Related
I want to calculate the number of users like this in SQL.
select count(*) as nbr_users from users
How can I do the same thing in mongodb?
db.nbr_users.count()
Check out the SQL-Comparision on MongoDB website, It has all the common select statements converted to MongoDB queries -
https://docs.mongodb.com/manual/reference/sql-comparison/#select
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}}])
How could I get the distinct values from the mongodb collection in yii.
Is it same as that in sql ie can I use distinct(fieldname) in yii.
In Yii I believe you would have to access the mongodb object directly and work on it using the distinct() command:
Yii::app()->mongodb->command(array("distinct" => "your_field", "key" => "your_key"))
This will not return an AR format of the results since MongoDBs distinct doesn't quite work like SQLs however you could emulate something of SQLs distinct with the aggregation framework with most likely $group on your field.
There might be a way of doing this with whatever plugin you are using in Yii however we have no idea other than the fact that you are using Yii...
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 would like to use benchRun http://www.mongodb.org/display/DOCS/JS+Benchmarking+Harness on a query like this:
.find().sort({ last_date : 1 }).limit(1,
Is there a way to specify such a query in the "ops" JSON array? I couldn't see anything in the benchRun.cc source code, so I just wanted to check. Thanks for your help!
Currently, there is no way to use it for sort. It is "planned but not scheduled" according to the issue SERVER-5722.