Mongo DB Flashcards
What is the MongoDB syntax to write a JSON range query?
{fieldname:{$gte: numx, $lt: numy}}
What is the syntax to create a database?
use
What is the MongoDB syntax to find a collection within the command line?
db.collection_name.find()
what is the Mongo CLI syntax to insert multiple documents?
db.post.insertMany([])
How do you specify, or access field in an embedded document?
”..“
To specify or access a field of an embedded document with dot notation, concatenate the embedded document name with the dot (.) and the field name, and enclose in quotes:
db.post.find({‘username.fname’:’John’})
What is the syntax to find an array length?
db..find( { “tags”: { $size: 3 } } )
What is the syntax for AND in a ‘find’ clause?
db..find({$and:[{“Username”:”Alex”},{“title”:”NoSQL Database”}]}).pretty()
How do you use find with a specific clause
db.post.find( {:]} )
example
db.post.find( {tags:[ ‘mongodb’, ‘database’, ‘NoSQL’]} )
What is the MongoDB command line syntax to query an array by array length
db.find({:})
example
db.post.find( { “tags”: {$size:3}})
What is the mongoDB command line syntax to update a document?
db.mycol.update({:},{$set:{:}})
What is the MongoDB command line syntax to insert multiple documents into a collections
db. .insertMany([{:,,,:},{BSONFILE2},{BSONFILE3}])
* FYI:: ‘insertMany’ takes in an array.
What is the MongoDB command line syntax to delete a collection
db..drop()
What is the MongoDB command line syntax to rename a collection
db..renameCollection()
What is the MongoDB CLI syntax to iterate through a field to see if it equates to a boolean statement?
db.records.aggregate( [
{ $project: { : { $eq: [ “”, { $literal:} ] } } }
] )
What formats does MongoDB accept?
Use Json and Bson for documents.
What is the normalized data model? and when should you use it?
Normalized data models describe relationships using references between documents.
In general, when should you use normalized data models?
➢ when embedding would result in duplication of data but would not provide sufficient read performance advantages to outweigh the implications of the duplication.
➢ to represent more complex many-to-many relationships
➢ to model large hierarchical data sets.
Per the MongoDB University course, what argument can we pass through so that mongo DB continues inserting even if it contacts an error
{
‘ordered’: false
}
in MongoDB, what syntax goes after a projection query to get rid of the primary key (or any other field in a projection for that matter)
Setting a file to ‘:0’ in the projection field
I.E. _id:0
db.COLLECTIONNAME.find({FILTER(S)}, {PROJECTION, _id:0}
E.G: db.movies.find({genre:’drama’},{title, _id=0}
In MongoDB, what syntax goes within projection parameter of query to explicitly return respective field, and exclude all others
set a respective field to ‘1’
E.G.: db.movies.find({genre:’drama’},{title:1}
What method call is used to remove a specific field
$unset
What does upsert stand for in MongoDB. In which line does it appear in a Mongo query?
if exists update, if not then insert. it appears as the third parameter in an update
what does the $all operator do?
helps you find a combination of values for a given array within a query
e. g.
db. movies.find({genres:{$all: ‘Romance’, ‘comedy’,’action’}},{title:0})
what function can you use to get a count of a query
.count()
e. g.
find. movies.find({countries:{$size:1}}).count()
what are the three popular array operators shown in MongoDB?
1) $all: – takes in an array []
2) $size: (is enclosed within {})
3) $elemMatch: (enclosed within {} )
bonus:
4) $in: [ ] ## takes in an array
5) $nin [ ]
When are $and and $or used during a MongoDB?
When the query has two parameters for the same field. Otherwise, a query is only ran with the second search parameter for the given field.
db.post.find({$and:[arg1, arg2]})
What coding language does the MongoDB shell interpret in?
The Mongo shell is a JavaScript.
how is $in used in MongoDB queries?
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
What is the syntax in Mongo for $elematch queries? what can you not specify
{ : { $elemMatch: { query1, query2, … } } }
You cannot specify a $where expression in an $elemMatch.
You cannot specify a $text query expression in an $elemMatch.