How to query on multiple documents present in the same collection in mongdb based on condition? - mongodb

I am working one use case poc, where I want to query multiple documents present in the same collection in MongoDB based on some condition? an example would be enough.
Looking for a query similar like SQL select query on two tables(following query) is it possible in MongoDB?
select t1.X, t1.Y_DT,t1.Z,t1.adj,t1.bjc,t1.jbc,t1.mnk,t2.adj1,t2.bjc1,t2.jbc1,t2.mnk1 from test250 t1, test350 t2 where t1.X = t2.X AND t1.Y_DT=t2.Y_DT AND t1.Z = t2.Z;
if possible, any example query greatly helps me out.

Related

Translating MySQL NOT IN to MongoDB

I've recently started using MongoDB, and while translating queries, I've stumbled upon a problem when trying to translate MySQL NOT IN query.
What I'm trying to do is find ids that are not present in another collection. I need a way to return how many of these ids there are and then a list of these ids.
In MySQL, these ids are in a table called songs and are named spotify_id. I am checking against a table named artists where the ids are called track_id.
MySQL Query:
SELECT spotify_id FROM songs WHERE spotify_id NOT IN (SELECT artists.track_id FROM artists)
My current code using MongoDB:
track_ids = artists.find({}, {'track_id': 1})
track_ids_list = [d.get('track_id') for d in track_ids]
# Getting amount of ids not present in artists
tracks_num = songs.count_documents({'spotify_id': {'$nin': track_ids_list}})
# Getting tracks not present in artists
tracks = songs.find({'spotify_id': {'$nin': track_ids_list}})
The MongoDB query seems to return a positive number of documents, even when all of the ids should be present in both collections. The MySQL query works perfectly on the same database.
Is there a better way to perform a NOT IN query in MongoDB?
Thank you!

Querying MongoDB: retreive shops by name and by location with one single query

QUERYING MONGODB: RETREIVE SHOPS BY NAME AND BY LOCATION WITH ONE SINGLE QUERY
Hi folks!
I'm building a "search shops" application using MEAN Stack.
I store shops documents in MongoDB "location" collection like this:
{
_id: .....
name: ...//shop name
location : //...GEOJson
}
UI provides to the users one single input for shops searching. Basically, I would perform one single query to retrieve in the same results array:
All shops near the user (eventually limit to x)
All shops named "like" the input value
On logical side, I think this is a "$or like" query
Based on this answer
Using full text search with geospatial index on Mongodb
probably assign two special indexes (2dsphere and full text) to the collection is not the right manner to achieve this, anyway I think this is a different case just because I really don't want to apply sequential filter to results, "simply" want to retreive data with 2 distinct criteria.
If I should set indexes on my collection, of course the approach is to perform two distinct queries with two distinct mehtods ($near for locations and $text for name), and then merge the results with some server side logic to remove duplicate documents and sort them in some useful way for user experience, but I'm still wondering if exists a method to achieve this result with one single query.
So, the question is: is it possible or this kind of approach is out of MongoDB purpose?
Hope this is clear and hope that someone can teach something today!
Thanks

Using MongoDB to query selected field

I am trying to query out the data from my MongoDB database but there are some fields which I would like to omit as MongoDB will query the whole collections with id, n out.
I did this to limit the query but unfortunately only one field could be omitted but not the other which is the 'n' field. How can I omit two fields?
data = collection.find_one({"files_id": file_id},{"_id":0,"data":1})
And I also realized that my query for data has the field name (u'data') too, how can I query it so that it only returns the data? for this case it's a binary data
Example:
{u'data': Binary('\x00\x00\xed\x00\n\x00\x00\xd5\xa9\x00\x000\x00\x00\x00#\x00\x00\x0f\xff\xf0\x00\x0b\x80\x00\x00\x00
Kindly assist thanks!

to compare two fields of the same collection

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}});

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.