JSON and MongoDB
JSON is not just a way of exchanging data, but also a good way to store data, in fact MongoDB does not use JSON to store data, but instead uses an Open data format, called Bson, developed by the MongoDB team.
Document-oriented storage Bson
Bson is an open standard where Bson storage uses more space than Jsno (couchdb a powerful document-oriented database) in the same version.
1, processing data faster than JSON, consume a certain amount of storage space, simply said Bson easier to browse, traverse the index page very quickly.
2, using Bson easy to quickly convert its data into a programming surplus of the native data format
3. Bson also provides some extensions to JSON, Bson can store binary data, and handle specific data types, so Bson can store any JSON document, but a valid Bson document may not be a valid JSON
Browse the database
Use TestDB? Switch existing database or create a new database
To view available databases and collections
Show DBS?? Only the databases that already exist are displayed
Show collections//Show all collections in the current database
Inserting data into the collection
The most common operation is to insert data into the collection, all of which are stored in Bson format, insert data to define the data, and then use the Insertone function to save them in the collection, or use the Insert function to temporarily enter the contents of the document.
Douctment= ({"Key1": "Values"})
Db.test.insertOne (douctment)
???? When inserting data, the key name must follow the following rules:
???? The $ character cannot be the first character of a key name
???? Polka dots [.] Cannot appear in the key
???? Name _id is reserved for use
???? The name of a collection cannot exceed 128 characters
???? An empty string ("") cannot be used as a collection name
???? The collection name must start with a letter or an underscore
???? The collection name System is MONGDB reserved and cannot be used
???? The collection name cannot contain the null character "\"
All data query
Db.testDB.find ()?? Result Zombies all of these documents
Get a specific type of document
Db.testDB.find ({"Key1": "Values"})
Gets the data title associated with the values, ignoring the other
Db.testDB.find ({"Key1": "Values"},{"Tilte": 1})
Using functions sort, limit, and Skip
Db.testDB.find (). Sort ({"Tilte": 1})//The result of the key is ascending, if-1 is descending
Db.testDB.find (). Limit (10)?? Number of first n results to fetch a document
Db.testDB.find (). Skip (20)???//Skip the first n data of a document
? Use these functions together
? Db.testDB.find (). Sort ({"Tilte": 1}). Limit (Ten). Skip (10)
?
There are additional concepts and features to be aware of when using queries in MongoDB, including fixed collections, natural order, and $natural
Natural order: A native sort method that is combined in a database, so if the specified sort order is not displayed when the document in the collection is queried, the result is returned by default in the forward natural order.
Fixed set: is a collection of databases, and its natural order is guaranteed to be consistent with the order in which the documents are inserted, to ensure that the natural order is consistent with the document insert order, the other advantage is that the size of the collection is fixed, the oldest data will be deleted, the most recent data will be added to the end, to ensure that the natural order and
Fixed collections must use the CreateCollection function, such as creating a fixed collection named Auth
? db.createcollection ("auth": {capped:true,size:20480})
You can use Max to limit the number of documents in a pinned collection
? db.createcollection ("auth": {capped:true,size:20480,max:100})
$natural: Since the fixed set guarantees that the natural order is consistent with the insertion order, you do not need to use any special parameters, any other special commands or functions when querying, such as finding the last 10 data
Db.testDB.find (). Sort ({$natural: -1}). Limit (10)
To view the size of a collection
Db.testDB.stats ()
Get a single document
Db.testDB.findOne ()
Using clustered naming
Count () function returns the number of documents
Db.testDB.count ()? Specify the number of documents in the collection
Perform additional filtering statistics
Db.testDB.find ({"Key1": "Values"},{"Tilte": 1}). Count ()? The//count () function ignores the skip () or limit () parameters by default and uses count (true) if you do not want to be ignored
Db.testDB.find ({"Key1": "Values"},{"Tilte": 1}). Limit (.) count (True)
Get unique values using the DISTINCT function
Db.testDB.distinct ({"Tilte"})
Grouping Results Group ()
???? The purpose of the command is to return an array of grouped elements, and the function group () accepts 3 parameters: Key,initial and reduce, but does not work in the Shard environment
???? The parameter key specifies which key you want to use to group the results.
???? The parameter initial allows you to provide a cardinality for each grouped result (the starting cardinality of the start statistic for the element), and if you want to return the specified number, the parameter defaults to 0
???? Parameter reduce group all similar entries together, it accepts 2 parameters: items and Pre
???? Db.testDB.group (
???? key:{"Tilte": true},
???? initial:{total:0},
???? Reduct:function (Items,pre) {
???? prev. Total+=1
????}
????)
Using conditional operators
???? $gt greater than parameter ?
???? Db.testDB.find ({"Key1": {$gt: 2000}})
???? $gte greater than or equal to $lt less than $lte less than equals? $ne not equal to
???? to specify a matching array
???? Db.testDB.find ({"Key1": {$in: [1,2,3,4]}})? Similar and $nin.
???? match all properties of document $all
???? Db.testDB.find ({"Key1": {$all: [1,2,3,4]}})
???? Search for multiple expressions in a document
???? Db.testDB.find ({$or: [{"Key1": "Values1"},{"Key2": "Values2"}]})?//$nor
???? get documents using $slice
???? Db.testDB.find ({"Key1": "Values1"},{"Cast": {$slice: 3}})?//Get the first 3 items, negative for the second n
???? Like LIMT? n,m
???? search for odd and even $mod
???? Db.testDB.find ({"Key1": "Values1"},{"Cast": {$mod: [2,0]}})
???? Filter Results using $size: Filter out the size of the array in the document
???? Db.testDB.find ({"Key1": {$size: 2}})
???? returns the object that contains a specific field: $EXISTS (full table scan, cannot use index)
???? Db.testDB.find ({"Key1": {$exists: true}})
???? based on Bson type match result: $type can match results based on Bson type:
???? Db.testDB.find ({"Key1": {$type: 3}})?
???? match a complete array? $elemMatch operator
????
???? using regular Expressions
???? Db.testDB.find ({"Key1":/^sharesoe*/i})
????
Update data
???? With update () updates, the function accepts 3 main parameters: Criteria, objnew, and options
???? Argument criteria can be used to specify a query
???? Parameter objnew specifying update information
???? Parameter options are used to specify the option to update the document, Upsert and Multi,upsert are updated to create updates without updates, multi can specify whether all matching documents should be updated or only the first document is updated
???? Db.testDB.updateOne ({"Key1": "Values"},{"Tilte": 1},{upsert:true})?//You can use the Save () command to implement Upsert
???? Db.testDB.save ({"Key1": "Values"},{"Tilte": 1})?
???? # #此更新会导致多余的key-value was deleted
???? add value using $inc
???? Db.testDB.updateOne ({"Key1": "Values"},{$inc: {"Tilte": 5}})
???? update the specified value
???? Db.testDB.updateOne ({"Key1": "Values"},{$set: {"Tilte": "Sharesoe.com"})?
???? Delete the specified field
???? Db.testDB.updateOne ({"Key1": "Values"},{$unset: {"Tilte": 1})?
???? adds a value in the specified field $push, or multiple values with $each
???? Db.testDB.updateOne ({"Key1": "Values"},{$push: {"Tilte2": "Haha"}})
???? Db.testDB.updateOne ({"Key1": "Values"},{$push: {"Tilte2": {$each: ["haha", "]}})
???? adding data to the array
???? Db.testDB.updateOne ({"Key1": "Values"},{$addToSet: {"Tilte2": {$each: ["haha", "]}})
???? Deleting an element in an array
???? Db.testDB.updateOne ({"Key1": "Values"},{$pop: {"Tilte2": 1}})?//positive is the last element
???? delete a specified value, multiple values in an array
???? Db.testDB.save ({"Key1": "Values"},{$pull: {"Tilte2": "Haha"}})
???? Db.testDB.updateOne ({"Key1": "Values"},{$pullAll: {"Tilte2": ["haha", [+/-]}})
????
Batch processing data
???? The batch processing of MONGODB data is divided into order processing and unordered, the data processing in order to stop the writing of the remaining data, the disorderly operation in parallel, if the error will execute other data
???? Orderly implementation
???? var Bulk=initializeorderbulkop ()? Initialize an ordered list
???? Insert Sequence table bulk, execute execute () last
???? Bulk.insertone (XX)
???? Bulk.execute ()
???? Evaluation output: After executing the Execute () command, it is possible to review the write operation and evaluate if all data has been successfully written, via GetOperations ()
???? Bulk.getoperations ()
???? Batchtype 1 Insert 2 Update 3 Remove
???? Renaming collections
???? Db.testDB.renameCollection ("NewName")
???? Delete data one or more lines
???? Db.newname.deleteOne ({"Key1": "Values"})?//Delete Match a document
???? Db.newname.deleteMany ({})
???? Delete all documents for a collection
???? Db.newname.drop ()? Removed??
???? Delete Collection
???? Db.dropdatabase ()
MongoDB Big Data Grammar Encyclopedia