MongoDB Learning Notes (i)

Source: Internet
Author: User
Tags modifiers

Recommended Learning Books: MongoDB authoritative guide

First, Introduction

MongoDB: A document-oriented database. (Not relational database)

Its document-oriented data model enables it to automatically split data across multiple servers.

Second, the introduction

1. What is a document?

The basic unit of data in MongoDB when the document is in. (similar to rows in a relational database, but more complex than rows).

Self-understanding: Multiple key-value pairs are put together in order

Attention:

    • The key in the document is a string (with a few exceptions you can use any of the utf-8 characters)
    • MongoDB not only distinguishes between types, but also case-sensitive. such as: The number 3 and the string "3" are different.
    • Each document has a unique _id to indicate

2. Collection

A collection is a set of documents. Is modeless, that is, a collection of documents can be of various kinds.

3. Database

Multiple documents make up a collection, and the same multiple collections make up the database.

4. MongoDB Shell

MongoDB comes with a JavaScript shell that can interact with the MongoDB instance from the command line.

4.1. Basic operations in the shell (CRUD)

    • Create an Insert
    • Read find () or FindOne ()
    • Updating update ()
    • Delete Remove

4.2 Learn to use Help

5. Data type

NULL, Boolean, 32-bit integers, 64 integers, 64-bit floating-point numbers, strings, symbols?? , object ID, date, regular expression, code, binary data, maximum value, minimum value, undefined, array, inline document (common)

Iii. creating, updating, and deleting documents

1. Insert

If you insert multiple documents, bulk inserts will be faster.

2. Delete

Deleting data is permanent, cannot be undone, and cannot be recovered.

3. Update the documentation

    • Document Replacement Delete Update
    • Using modifiers
      • $set is used to specify a value for a key that is created if the key does not exist.
      • $inc the value used to increment and have a key, and if the key does not exist, create a key.
      • Array modifiers $push $ne (conditional judgment) $addToSet (you can avoid duplicates) $addToSet and $each groups and together, you can add multiple different values. $pop $pull
      • Array positioning modifier "." "$" (the locator only updates the first match to the element)
    • Upsert is a special kind of update. Without a matching document, a new document will be created based on this condition and the updated document. The third parameter of update is set to True.
      • By default, updates can only be updated on the first document that meets the criteria. If you want to update multiple documents, the fourth parameter of update is set to True
      • Returns the updated Documents Findandmodify () and GetLastError view the number of documents updated

Third, query

The query is to return a subset of the document in a collection.

1. Description of the Find () function

To query a document:

When the query document is empty ({}), the entire collection is returned. You can add multiple key-value pairs to the query document. (The conditions are grouped together with and)

Find ({"user_name": "Joe", "age": +, "sex": "Man", ...})

Make the key returned:

Sometimes it is not necessary to return all key-value pairs in the document. You can now use the second parameter of find to make the key you want to display. (1: Display 0: not shown)

Find ({}, {"user_name": 1, "email": 1, "_id": 0})

2. Query conditions

    • Comparison operators $lt, $lte, $GT, $gte correspond to <, <=, >, and >= respectively. For example: {"Age": {"$gte": "LTE": (18<=AGE<=30)}
    • or query
      • $in single key matches multiple values {"age": {"$in": [12, 13, 17, 30]}
      • $or multiple condition combinations and an and condition a little different {"$or": [{"Age": {"$in": [98]}, {"Winner": True}]}
      • $not-element conditional sentence, which can be used on any condition
      • Type-specific queries
        • $null (1. Value is null 2. Match does not exist)
        • Regular expressions
      • Array query
        • $all {"Fruit": {"$all": ["apple", "Banana"]}}
        • $size {"Fruit": {"$size": 3}}
        • $slice {"Comments": {"$slice": Ten}} #前10条评论
      • Querying internal documents
        • $eleMatch to group the qualifying conditions only if you need multiple key operations on an inline document.
    • $where Query
    • The cursor database Returns the result of find execution with cursors.
    • Limit, skip, and sort

MongoDB Learning Note (i)

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.