(1) The Definition of Documents
A record in a MongoDB collection and the basic unit of data in MongoDB. Documents are analogous to JSON objects but exist in the database in a more type-rich format known as BSON.
(2) The Definition of Collections
A grouping of MongoDB documents. A collection is the equivalent of an RDBMS table.
(3) The Definition of Fields
A name-value pair in a document. A document has zero or more fields. Fields are analogous to columns in relational databases. For example, a certain MongoDB document will be seen as follows,
1{
2 field1: value1,
3 field2: value2,
4 field3: value3,
5
6 fieldN: valueN
7}
(4) Reasons for MongoDB
Ease of use: schemaless
Ease of scaling: can be used for distributed computing
Supports many features:
Suports many languages/drivers
(5) Data Type of MongoDB
Number()
, 4-byte integer NumberInt()
, 8-byte integerNumberLong()
Date()
_id
, every document must have this field(0) External Operations
xxxxxxxxxx
11$ mongod --dbpath=<path>
xxxxxxxxxx
11$ mongoimport --db <DBname> --collection <collectionname> --file <filepath>
Note that this command requires mongo-tools
,
xxxxxxxxxx
11$ conda install mongo-tools
We can also select the mode by option --mode
between insert
(means to allow duplicate) or upsert
(means to update duplicate).
(1) Creation
database_name
xxxxxxxxxx
11use database_name
collection_name
xxxxxxxxxx
11db.createCollection(collection_name)
doc_name
in collection collection_name
xxxxxxxxxx
11db.collection_name.insertOne(doc_name)
doc_name_1
and doc_name_2
in collection collection_name
xxxxxxxxxx
11db.collection_name.insertMany([doc_name_1, doc_name_2])
xxxxxxxxxx
11db
(2) Read
{"field" : value}
xxxxxxxxxx
11db.collection_name.find({"field" : value})
{"field_1" : value}
without returning field_2
xxxxxxxxxx
11db.collection_name.find({"field_1" : value}, {"field_2" : false})
{"field" : value}
xxxxxxxxxx
11db.collection_name.findOne({"field" : value})
{"field_1" : value}
without returning field_2
xxxxxxxxxx
11db.collection_name.findOne({"field_1" : value}, {"field_2" : false})
collection_name
xxxxxxxxxx
11db.collection_name.count()
(3) Update
updateOne
or updateMany
to {upsert : true}
field_new
as value_new
in one document matching rule {"field" : value}
xxxxxxxxxx
11db.collection_name.updateOne({"field" : value}, {$set : {"field_new" : value_new}})
field_new
as value_new
in all documents matching rule {"field" : value}
xxxxxxxxxx
11db.collection_name.updateMany({"field" : value}, {$set : {"field_new" : value_new}})
field_del
in one document matching rule {"field" : value}
xxxxxxxxxx
11db.collection_name.updateOne({"field" : value}, {$unset : {"field_del" : ""}})
field_del
in all documents matching rule {"field" : value}
xxxxxxxxxx
11db.collection_name.updateMany({"field" : value}, {$unset : {"field_del" : ""}})
field_inc
by 5 in all document matching rule {"field" : value}
xxxxxxxxxx
11db.collection_name.updateMany({"field" : value}, {$inc : {"field_inc" : 5}})
field_1
to field_2
in all documentsxxxxxxxxxx
11db.collection_name.updateMany({}, {$rename : {"field_1" : "field_2"}})
field_min
no more than 5 in all documentsx1db.collection_name.updateMany({}, {$min : {"field_min" : 5}})
2// 6 -> 5
3// 4 -> 4
field_max
no less than 5 in all documentsx1db.collection_name.updateMany({}, {$max : {"field_max" : 5}})
2// 6 -> 6
3// 4 -> 5
element
to the end of the array value of field field_array
in all documentsxxxxxxxxxx
11db.collection_name.updateMany({}, {$push : {"field_array" : element}})
field_array
in all documentsxxxxxxxxxx
11db.collection_name.updateMany({}, {$pop : {"field_array" : -1}})
field_array
in all documentsxxxxxxxxxx
11db.collection_name.updateMany({}, {$pop : {"field_array" : 1}})
element
in the array value of field field_array
in all documentsxxxxxxxxxx
11db.collection_name.updateMany({}, {$pull : {"field_array" : element}})
10
in the array value of field field_array
in all documentsxxxxxxxxxx
11db.collection_name.updateMany({}, {$set : {"field_array.$" : 10}})
10
in the array value of field field_array
in all documentsxxxxxxxxxx
11db.collection_name.updateMany({}, {$set : {"field_array.$[]" : 10}})
element.score = 5
to 10
in the array value of field field_array
in all documentsxxxxxxxxxx
31db.collection_name.updateMany({},
2{$set : {"field_array.$[element].score" : 10}},
3{arrayFilters : [ {"element.score" : 5} ]})
(4) Delete
{"field" : value}
xxxxxxxxxx
11db.collection_name.deleteOne({"field" : value})
{"field" : value}
xxxxxxxxxx
11db.collection_name.deleteMany({"field" : value})
collection_name
xxxxxxxxxx
11db.collection_name.drop()