Loading...
Loading

Mongodb Top Most Interview Questions And Tips.

2018-01-08by emma blisa

Interview Tips

If you're looking for MongoDB Interview Questions and Answers for Experienced & Freshers, you are at right place. There are lot of opportunities from many reputed companies in the world. According to research MongoDB has a market share of about 4.5%. So, You still have opportunities to move ahead in your career in MongoDB. Mindmajix offers advanced MongoDB Interview Questions and Answers that helps you in cracking your interview & acquire your dream career.


Q. What’s a good way to get a list of all unique tags?


What’s the best way to keep track of unique tags for a collection of documents millions of items large? The normal way of doing tagging seems to be indexing multikeys. I will frequently need to get all the unique keys, though. I don’t have access to mongodb’s new “distinct” command, either, since my driver, erlmongo, doesn’t seem to implement it, yet.
Even if your driver doesn’t implement distinct, you can implement it yourself. In JavaScript (sorry, I don’t know Erlang, but it should translate pretty directly) can say:
result = db.$cmd.findOne({“distinct” : “collection_name”, “key” : “tags”})
So, that is: you do a findOne on the “$cmd” collection of whatever database you’re using. Pass it the collection name and the key you want to run distinct on.
If you ever need a command your driver doesn’t provide a helper for, you can look at HTTP://WWW.MONGODB.ORG/DISPLAY/DOCS/LIST+OF+DATABASE+COMMANDS  for a somewhat complete list of database commands.

Q. MongoDB query with an ‘or’ condition


I have an mongodb embedded document that tracks group memberships. Each embedded document has an ID pointing to the group in another collection, a start date, and an optional expire date.
I want to query for current members of a group. “Current” means the start time is less than the current time, and the expire time is greater than the current time OR null.
This conditional query is totally blocking me up. I could do it by running two queries and merging the results, but that seems ugly and requires loading in all results at once. Or I could default the expire time to some arbitrary date in the far future, but that seems even uglier and potentially brittle. In SQL I’d just express it with “(expires >= Now()) OR (expires IS NULL)” – but I don’t know how to do that in MongoDB.
Just thought I’d update in-case anyone stumbles across this page in the future. As of 1.5.3, mongodb now supports a real $ or operator:

https://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24 or

Your query of “(expires >= Now()) OR (expires IS NULL)” can now be rendered as:
{$or: [{expires: {$gte: new Date()}}, {expires: null}]}
In case anyone finds it useful, www.querymongo.com does translation between SQL and MongoDB, including OR clauses. It can be really helpful for figuring out syntax when you know the SQL equivalent.
In the case of OR statements, it looks like this
SQL:
SELECT * FROM collection WHERE columnA = 3 OR columnB = ‘string’;
MongoDB:
db.collection.find({
“$or”: [{
“columnA”: 3
}, {
“columnB”: “string”
}]
});

Q. MongoDB Get names of all keys in collection
I’d like to get the names of all the keys in a MongoDB collection.
For example, from this:
db.things.insert( { type : [‘dog’, ‘cat’] } );
db.things.insert( { egg : [‘cat’] } );
db.things.insert( { type : [] } );
db.things.insert( { hello : []  } );
I’d like to get the unique keys:
type, egg, hello
You could do this with MapReduce:
mr = db.runCommand({
“mapreduce” : “my_collection”,
“map” : function() {
for (var key in this) { emit(key, null); }
},
“reduce” : function(key, stuff) { return null; },
“out”: “my_collection” + “_keys”
})
Then run distinct on the resulting collection so as to find all the keys:
db[mr.result].distinct(“_id”)
[“foo”, “bar”, “baz”, “_id”, …]
Explore More Interview Questions Please Visit Us : MindMajix MongoDB Interview Questions
news Buffer
Author

emma blisa

Mindmajix technologies ANALYST View emma blisa`s profile for more
line

Leave a Comment