How to remove null matching document rows in case of populate in mongodb - mongodb

Hello all I am new to mongodb and nodejs application .i want to join two documents and filter the same by query but it return null when no matching is found in second document i do not want null i want to remove that complete row

Related

I want to create a index in each document in mongo which having a unique number like roll number

Currently i have a 3.1k documents each one have mobile and email field but i want to add a another filed with a unique number in all documents.
is there any query that can add a new field in each documents and insert number from 1 to end by incrementing?

Is there a way to quickly check every table in a mongodb database with the column "title"?

Is there a way to quickly check every table in a mongodb database with the column "title"? I need to identify every table or rather collection where there's a column with the word "title", is there a way to do this using a mongodb query?
In Mongo there is no straight forward query to check all collections and fields. Instead, you can get a list of all collections using getCollectionInfos and then query each collection to see if there exists the field that you are looking for.
db.getCollectionInfos().forEach(function(c){
result = db.getCollection(c.name).findOne({"title":{$exists:true}});
if(result != null){
print(c.name);
}
}
);
This will not look for nested documents, though.

MongoDB fetch all the id from the other document and make it's value in array

I have 2 documents where 1st document contains data and its reference key is in another document.
So I want to map both the table where collection A's id is not in collection B.
I have tried $unwind, $in, $nin but it is not working.
I have tried with aggregation as well.
ie. Consider SQL join where 1 table primary key becomes reference of 2nd table

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!

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!