MongoDB Big Data Grammar Encyclopedia

Source: Internet
Author: User
Tags auth reserved

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.


The advantage of Bson is that
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//Toggle existing database or create a new database
To view available databases and collections


Show DBS//will only show databases that already exist


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 ()//results show all documents in it


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)//Fetch the number of first n results of a document
Db.testDB.find (). Skip (20)//skip top N data for a document


Use these functions in combination
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}). The count ()//count () function ignores the skip () or limit () parameters by default, and if you do not want to be ignored, you need to use COUNT (true )
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: +}})
$gte greater than or equal to $lt less than $lte less than or equal to $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 last n
similar to 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 the 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})//Can be implemented using the Save () command 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 number 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 a sequence of tables
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.