MongoDb Flashcards

1
Q

How can you add a custom ID field to a record if you don’t want to use the default?

A

Use _id: “custom content” for the field

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What cmd line command will start the Mongodb server and allow cmd line input?

A

mongo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Command to Delete a single document from a collection ?

A

db.posts.deleteOne( { “transparency” : “several” } );

Removes the first that matches the criteria.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Update a single document and create (or overwrite) a single field.

A

db.posts.updateOne( { “airport”: “Logan” }, { $set: { “marker”: “delete” }} );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what type of update or delete commands can the selection criteria just be { } ?

A

updateMany and deleteMany

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

show a way to delete all records in a collection regardless of whether they have fields in common or not.

A

db.posts.deleteMany( { } );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

show insertMany using a text array.

A

db.posts.insertMany( [ { … } ]

where a JSON text file is insert (can be many lines)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

show insertMany as you would enter fields individually

A

db.posts.insertMany( { “a”: “value1”}. { “b”: “another” }, …

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

list current databases

A

show dbs

NOTE: a new database won’t appear until you add (insert) at least one record OR at least create a new collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

create a new database

A

use myNewDb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

use an existing database

A

use myDatabase

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Add a new collection to the current database

A

db.createCollection(“name”, { options } )
or
db.createCollection(“name”);

Options:
capped	Boolean	(Optional) If true, enables a capped collection. Capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also.

autoIndexId Boolean (Optional) If true, automatically create index on _id field.s Default value is false.

size number (Optional) Specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also.

max number (Optional) Specifies the maximum number of documents allowed in the capped collection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the result of the following syntax:

db.col.update{ {_id: Object:”sdfsd”} { fieldA: “xyzzy” } }

A

The entire record is replaced with ONE field in this case fieldA.
If $set is added, then it will only update the one fieldA without touching the other fields.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to delete a database.

A

use databaseToDelete

db.dropDatabase()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

show how to replace an existing record.

A

db.flights.replaceOne(
{_id: ObjectId(“5f41477288e0fe4c33f4c928”)},
{ “departureAirport”: “MUC”, “arrivalAirport”: “SFO”, “aircraft”: “Airbus A380”, “distance”: 12000, “intercontinental”: true } )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

show how to output all the records (and not stop after the first 20) as an array.

A

db.passengers.find().toArray()

17
Q

Show how to send all records to a function that will print all records in json format.

A

db.passengers.find().forEach( (passData) =>
{ printjson(passData) }

This can output to a language specific function(s); see Mongodb drivers documentation