Chapter 2. CRUD: Creating, reading and updating data
Insert operation
show dbsuse database
db --> show current database
show collections --> show the current database collections
db.sample.insert ( { a : 1 }) --> to insert a document
db.sample.find( ) --> to query documents
db.sample.find( ).pretty() --> to query documents in compact format
a documents can have differents schemes.
var aa = db.sample.find( ).toArray() --> put the result data in an array and assign to a variable aa like javascript
db.getLastError() --> to force the acknowlodge of the insert operation
Insert the document
{ x : 3 , y : 4 }
into the temperature collection for the current database.
The collection will initially be empty, but you can (and should) check for your document after insertion. The shell will automatically add an _id field to the inserted document. You should only insert the one document.
> db.temperature.insert({x:3,y:4})
> WriteResult({ "nInserted" : 1 })
> db.temperature.find()
{ "_id" : ObjectId("54b5835f132c1f084b02ab21"), "y" : 4, "x" : 3 }
Update operation
you can update one o more documents in a collection. db.colection.update (<<where>>,
<<doc or partial update expression in JSON or BSON format >>
, <upsert> --> optional
, <multi> --> optional
)
There are two kinds of updates:
full document update replacement
partial update: only change a single field
<upsert> : update or insert if not present
<multi> : many documents not only one
t = db.sample
t.update( { "_id" : 100}, { "a" : 100 } ) --> the id field must not change instead of we get an error
myobj = t.findOne()
{ "_id" : 190, x : "hello" }
myobj.y = 123
{ "_id" : 190, x : "hello", y : 123 }
t.update( { "_id" : myobj._id}, myobj )
Check all that are true about the _id field:
In v2.6 and higher, by default --> powerOf2sizes = on
Having powerOf2sizes on, the smallest documents you insert will be allocated in 32 byte allocation units. Then, it it's greater than that size, the next size will be 64 bytes, 128 bytes, etc. until to 16Mbytes limit. After 4MB it actually adds only 1MB at a time since we will getting closed to the upper limit .
The reason for this sizing strategy is to avoid heap fragmentation or deleted space fragmentation.
If you have a collection where you really needed to optimize, and you wanted a very specific allocation unit size you could turn this off.
What operations could cause an existing document to be moved on disk? Check all that apply.
Save() command
It's a shell helper function, it's not a mongoDB server operation,
myobj = t.findOne()
{ "_id" : 190, x : "hello" }
myobj.y = 897
t.save(myobj)
{ "_id" : 190, x : "hello", y : 897 }
What happens if you try to use db.collection.save(document) if the inserted document has no _id?
Partial Updates & Documents
The size limit for each document is 16MBytes in order not to saturate the ethernet connection in high documents like that 100MB.
We can use operators like:
- $set : to set a new value
- $push: to add somethig to an array. If it's not exist, it will be created
- $addToSet: to add to an array if not already present
- $pop : to remove from an array
- $unset: to delete a field
- etc ...
The Mongo Web Shell has been initialized with one collection, cars, with one document preexisting:
{ "_id" : 100, "name" : "GTO", "year" : 1969, "color" : "red" }
Set the available field to 1.
You can verify the initial state of the collection, and verify your answer before you submit. There should be only one document in the collection.
> db.cars.find()
{ "_id" : 100, "color" : "red", "name" : "GTO", "year" : 1969 }
> db.cars.update({ "_id":100}, { "$set":{"available":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.cars.find()
{
"_id" : 100,
"color" : "red",
"available" : 1,
"name" : "GTO",
"year" : 1969
}
Removing Documents
db.<< collection name>>.remove( << expression >>)
This is a multidocument operation.
db.test.remove( { "_id" : 100 } )
db.test.remove( { "x" : /hello/ }) --> with regular expressions
We have initialized documents in the users collection of the form:
{ _id : ObjectId("50897dbb9b96971d287202a9"), name : "Jane", likes : [ "tennis", "golf" ], registered : false, addr : { city : "Lyon", country : "France" } }Delete all documents in the collection where city is "Lyon" and registered is false.
You can check the initial state of the collection, and verify your answer before you submit.
> db.users.count()
300
> db.users.remove({ "addr.city" : "Lyon", "registered" : false})
WriteResult({ "nRemoved" : 33 })
> db.users.count()
267
Multi Update
db.collection.update( query_document , update_document , [ options_document ] )
where optional options_document has any one or more of the following optional parameters:
upsert : true/false,
multi : true/false,
writeConcern: document
by default --> upsert : false, multi : false
Which of the following are disadvantages to setting multi=false (as it is by default)?
Upsert
updates or inserts if not present
We have initialized documents in the users collection, one of which is the following:
{ _id : "Jane", likes : [ "tennis", "golf" ], registered : false, addr : { city : "Lyon", country : "France" } }In the shell, add that this user likes "football". You should not need to pass "tennis" or "golf" to theupdate query at all.
You can look at the initial state of the collection, and verify your answer before you submit.
> db.users.count()
11
> db.users.find( { "likes" : "football" })
> db.users.update( {}, { "$addToSet" :{ "likes" : "football" }}, { "upsert":true,"multi":true})
WriteResult({ "nMatched" : 11, "nUpserted" : 0, "nModified" : 11 })
Wire Protocol
What are the basic building blocks of the wire protocol? Check all that apply.
Which operation is overloaded in the wire protocol to handle commands?
Bulk() write Operations & Methods
There are two basic forms:
- ordered: With an ordered list of operations, MongoDB executes the operations serially. If an error occurs during the processing of one of the write operations, MongoDB will return without processing any remaining write operations in the list.
- unordered: With an unordered list of operations, MongoDB can execute the operations in parallel. If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list.
To use the Bulk() methods:
- Initialize a list of operations using either db.collection.initializeUnorderedBulkOp() ordb.collection.initializeOrderedBulkOp().
- Add write operations to the list using the following methods:
- Bulk.insert()
- Bulk.find()
- Bulk.find.upsert()
- Bulk.find.update()
- Bulk.find.updateOne()
- Bulk.find.replaceOne()
- Bulk.find.remove()
- Bulk.find.removeOne()
- To execute the list of operations, use the Bulk.execute() method. You can specify the write concern for the list in the Bulk.execute() method.
Once executed, you cannot re-execute the list without reinitializing.
For example,
var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert( { _id: 1, item: "abc123", status: "A", soldQty: 5000 } );
bulk.insert( { _id: 2, item: "abc456", status: "A", soldQty: 150 } );
bulk.insert( { _id: 3, item: "abc789", status: "P", soldQty: 0 } );
bulk.execute( { w: "majority", wtimeout: 5000 } );
Common Commands
- user commands
- getLastError()
- isMaster()
- aggregation operations
- map-reduce
- count
- findAndModify: for complex updates. Nice way to do atomic, like, dequeuing of things from a queue
- dba commands
- drop collection
- create collection
- compact collection
- serverStatus
- replSetGetStatus()
- addShard
Which of the following are user commands (as opposed to admin commands)? Check all that apply.
db.runCommand()db.runCommand( { << command name >> : <value>, param1: value1, param2 : value2} )
to get confirmations when the last operation was completed:
db.runCommand( { "getLastError" : 1,
"w" : 2
"wtimeout" : 3 }
)
When we execute an operation through the shell or another interface, we can use which of the following?
db.isMaster()
db.runCommand( "isMaster" )
db.isMaster()
db.runCommand( { "isMaster" : 1 } ) --> tell us if the server we are talking to is primary or not
What does the db.isMaster() command do?
db.serverStatus()
db.serverStatus() --> gives a lot of the statistics on what is happening on the server and a alot of those are used by the MMS (Mongo Monitoring Service)
When running the command db.serverStatus() in the shell, what does the “ok” field represent?
db.currentOp() & db.killOp()
db.currentOp() --> shows information of the current operation
db.killOp() --> to stop a long-running operation
If you’re looking for problems with database performance, what is a good place to look, when you run db.currentOp()?
ensureIndex(), getIndexes() & dropIndex()
db.collection.ensureIndex( { "name" : 1} ) --> to create a index
db.collection.getIndexes() --> get the indexes of a collection
db.collection.dropIndex(index) --> drop th indexes of a collection
db.products.find().explain() --> give us information about how the find was done
What will happen if an index is created on a field that does not exist in any of the documents in the collection?
collection.stats() & collection.drop()
db.collection.stats() --> give us statistical information about the collection
db.collection.drop() --> remove collection even though from namespaces
True or false: db.collection.remove({}), which removes all messages in a collection, is the same as db.collection.drop(), which drops the collection.
Review of Commands
Commands:
- Server
- isMaster
- serverStatus
- logout
- getLastError
- db
- dropDatabase
- repairDatabase
- clone
- copydb
- dbStats
- collection
- DBA
- create --> is implicit
- drop
- collstats
- rename collection
- user
- count
- aggregate
- MapReduce
- findAndModify
- geo*
- index
- ensureIndex
- getIndex
Which of these statements is true?
Thanks for sharing the valuable information,This is useful information for online learners
ResponderEliminarMongoDB Training in Chennai
Nice post. Keep sharing. For best practical classes
ResponderEliminarMachine Learning training in Pallikranai Chennai
Pytorch training in Pallikaranai chennai
Data science training in Pallikaranai
Python Training in Pallikaranai chennai
Deep learning with Pytorch training in Pallikaranai chennai
Bigdata training in Pallikaranai chennai
Mongodb training in Pallikaranai chennai