Using MongoDB to query selected field - mongodb

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!

Related

MongoDb database Filter is not working in Compass

When I query all the records, i get the results as shown below.
When i copy an ID from the result and add it in the filter, no result is found.
Here is the example filter from documentation
What am i doing wrong? mongo db got to be kidding me :/
You are not providing the data type with the query. Since your _id field is ObjectId type and you are comparing as string that is why it is not working.
Change your filter condition and convert your string id to ObjectId type.
{_id: ObjectId('yourId')}
MongoDB provides an automatic unique identifier for the _id field in the form of an ObjectId data type.

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!

mongodb custom sorting like mysql

I am trying to sort a field, but I need to do it like how mysql does and specify the value,
instead of field number sory by -1, 4 3 2 1.
I need to sort it by 2,1,3,4.
is this possible in mongodb?
This is not possible in MongoDB. MongoDB doesn't know which and how many fields exist in a particular document unless the document is retrieved.
Hence MongoDB practically cannot give numbers to specific fields.
MongoDB is a JSON-style data store. The documents stored in the
database can have varying sets of fields, with different types for
each field.
In Databases the number and types of the column is fixed by particular table definition and it's easier for a database system to give numbers to the columns.

Which Value Pass to Param Select all Records in Mongo DB Collections using Meteor?

Which value pass to param select all records in mongo db collections using meteor.
For Example :
var filterResult = auth_user.find({staff:0,issuper:0}).fetch();
The above one to pass either 0 or 1 this time not a problem.But some times pass only staff value and issuper is to select all records and vice versa.If not given two values select all records in a collection.
I need the above one in a single query? is it possible or suggest me what to do for this?
Its hard to know what you're asking but in general you can get everything in the collection without any query at all:
var filterResult = auth_user.find({}).fetch();

mongodb computed field based on another query

I have a mongodb query, and I want to add a computed field. The computed field is based on where or not the item is in the results of another query. So my query returns the columns a,b,c,d, and then column e should be based on whether or not the current row would be matched by another query.
Is there an efficient way to do this in mongo? I'm not really sure how to do this one...
There is no way currently to execute a function as you describe within the database when returning a document via standard functions such as find. It's been requested by the community, but the general request is to operate only on a single document.
There are calculated fields using $project in the aggregation framework. But, they only operate on the current document in the pipeline. So, they can't summarize other queries.
You'll need to likely build your e value as part of your data access layer.