Mongodb:the Definitive Guide [1]

Source: Internet
Author: User
Tags mongodb client

Introduction to the first chapter

MongoDB is a document-oriented database, not a relational database. Built-in support for MapReduce, as well as support for geo-spatial indexes.

    • A rich data model
    • Easy to scale, it uses a document-oriented data model that allows it to split data between multiple servers
    • Rich functionality, indexing, storing JavaScript, aggregation, fixed collections, file storage
    • Deliver server-side processing logic to the client as much as possible without sacrificing speed
    • Easy management, as much as possible for the server to automatically configure

Chapter II Introduction

2.1 Documentation

Documentation is the core concept of MongoDB.

    • MongoDB not only distinguishes between types, but also case-sensitive.
    • Cannot have duplicate keys.
    • The key of the document is a string and cannot contain ' \ s ', which is used to denote the end of the key
    • . and $ have special meaning to be kept
    • The key that begins with the underscore _ is reserved

2.2 Integrated

A collection is a set of documents.

    • It is better to divide the different types into different sets by using the key callout type in a collection, so that the query will be much faster.
    • Put documents of the same type into a collection so that the data is more concentrated
    • An index is defined by a collection.

Named:

    1. The collection name cannot be an empty string ""
    2. The collection name cannot contain the \ s character (null character)
    3. The collection name cannot begin with system.
    4. User-created collection names cannot contain $

It is recommended to use subcollections to organize your data. With "." Divided by namespace.

2.3 Database

Multiple collections make up the database. The database is identified by name:

    • Cannot be an empty string
    • Must not contain ' 、.、 $,/, \, and
    • Should all lowercase
    • Up to 64 bytes

Keep database: admin, local, config

2.4 Start MongoDB

To start the service, you need to run the Mongod executable file.

    • Default path/data/db (C:\data\db); Port 27017
    • Starting the HTTP server, the listening number is 1000 higher than the primary port number. For the default port, which is 28017, you can access http://localhost:28017 to get database management information.

2.5 MongoDB Shell

Run MONGO to start the shell. (MONGO [Host/db:port])

    • A full-featured JavaScript interpreter that leverages the JavaScript standard library to define and invoke JavaScript functions that can be used with multiple lines of command.

MongoDB client, global variable db, switching database with use [db] command

Basic operations

    1. Create Db.blog.insert (POST)
    2. Read Db.blog.find () | Db.blog.findOne ()
    3. Update db.blog.update (condition, new)
    4. Delete Db.blog.remove (condition)

Db.help () can view the help of a database-level command, and the associated Help for a collection can be viewed through db.foo.help ().

If the function name does not write parentheses, the JavaScript source code for the function is displayed.

    • Db.version cannot return the correct collection when there is a property with the same name as the target collection (for example, version), you can use the GetCollection function: Db.getcollection ("version")
    • x.y and x[' y ' are completely equivalent, db.blog.posts can also write db.blog[' posts ']
var collections = ["posts", "comments", "authors"] for in collections) {    Dostuff (Db.blog[collections[i]);}

2.6 Data types

Basic data type, number, date (new Date ()), array, inline document, and so on.

    • Try not to overwrite the entire document under the shell (integer converted to floating point)

_ID and Objectid

    • Objectid is the default type for _id, with 12 bytes of storage space

    • The uniqueness of the second level, roughly in the order in which they were inserted, implies when the document was created
    • _ID is typically done by the client driver

Chapter III Creating, updating, and deleting documents

Basic Insert Delete Modify action

Db.foo.insert ({"Bar": "Baz"}) Db.users.remove () db.mailing.list.remove ({"opt-out":true  = Time.time () db.drop_collection ("Bar") joe.relationships={"Friends": Joe.friends, "Enimies": Joe.enimies}; Delete joe.friends; Delete joe.enimies;db.users.update ({"name": "Joe"}, Joe)

Using the modifier $set, $inc, $ne, $push, $addToSet, $each

//Add or remove a key valueDb.users.update ({"Name": "Joe"}, {"$set": {"Favorite book": "War and Peace"}}) db.users.update ({"Name": "Joe"}, {"$unset": {"Favorite book": 1}})//Increase or decrease valueDb.games.update ({"Game": "Pinball", "User": "Joe"}, {"$inc": {"score": 50}})//add a value to an arrayDb.papers.update ({"Authors cited": {"$ne": "Richie"}}, {"$push": {"authors cited": "Richie"}})//add value in collectionDb.users.update ({"_id": ObjectId ("xxxx")},{"$addToSet": {"emails": "[email protected]"}}) db.users.update ({"_id": ObjectId ("xxxx")},{"$addToSet": {"emails": {"$each": ["[Email protected]", "[email protected]"]}}})//Delete value in collectionDb.lists.insert ("Todo": ["Dishes", "laundry", "dry cleaning"]) db.lists.update ({},{"$pull": {"Todo": 1}})//Remove from end of arrayDb.lists.update ({},{"$pop": {"Todo":-1}})//Delete from the beginning of the array//Array PositioningDb.blog.update ({"POST":p ost_id},{"$inc": {"comments.0.votes": 1}})//Use subscriptDb.blog.update ({"Comments.author": "John"},{"$set": {"Comments.$.author": "Jim"}})//Replace only the first one

Fourth Chapter Inquiry

Fifth Chapter Index

Sixth Chapter Aggregation

Seventh Chapter Advanced Guide

Eighth Chapter Management

Nineth Chapter Copy

Tenth Chapter Shard

The 11th Chapter Application Example

Mongodb:the Definitive Guide [1]

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.