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

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.

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!

Duplicate the documents in same collection in mongo

Is there a way to duplicate the records in a collection into the same collection ? I am trying to generate lots of records and hence this is needed.
If you just want to duplicate easy way is like below
db.col1.find({},{_id:0}).forEach(function(doc){db.col1.save(doc)});
A quick but maybe not the most efficient way to do that could be:
Get all the documents of the collection
For each one re-write the ObjectId with a new value
Insert the modified document inside the collection
With mongo shell you could do that using the forEach as follows:
db.getCollection('YOUR_COLLECTION').find({}).forEach(
function(doc){
doc._id = new ObjectId();
db.getCollection('YOUR_COLLECTION').insert(doc);
}
)
This way, each time you run this query, all the documents in the collection are duplicated.

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!

how could mongodb use query condition, index, sort at the same time?

My company project use mongodb as database.
Current data structure is like below: (for table user)
{
username:xxx,
password:xxx,
firstname:xxx,
lastname:xxx,
age:xxx,
created_at:xxx,
updated_at:xxx
}
Because this data will be supported by any query condition and sort condtion (more than one).
You know mongodb has to use combine field index at first, but I can't use index for every combination of fields.
So i try to change data structure like below:
{
data:[{key:username,value:xxx},
{key:password,value:xxx},
....],
username:xxx,
password:xxx,
firstname:xxx,
lastname:xxx,
age:xxx,
created_at:xxx,
updated_at:xxx
}
If I still use db.collection.find() function ,i can't use sort. Cause I can't accomplish that when data.key = username, sort data.value
If I use db.coolection.aggregate() function, i can't support sort for more than one field.
So how can I achieve my goal?
Is there any other data structure design?
Or is there any options I can do for find or aggregate function?

Get a document in MongoDB without specifying collection

MongoDB IDs are unique for a single database cluster. Is it possible to get documents using their IDs, without specifying the collection name?
If yes, how?
If no, why not?
Yes, but not in a scalable way (since you must query each collection). If you have 2 or 3 collections, this might be ok, but... you probably should review your design to figure out why you're doing this. Why are you, by the way?
You get a list of all of the collections in the database.
You loop through them, and query based on _id
Sample shell code:
db.test1.save({});
db.test2.save({});
db.test3.save({});
db.test4.save({});
db.test5.save({});
db.test6.save({});
db.test2.findOne(); // gives: { "_id" : ObjectId("4f62635623809b75e6b8853c") }
db.getCollectionNames().forEach(function(collName) {
var doc = db.getCollection(collName).findOne({"_id" : ObjectId("4f62635623809b75e6b8853c")});
if(doc != null) print(doc._id + " was found in " + collName);
});
gives: 4f62635623809b75e6b8853c was found in test2
ObjectId is designed to be globally unique (worldwide, not just within a single cluster). And it pretty much is.
It includes time, machine id, process id and a random number. However, it does not include database or collection name. Therefore, it is impossible to fetch a document using only the id. You have to provide database and collection names as well.