CRUD Flashcards
(21 cards)
crud
stands for create read update and delete the data
an operation in data managements
BSON
data format used in mongodb. it is binary-encoded
of json and extends this
one example is that objectId used in mongodb but this format is not legal in json data
two documents don’t need to have the same schema
may one document has four fields and another one has
two
db.collectionName.deleteOne(filter, options)
this delete the first document that match with the filter
db.collectionName.updateOne(filter, data, options)
db.collectionName.updateOne(filter, {$set: {key: value}})
it updates the first document in the collectionName
that matched with the filter and it add or replace with data
to do this you should use $set with :
db.collectionName.updateMany(filter, data, options)
update more than one document that matched with filter
db.collectionName.deleteMany(filter, options)
delete every documents that matched with the filter
db.collectionName.insertMany([data1, data2, …])
allows to insert many documents to collectionName of db
db.collectionName.find(filter)
find all documents that matched with filter
do.collectionName.findOne(filter)
find the first element that matched with the filter
note: pretty not work in this method
key: {$gt: value}
this is a filter for every documents that has greater value
for that key
db.collectionName.replaceOne(filter, data, options)
replace the existing data with the new data if filter was matched
db.collectionName.update(filter, data, options)
deprecated
it do the same as replaceOne do but not update and not
like updateOne or Many
if the documents be more than 20
it doesn’t show the others it called cursor object
db.collectionName.find().toArray()
shows all the documents in array even more than 20
projection
if you wana retrive some special fields of document, not entire document,
should use projection and it reduce usage of bandwith, for this use find(filter, {key: 1})
if you want to dont retrive id you must use _id: 0, in projecction section
Projection ai explaination
In MongoDB, projection refers to the process of selecting only a subset of fields from a collection. When querying a MongoDB collection, you can specify which fields you want to retrieve using a projection. This can be useful when you only need certain pieces of data and don’t want to retrieve the entire document.
To project fields in MongoDB, you use the projection parameter when calling the find() method. For example, if you have a collection of documents representing products with fields such as name, price, and description, and you only want to retrieve the names of the products, you would use:
db.products.find({}, { name: 1 })
This query will retrieve all documents in the products collection, but only return the name field for each document. The 1 value in the projection indicates that the field should be included in the result, while a 0 value would indicate that the field should be excluded.
You can also use complex projections to include or exclude multiple fields, or to specify nested fields within embedded documents. The syntax for projections can be found in the MongoDB documentation.
read essays on folder
embedded documents or arrays
limited to 100 embedded docs
and has limited size below 16MB
db.collectionName.find({“embedded.key”: “valu”})
returns all docs with filter of key of embedded document has vlue of vlue
db.collectionName.find().embedded
return all embedded documents of all documents