Play turn MongoDB (ii): MongoDB Basics

Source: Internet
Author: User

Common Basic data types:

    • Null

Null is used for fields that represent null values or that do not exist:

{"Data": null}

    • Boolean type

A Boolean type has only two values, True and false:

{"Data": true}, {"Data": false}

    • String

The data of a string type consists of UTF-8 characters:

{"Data": "Pingan"}

    • Regular expressions

When querying, the regular expression is used as a qualification for syntax and JavaScript regular expressions:

{"Data":/pingan/i}

    • Object ID

The object ID is a 12-byte (24-character) ID, which is a unique identifier for the document.

{"Data": ObjectId ()}

    • Numerical

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

    • Array

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"]}

    • Date

The date is stored as the number of milliseconds elapsed since the new era, and the time zone is not stored:

{"Data": New Date ()}

    • Inline document

Documents can nest other documents and be nested documents as the values of the parent document:

{"Data": {"Company": "Pingan"}}

    • Binary data

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

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.