Common Basic data types:
Null is used for fields that represent null values or that do not exist:
{"Data": null}
A Boolean type has only two values, True and false:
{"Data": true}, {"Data": false}
The data of a string type consists of UTF-8 characters:
{"Data": "Pingan"}
When querying, the regular expression is used as a qualification for syntax and JavaScript regular expressions:
{"Data":/pingan/i}
The object ID is a 12-byte (24-character) ID, which is a unique identifier for the document.
{"Data": ObjectId ()}
The shell defaults to a 64-bit floating-point value, which is the double type. For integer values, you can use the Numberint class (4-byte signed integer) or the Numberlong class (8-byte signed integer).
{"Data": 3.33}, indicating double type
{"Data": Numberint ("3")}, representing int type
{"Data": Numberlong ("3")}, indicating long type
Either a data list or a dataset can be represented as an array. Elements of an array can be numeric, string, and other basic data types, separated by commas.
{"Data": [[+]}, {"Data": ["a", "B", "C"]}
The date is stored as the number of milliseconds elapsed since the new era, and the time zone is not stored:
{"Data": New Date ()}
Documents can nest other documents and be nested documents as the values of the parent document:
{"Data": {"Company": "Pingan"}}
Binary data is a string of any byte, and the binary data is the only way to save non-UTF-8 characters to the database. For example, save the image data. But it cannot be used directly in the shell.
1 //Save the picture in MongoDB2 Public voidSAVEIMGMG (byte[] byteimg)3 {4Document doc =NewDocument ();5doc["ID"] = 1;6doc["IMG"] =byteimg;7 Mongocollection.save (DOC);8 }9 //get picture byte data from MongoDB storeTen Public byte[] GETIMGMG () One { ADocument doc= Mongocollection.findone (NewDocument {"ID", 1 } }); - returndoc["IMG"] as Binary; -}
View Code
Document:
A document is an ordered set of key-value pairs, a basic unit of data in MongoDB, very similar to a row in a relational database management system, but more expressive.
1 varMyDoc = {2_id:objectid ("5099803df3f4948bd2f98391"),3Name: {first: "Alan", Last: "Turing" },4BirthNewDate (' June 23, 1912 '),5Death:NewDate (' June 07, 1954 '),6contribs: ["Turing Machine", "Turing test", "Turingery" ],7Views:numberlong (1250000)8}
Collection:
A collection is a set of documents, and if a document in MongoDB is likened to a row in a relational database, then a collection is equivalent to the concept of a table.
Database:
In MongoDB, multiple documents form a collection, and multiple collections can comprise a database, a MongoDB instance, which can host multiple databases, each with 0 or more collections. MongoDB3.0 in this version, three database names are reserved. are: admin, local, config.
Admin: In terms of authentication, this is the "root" database. If you add a new user to the Admin database, the user automatically gets permissions for all databases.
Local: This database can never be copied, and all local collections on a server can be stored in this database.
when Config:mongodb is used for sharding settings, shard information is stored in the config database.
Basic operations in the shell:
The shell uses 4 basic operations: Create, read, update, and delete (that is, crud operations).
Create:
1 db.person.insert ({"Name": "Ryan", "age": +}); 2 db.person.find ({"Name": "Ryan"});
Update: Use update to modify the personnel information. Update accepts (at least) two parameters, the first is a qualification (to match the document to be updated), and the second is a new document.
1 db.person.update ({"Name": "Ryan"},{"name": "Ryan", "age": +}); 2 db.person.find ({"Name": "Ryan"});
Delete: Use the Remove method to permanently delete a document from the database. If you do not use any parameters, it will delete all the documents in the collection (very use!!). )。 It can accept a document as a qualification as a parameter.
1 db.person.remove ({"Name": "Ryan"}); 2 db.person.find ({"Name": "Ryan"});
Play turn MongoDB (ii): MongoDB Basics