MongoDB (a) Introduction and basic operation (Beginners)

MongoDB (a) Introduction and basic operation (Beginners)

1 Data Type

1.1 BSON

Documents (Documents) is the basic unit of data in MongoDB, it is an ordered collection of key-value pairs, similar to the data structure in JSON, but because JSON supports only basic 6 kinds of data types: null, boolean, numeric, character string, array and object, and therefore its expression is limited, so MongoDB develop new JSON-based data format BSON. BSON (Binary JSON) JSON document is similar to the coding sequence of binary format, and JSON is similar, but supports more data types. All current BSON support MongoDB 4.0 of the following data types: Null, Boolean, Double, String, Array, Object, Binary data, ObjectId, Date, regex, JavaScript, Timestamp, long, decimal, minKey, maxKey and so on.

1.2 ObjectId

MongoDb each document stored in the set has a unique _id field as the primary key, either manually inserted, or automatically generated by the program, which can be any data type, default ObjectId type.

ObjectId using 12 bytes of storage, is a string of 24 hexadecimal digits, each string can store two hexadecimal digits:

. A first four-byte time stamp from the standard is the epoch of seconds;

. B intermediate 5 bytes is composed of two parts: the first 3 bytes of the host that is a unique identifier, typically a hash value hostname for ensuring the ObjectId different hosts generate different, the two bytes the process identifier (PID), to ensure that on the same host different processes to produce different ObjectId.

c. the last 3 bytes is an increment a counter, to ensure that the same process on the same host at the same second ObjectId generated are different, i.e., each process can have one second up to 16,777,216 different ObjectId (16777216 = (2 * * 8) * 3,1 equal to 8 bit binary bytes).

{ "_id" : ObjectId("5e82546e077a2d29f4f1d017"), "x" : 1 }

2 basic database operations

1, check the database

show dbs

2, database (the database may not be present)

use users

3, view the current database

db

4, view collection

show collections

5, new and used collection - test

db.test.insertOne( {name:"mike",age:25} )

At this point, the database users to automatically create, collection is also automatically create a test table. Or create a collection manual

db.createCollection("log",	{ capped:true,size:5242880,max:5000 })

6, delete the collection - test

db.test.drop()

7, delete the current database

db.dropDatabase()

3 mongodb view the database and table information

Check method is recommended to use methods stats, intuitive and detailed

1, check the database status information

db.stats()

{
“db” : “myNewDB”,
“collections” : 3,
“views” : 0,
“objects” : 12,
“avgObjSize” : 71.25,
“dataSize” : 855,
“storageSize” : 86016,
“numExtents” : 0,
“indexes” : 3,
“indexSize” : 86016,
“fsUsedSize” : 5271592960,
“fsTotalSize” : 18682343424,
“ok” : 1
}

2, view the database user tables in user status information

db.user.stats()

3, see the help file db

db.help()

DB methods:
db.adminCommand(nameOrDocument) - switches to ‘admin’ db, and runs command [just calls db.runCommand(…)]
db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor…

4, see the table help files

db.user.help()

DBCollection help
db.user.find().help() - show DBCursor help
db.user.bulkWrite( operations, ) - bulk execute write operations, optional parameters are: w, wtimeout, j…

Well, now you're familiar with the basic concepts and underlying operating mongoDB, ready to advanced!

Published 22 original articles · won praise 22 · views 770

Guess you like

Origin blog.csdn.net/weixin_45568892/article/details/105233166