mongoid distinct with matching - mongodb

In mongodb you could use command like
db.sessions.distinct("Ip",{ 'Application': '123'})
which will return all unique ip for the selected application. How to do that via Mongoid?
I trying to pass 2 argument in distinct function but it fails with exception 'ArgumentError: wrong number of arguments (2 for 1)'

Distinct in Mongoid takes one argument -- the field you wish to filter distinct on. So in your case, you could chain a where clause w/a distinct like so:
YourModel.where(Application: '123').distinct(:Ip)
This would produce a collection of distinct YourModel's by field Ip where the field Application is equal to '123'.

Show you full distinct query, please.
I try follow syntax with users collection(3 documents with name Bob):
db.users.distinct("_id", {name: "Bob"})
And It's works:
[
ObjectId("5121792d499af102889f2576"),
ObjectId("5121792e499af102889f2577"),
ObjectId("5121792f499af102889f2578")
]
My MongoDB version is 2.2.0

Related

How to retrieve all values from a column in mongoDB

I am wondering how to retrieve all values stored in a column in mongoDB, and put them in a list. find() only get all the fields, and specify <field>: <value> isn't an option as well.
If you are expecting to retrieve unique values in a column then distinct should work for you. Following is the syntax :
db.yourcollection.distinct("yourfield");
Learn more about distinct here

Mongodb query how to get createdby records or associated with records

My user database structure is as follows
{ _id:123, fname:Name,
projects:[
{projectid:123,
createdby:123}
]
}
{ _id:456, fname:Name,
projects:[
{projectid:789,
createdby:456,
teammembers:[{memberid:123},{memberid:654}]
}
]
}
I am trying to get the list of projects where either i am the creator or i am one of the teammembers. I have trued the following query
db.user.find({"$or":[{"projects.teammembers.memberid":"123"},{"projects.createdby":"123"}],{projects:1})
This query gives me projects where i am not a member also.
If i put the column restriction as
projects.$.memberid:1
mongo throws this error.
"Positional operator does not match the query specifier."
I know by changing the structure of projects.teammembers to just array will work but for now the change process will take time.
Any solution?
You are trying to execute a query on a "2 levels array" that's why you get :
projects.$.memberid:1
So, you must to use the $ proection operator, for "search in each table values in their own table".
Try with
db.user.find({"$or":[{"projects.teammembers.$.memberid":"123"},{"projects.createdby":"123"}],{projects:1})
Or take a look at this doc: Projection doc
Sorry for my english
I was able to solve the problem using aggregate command.
Here's what i did
collection.aggregate([{"$unwind":"$projects"},
{"$match":{"$or":[{"projects.createdby":"user1"},
{"projects.teammembers.memberid":"user1"}]}},
{"$project":{"projects":1}}]
It gives the list of projects where i am the creator and also only those list of projects where i am a team member.

Mongodb select with condition is selected result must in sub select query

How do you do nested select in MongoDB analogous to
SELECT id FROM table1 WHERE id IN (SELECT id FROM table2)
MongoDB does not yet possess the ability to do subqueries which would allow this functionality.
I am sure it is something within the JIRA however, I could not immediately find it.
The only way currently is to actually pick the table, iterate the cursor pulling out the information and using that as part of a $in query as shown by #Bruno, like so:
ids=[];
for(i in db.c2.find({},{_id:1}){ // I have assumed id=_id
ids[ids.length]=i; // The default return of the index pos is actually _id
}
return db.c1.find({$in:ids});
As a mere example I wrote off of the top of my head.
This page contains comparator between SQL and MongoDB:
http://docs.mongodb.org/manual/reference/sql-comparison/
For example, you can to use a aggregation pipeline and where is the same as:
db.your_collection.aggregate({$match: {"id": {$in: ['id_0', 'id_1']} }})
A simple query as the same as:
db.your_collection.aggregate({field: "value"})
Mongodb official page has a lot of informations

How to retrieve whole document in mongodb distinct query rather than only the key?

Mongodb distinct command returns the list of distinct keys in a given document.
Is there any way to retrieve the whole document rather than only keys using mongodb java driver or morphia?
In the documentation it says:
When possible, the distinct command will use an index to find the documents in the query as well as to return the data.
How does this work, when I try using java driver it always returns the list of keys to my queries. And it doesn't take limit and order queries into consideration. Is it possible to give a order by and limit queries into distinct query?
Thanks for the feedback.

MongoDB: How to execute a query to result of another query (nested queries)?

I need to apply a set of filters (queries) to a collection. By default, the MongoDB applies AND operator to all queries submitted to find function. Instead of whole AND I need to apply each query sequentially (one by one). That is, I need to run the first-query and get a set of documents, run the second-query to result of first-query, and so on.
Is this Possible?
db.list.find({..q1..}).find({..q2..}).find({..q3..});
Instead Of:
db.list.find({..q1..}, {..q2..}, {..q3..});
Why do I need this?
Bcoz, the second-query needs to apply an aggregate function to result of first-query, instead of applying the aggregate to whole collection.
Yes this is possible in MongoDB. You can write nested queries as per the requirement.Even in my application I created nested MongoDb queries.If you are familiar with SQL syntax then compare this with in of sql syntax:
select cname from table where cid in (select .....)
In the same way you can create nested MongoDB queries on different collections also.