You are on page 1of 4

1.

_id is a ________ bytes hexadecimal number which assures the uniqueness


of every document. (12BYTES)
2. C++, JavaScript, python
3. most nosql databases support automatic meaning that you get high
availability and disaster recovery (Replication)
4. to create a or switch to a database in mongodb which command would you
use (use <>)
5. which of the following is/are true about MongoDb? (D. All of the mentioned
above)
6. A query may include a ___________ that specifies the fields from the
matching documents to return. (projection)
7. What is the use of the $nin operator in MongoDB? (None)
8. What is the use of the $push operator in MongoDB? (Array)
9. What is the use of the $set operator in MongoDB? (to set the value)
10. quan tam toi thu tu chinh xac(db.inventory.find({tags: [“red”, “blank”]}),
all la khong quan tam thu tu
11. db.products.insertMany([{ "id": 10, "item": "large box", "qty": 20 },
{ "_id": 11, "item": "small box", "qty": 55 },{ "id": 11, "item": "medium
box", "qty": 30 },{ "id": 12, "item": "envelope", "qty": 100 }, { "_id": 13,
"item": "stamps", "qty": 125 },{ "_id": 14, "item": "tape", "qty": 20 },
{ "id": 15, "item": "bubble wrap", "qty": 30 }]) (7) trung cung duoc
Connect to local MongoDB instance on default port: ▪ mongosh
List the databases available to the user: ▪ show dbs
Switch/Create databases: ▪ use db_name
List of all collections for current database: ▪ show collections
find() selects documents in a collection or view and returns a cursor to the selected documents
db.collectionname.find(query, projection)
To query for all documents where the field tags value is an array with exactly two elements, “A" and
“B", in the specified order: ▪ db.inventory.find( { tags: [“A", “B"] } ) //match an array
- To find an array that contains both the elements “A" and “B", without regard to order or other
elements in the array, use the $all operator ▪ db.inventory.find( { tags: { $all: [“A", “B”] } } )
- To returns documents in the bios collection where the awards array contains an element with award
field equals "Turing Award": ▪ db.bios.find({ "awards.award": "Turing Award" })
- To return all documents in the bios collection where the embedded document name contains a field
first with the value "Yukihiro" and a field last with the value "Matsumoto“: ▪
db.bios.find( { “name.first” : "Yukihiro", “name.last” : "Matsumoto" }) The query would match
documents with name fields that held either of the following values: { first: “Yukihiro”, aka: “Matz”,
last: “Matsumoto” } { last: “Matsumoto”, first: “Yukihiro” }
- To returns documents in the bios collection where the array field contains the element "UNIX“: ▪
db.bios.find( { “contribs” : "UNIX" } )

$in {field: { $in: [value1, value2, ... valueN ] } }


db.inventory.find( { qty: { $in: [ 5, 15 ] } } ) db.bios.find( { contribs: { $in: [ "ALGOL", "Lisp"] } } )
Matches any of the values specified in an array.

$not { field: { $not: { operator-expression } } }


db.inventory.find( { price: { $not: { $gt: 1.99 } } } ) Inverts the effect of a query expression and
returns documents that do not match the query expression.

$nor { $nor: [ { exp1 }, { exp2 }, ... { expN } ] }


db.inventory.find( { $nor: [ { price: 1.99 }, { qty: 2 } ] } ) Joins query clauses with a logical NOR
returns all documents that fail to match both clauses.

$exists { field: { $exists: } } db.inventory.find( { qty: { $exists: true, $nin: [ 5, 15 ] } } )


When is true, $exists matches the documents that contain the field, including documents where the
field value is null
- matche all documents where the sku field is like "%789": db.inventory.find( { sku: { $regex:
/789$/ } } )
- use the i option perform a case-insensitive match for documents with sku value that starts with ABC
db.inventory.find( { sku: { $regex: /^ABC/i } } )
- use the m option to match lines starting with the letter S for multiline strings:
db.inventory.find( { description: { $regex: /^S/m } } )
- Use the s option to allow the dot character (i.e. .) to match all characters including new line as well
as the i option to perform a case-insensitive match: db.inventory.find( { description: { $regex:
/m.*line/, $options: 'si' } }
$all { field: { $all: [ value1, value2 ... ] } } - To find an array that contains both the elements “A” and
“B”, without regard to order or other elements in the array: db.inventory.find( { tags: { $all: [“A”,
“B”] } } )
$size { field: { $size: number} } db.inventory.find( { tags: { $size: 3 } } ) Selects documents if the
array field is a specified size

- Counts the number of documents referenced by a cursor. Append the count() method to a find()
query to return the number of matching documents.
Use limit() to maximize performance and prevent MongoDB from returning more results than
required for processing.
- The skip() method controls the starting point of the results set.
The sort() method orders the documents in the result set

db.collection.update()
Modifies an existing document or documents in a collection. (Read more) By default, the update()
method updates a single document. Include the option {multi : true} to update all documents that
match the query criteria. Hence we can use it as both ways.
$set Sets the value of a field in a document { $set : { : , ... }}
$unset { $unset : { : "", ... } } Removes the specified field from a document.
$min { $min : { : , ... } }
Consider the following document in the collection scores : { _id : 1, highScore: 800, lowScore : 200 }
db.scores.update( { _id : 1 }, { $min : { lowScore : 150 } } )
The scores collection now contains the following modified document: { _id : 1, highScore: 800,
lowScore : 150 }. Add "unit price" field (does not exist before) and set to specified value
db.products.update( { _id : 103 }, { $min : { unit_price : 150 } } )
$max { $max : { : , ... } }
Consider the following document in the collection scores : { _id : 1, highScore: 800, lowScore :
200 } db.scores.update( { _id : 1 }, { $max : { hightScore : 950 } } )
The scores collection now contains the following modified document: { _id : 1, highScore: 950,
lowScore : 200 }
$ The positional operator. Acts as a placeholder to update the first element that matches the query
condition
To update the first element whose value is 80 to 82 in the in the grades array, use the positional $
operator if you do not know the position of the element in the array: db.students.update ( { _id : 4,
grades : 88 }, { $set : { "grades.$" : 82 } } )
$[] The all positional operator. Acts as a placeholder to update all elements in an array for the
documents that match the query condition (updateMany, update)
$addToSet - Adds elements to an array only if they do not already exist in the set. - if you use
$addToSet on a field that is absent in the document to update, $addToSet creates the array field with the
specified value as its element.
$pop Removes the first or last item of an array
$pull Removes all array elements that match a specified query.
$push appends a specified value to an array
$each Modifies the $push and $addToSet operators to append multiple items for array updates.
$position Modifies the $push operator to specify the position in the array to add elements.
$slice Modifies the $push operator to limit the size of updated arrays.
$sort Modifies the $push operator to reorder documents stored in an array
db.collection.deleteOne() Removes a single document from a collection. (Read more)
db.collection.deleteMany() Removes all documents that match the from a collection.

You might also like