MongoDb Flashcards
How can you add a custom ID field to a record if you don’t want to use the default?
Use _id: “custom content” for the field
What cmd line command will start the Mongodb server and allow cmd line input?
mongo
Command to Delete a single document from a collection ?
db.posts.deleteOne( { “transparency” : “several” } );
Removes the first that matches the criteria.
Update a single document and create (or overwrite) a single field.
db.posts.updateOne( { “airport”: “Logan” }, { $set: { “marker”: “delete” }} );
what type of update or delete commands can the selection criteria just be { } ?
updateMany and deleteMany
show a way to delete all records in a collection regardless of whether they have fields in common or not.
db.posts.deleteMany( { } );
show insertMany using a text array.
db.posts.insertMany( [ { … } ]
where a JSON text file is insert (can be many lines)
show insertMany as you would enter fields individually
db.posts.insertMany( { “a”: “value1”}. { “b”: “another” }, …
list current databases
show dbs
NOTE: a new database won’t appear until you add (insert) at least one record OR at least create a new collection.
create a new database
use myNewDb
use an existing database
use myDatabase
Add a new collection to the current database
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.
What is the result of the following syntax:
db.col.update{ {_id: Object:”sdfsd”} { fieldA: “xyzzy” } }
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 to delete a database.
use databaseToDelete
db.dropDatabase()
show how to replace an existing record.
db.flights.replaceOne(
{_id: ObjectId(“5f41477288e0fe4c33f4c928”)},
{ “departureAirport”: “MUC”, “arrivalAirport”: “SFO”, “aircraft”: “Airbus A380”, “distance”: 12000, “intercontinental”: true } )