MongoDB Learning Note one: Getting Started

Source: Internet
Author: User
Tags findone mongodb client mongodb server

Document: Multiple keys and their associated values are "ordered" to be placed together.
{"Greeting": "Hello, world!", "foo": 3}
Collection: A set of documents. The collection is modeless, and the following two documents can exist in the same set:
{"Greeting": "Hello, world!"}
{"Foo": 5}
Naming conventions for collections:
(1) The collection name cannot make an empty string "".
(2) The collection name cannot contain the \ s character (null character), which represents the end of the collection name.
(3) The collection name cannot be "system." Begins with a prefix reserved for the system collection. For example system.users the user information that holds the database, the System.namespaces collection holds the information for all the database collections.
(4) User-created collection names cannot contain reserved characters of $.
Sub-collections Press "." Character Division. For example, a blog-enabled app Ken has two collections, namely Blog.posts and blog.authors. The purpose of this is to make the organizational structure better, although the collection of blogs has nothing to do with his subset.
Database: Multiple collections.
Multiple documents in MongoDB make up a collection, and multiple collections form a database.
Naming rules for databases: the database name can be any UTF-8 string that meets the following criteria.
(1) cannot be an empty string ("").
(2) must not contain ' (space) 、.、 $,/, \ and (null characters).
(3) should be all lowercase.
(4) up to 64 bytes.
Because the database name will eventually program files in the file system.
Reserved Special database name:
Admin: From the perspective of permissions, this is the "root" database. If a user is added to this database, the user automatically inherits permissions from all databases.
Local: This data is never copied and can be used to store a collection that is limited to a single local server.
Config: When MONGO is used for sharding settings, the Config database is used internally to package information about your shard in UC.
Namespace = database name. Collection Name
For example, if you use the Blog.posts collection in a CMS database, the namespace for this collection is cms.blog.posts.
Start MongoDB:
Linux: $./mongod
windows:$ Mongod.exe
Mongod Default Data Directory/data/db (under Windows C:\data\db\), default port 27017. It is important to create a data directory (such as Mkdir-p/data/db) and make sure that you have some permissions on the directory.
Mongod also launches a very basic HTTP server on port 28017, which can be accessed by the browser http://localhost:28017 to obtain management information for the database.
MongoDB Shell:mongodb comes with a JavaScript shell that can interact with MongoDB instances from the command line.
Run MONGO to start the shell:
$./mongo
The shell will automatically connect to the MongoDB server at startup, so be sure to start mongod before using the shell.
The shell is a fully functional JavaScript interpreter that can run any Javascripe program.
The shell is a standalone MongoDB client. When turned on, the shell connects to the MONGODB server's test database and assigns the database connection to the global variable db. This variable is the main entry point for accessing MongoDB through the shell.
The shell also has extensions for non-JavaScript syntax that are added to make it easier for users who are accustomed to SQL shell. For example, the most important thing to do is to select the database you want to use:
> Use Foobar
Switched to DB Foobar
Now if you look at the db, you will find that it points to the Foobar database:
> Dbfoobar
The collection can be accessed through the DB variable. For example, Db.baz returns the Baz collection for the current database.

Basic operations in the shell: Create, read, update, and delete (CRUD).
1. Create
Example: Suppose you want to store a blog post. First, create a local variable post, which is the JavaScript object that represents the document. There will be "title", "Content" and "date" (publication date) several keys.
> post = {"title": "My blog post",
"content": "Here ' s My blog post.",
"date": New Date ()}
This object is a valid Mong ODB the document, you can use the Insert method to save it to the Blog collection:
> Db.blog.insert (POST)
to call the Find method to view:
> Db.blog.find ()
2. Read
Find returns all the documents in the collection. If you just want to see a document, you can use FindOne:
> Db.blog.findOne ()
3. Update
Example: The first step to modify the variable post, add or subtract "comments" key:
> post.comments = [ ]
then perform the update operation to replace the article titled "My blog post" with the new version of the document:
> Db.blog.update ({title: "My blog post"}, Post)
The document is now available " Comments "Key, you can see again with find:
> Db.blog.find ()
4. Remove
Remove to permanently delete the document from the database. It deletes all documents within a collection without using parameters for invocation. He can also accept a document to specify a qualifying condition. For example, the following command removes the article we just wore:
> Db.blog.remove ({title: "My blog Post"})

Tips for using the shell
> Help
Use Db.help () to view Help for database-level commands;
Use Db.foo.help () to view Help for a collection.
Do not enter the parentheses when entering the function, and the JavaScript source code of the function is displayed. Such as:
> Db.foo.update
To view all the automatically generated JavaScript function API documentation provided by the shell, you can access the http://api.mongodb.org/js/

Data type of MongoDB:
· Null
Null is used to represent a null value or a field that does not exist.
{"X": null}
· Boolean
Boolean type has two values of ' true ' and ' false '
{"X": True}
· 32-bit integer
This type is not available in the shell. JavaScript only supports 64-bit floating-point numbers, so 32-bit integers are automatically converted.
· 64-bit integer
The shell also does not support this type. The shell uses a special inline document to display a 64-bit integer.
· 64-bit floating point number
The numbers in the shell are all of this type. Here is a floating-point number:
{"X": 3.14}
This is also a floating point number:
{"X": 3}
· String
UTF-8 strings can be represented as string types of data:
{"X": "Foobar"}
· Symbol
The shell does not support this type. The shell converts the symbol types in the database into strings.
· Object ID
The object ID is a unique 12-byte ID for the document.
{"X": ObjectId ()}
· Date
The date type stores the number of milliseconds that start from the standard era. No time zone is stored:
{"X": New Date ()}
· Regular expressions
The document can contain regular expressions, using JavaScript's regular expression syntax:
{"X":/foobar/i}
· Code
The document can also contain JavaScript code:
{"X": function () {/* ... */}}
· Binary data
Binary data can be made up of strings of arbitrary bytes. However, the shell cannot be used.
· Maximum Value
Bson includes a special type that represents the maximum possible value. There is no such type in the shell.
· Minimum value
Bson includes a special type that represents the minimum possible value. There is no such type in the shell.
· Not defined
Undefined types can also be used in documents (null and undefined are different types in JavaScript).
{"X": undefined}
· Array
Worth a collection or list can represent an array:
{"X": ["a", "B", "C"]}
· Inline document
The document can contain other documents, or it can be embedded as a value in the parent document:
{"X": {"foo": "Bar"}}

MongoDB Learning Note one: Getting Started

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.