First entry JavaScript knowledge points (eight)

Source: Internet
Author: User
Tags bulk insert

jquery Performance Optimization
    • 1. For tags that are reused in jquery, use a variable to save it for the first time you choose.
    • 2. When using the class name selector, Add the parent selector (parent selector uses ID or signature)
    • 3. Use the Find method to select descendants
    • 4. Use the chained notation in jquery as much as possible
    • 5. Using Event delegates
Database

A database is a warehouse that organizes, stores, and manages data in accordance with its data structure.

About MongoDB

MongoDB is a database based on distributed file storage, written in the C + + language, designed to provide scalable, high-performance data storage solutions for Web Applications. MongoDB is a high-performance, open-source, modeless document-based database that is officially defined as a bridge between Key-value storage (high performance and high scalability) and traditional RDBMS (rich queries and features).

Document & BSON

The data format saved in MongoDB is bson, such as:

{  name: "sue",  age: 26,  status: "A",  groups: [ "news", "sports" ]}

The basic unit of data in MongoDB is called a document ( Document ), which is the core concept of mongodb, which consists of an orderly placement of values with more than one key associated with it, which corresponds to the row of the relational database in the Database. The data is stored on disk in MongoDB in the BSON Binary-JSON format () document. BSON (binary Serialized Document Format) is a binary form of a class JSON storage format that, like binary Json,bson and json, supports inline document objects and array Objects. But Bson has some data types, such as and types, that JSON does not have Date BinData . The advantage of Bson is its high flexibility, but its disadvantage is that space utilization is not ideal. Bson has three characteristics: light weight, ergodic, High Efficiency.

Create a database

usecommand can be used to create a new database or to switch to a database that already exists:

use DATABASE_NAME

If you want to see a list of all the current databases, you can use the Command:

show dbs

If you want to delete the database you are currently using, you can use the Command:

db.dropDatabase()
Create a Collection

You can use createCollection methods to create Collections.

Grammar

db.createCollection(name, options)

Parameter description

| Parameters | Type | Description | | --- | --- | --- | | Name | String | The name of the collection to create | | Options(optional) | Document | Specify the memory size and index Options |

OptionsOptions that you can use:

| field | Type | Description | | --- | --- | --- | | Capped (optional) | Boolean | If it is true , the CAP collection is Enabled. A capped collection is a Fixed-size collection that automatically overwrites the oldest entry when it reaches its maximum size. If specified true , the dimension parameter needs to be specified as Well. | | Autoindexid (optional) | Boolean | If true yes, the default value for Auto-create indexed _id fields is false . | | size (optional) | number | Specifies the maximum size of a byte capping Collection. If it is capped true , then you need to specify this FIELD. | | Max (optional) | number | Specifies the maximum number of files allowed in the capping Collection. |

If you want to remove a collection, you can use the following command:

javascript db.collection.drop()

Insert Document

In the database, data insertion is the most basic operation in MongoDB using a db.collection.insert(document) statement to insert a Document.

Documents are document data, and collection are collections of document Data. For example, the information for all users is stored in the users collection, and each User's information is a users document, inserting data:

db.users.insert(user);

* note: If the user collection does not exist, the user collection will be created automatically at this time

BULK INSERT

Insert statements can be inserted into a single document, as well as multiple documents at Once. When you insert multiple documents, the parameters of the Insert command are an array, and the array elements are bson-formatted Documents. Multiple documents can be placed within an array, inserting multiple data at a time, for example:

db.users.insert([{name:"tommy"},{name:"xiaoming"}])

Document bulk insertion is convenient, but there are also some issues to be aware of when using bulk inserts, because the Bson format limits the amount of data that can be inserted at one time without exceeding 16M, and MongoDB does not guarantee full success or complete failure when inserting multiple data into an insert Command.

Querying documents

In mongodb, the query points to a specific collection of documents, queries the set criteria, indicates the document that MongoDB needs to return, and the query can contain a projection that specifies the fields Returned.

In a relational database, a projection refers to a filter on a column, and, similarly, in mongodb, a projection is a filter on the properties of an object that appears in the result set.

Find () method

In mongodb, the query retrieves data using find commands, using the following method:

Syntax :

db.collection.find(criteria,projection);

parameter Description :

criteria– Query criteria, document type, Optional.

projection– The returned field, document type, optional, If you want to return all fields, this parameter is Ignored.

The Find command has two optional arguments, criteria is the query condition, projection is the returned field, and if not passed in the condition database returns all documents for that Collection.

Modify Document Update () command

updatecommand can update specific field values for the specified document, or replace the entire document, and MongoDB will reallocate space and relocate if the update operation increases the document Size.

Syntax :

db.collection.update(query,update,{upsert:boolean,multi:boolean});

Parameters :

query-query criteria, documents, and query conditions in find are written in the same notation. update-modify content, Documents. upsert (optional)-if The value is true , the document is created when there are no matching documents in the Collection. Default false . multi(optional)-if The value is true , all documents that match the criteria are updated, otherwise only one document is updated, default false .

Save () Command

savecommand to update or insert a new document, unlike update a command, you can save manipulate only one Document.

Syntax :

db.collection.save();

Parameters :

document-new documents;

Delete a document

When you need to delete a document using a remove command, deleting a document cleans up unwanted data, frees up storage space, and improves retrieval efficiency, but a bad deletion can be a disaster, so you need to be very cautious when performing data deletion operations!

Syntax :

db.collection.remove(  query,  justOne);

Parameters :

query-bson type, which removes the condition of the Document.

justOne-boolean type true : Delete only one document false : default, Delete all documents that match the CRITERIA.

Query condition Equality Condition

The equality condition is the simplest common query operation, and the query condition is the Bson object in the format {field:value}.

Syntax :

{ field: value }
Comparison criteria

The comparison condition is also one of the most basic query conditions, by comparing operators to filter or exclude the document, so as to obtain the document data we Want.

Syntax :

{ field: { op: value } }
Comparison operators op
    • $gt: Greater Than
    • $gte: Greater than or equal to
    • $lt: Less than
    • $lte: Less than or equal to
    • $ne: Not equal to
    • $in: Included in
    • $nin: not included in

Through the rich operators provided in mongodb, you can help you to accurately retrieve data, such as numeric fields can be used,,, and $gt $gte $lt $ne Other operators to compare, array conditions can be used $in , $nin to Achieve.

Array condition

When a field contains an array, you can make an array match exactly or match a specific value. A single element match only needs to pass in the value of the element, an exact match needs to pass in an array, and a specific element match requires a "field. index" form to pass in the Parameter.

Example

Array exactly matches:

db.inventory.find( { tags: [ ‘fruit‘, ‘food‘, ‘citrus‘ ] } );

Single element matching:

db.inventory.find( { tags: ‘fruit‘ } );

Specific element matches:

db.inventory.find( { ‘tags.0‘ : ‘fruit‘ } );
Subdocument conditions

When querying data, we may encounter data that contains embedded Subdocuments.

If the document contains an embedded subdocument, you can access the child document node using ". key", for example:

db.mycol.find({"access.level":5});
Compound query

When the query condition is multiple fields, you need to use a multi-field compound criteria query.

and compound query

example: queries all documents in the inventory collection where the Type field is food and the price is less than 95.

db.inventory.find({  $and:[    {type: "food"},    { price: { $lt: 95 }}  ]});
or compound query

example: retrieves all data in the inventory collection (qty) greater than 100 or ($or) price less than 9.95.

db.inventory.find({  $or:[    { qty: { $gt: 100 } },    { price: { $lt: 9.95 } }  ]});
Cursor cursor

The find command does not return the result directly, but instead returns an iterator to the result set, the Cursor.

To get the data, we can use the next method to traverse the cursor, as Follows:

var myCursor = db.inventory.find( { type: "food" } );var myDocument = myCursor.hasNext() ? myCursor.next() : null;if (myDocument) {    var myItem = myDocument.item;    print(tojson(myItem));}



What happens when we visit a web site? (http Transaction)

  • 1. Enter the URL
  • 2. Resolving Domain Names
  • Resolve the domain name to an IP address
  • 3. Establish a link (three handshake)
  • First time: the client sends the request to the server
  • Second Time: customer Service pick-up Information
  • Point three Times: customer service confirmation Information
  • 4. Send Data
  • 5. Break the link (four waves)

Client customer service Side

First time handshake →

Second handshake ←

Third handshake →

Server Service Side

Duplex Channel: both transmit and receive channels

HTTP Protocol: also known as a stateless protocol, each request is a new request for the server

Request Request:get request header such as: Req.query.username. Post request body such as: req.body.username

Nodejs is modular for Frame Express with background: The purpose of modularity is to:
    • Resolving the problem of variable name conflicts
    • Dependency management issues
Commonjs specification: Each JS file is a module
    • 1. Excuse for exposing the module through the Module.exports
    • 2. Introduce the module via require (path)

Nodejs Three-layer Separation

1. Presentation Layer

2. Service Layer

3. Persistence layer

Nodejs structure with express: MyApp

1.bin

2.dao Persistence layer

Model

user.js describes the types of data structures received by mongoose

database.js Setting Mongoose

userdao.js introduced mongoose with require

3.doc folder where spec files are placed

4.node_modules placing dependent files

5.public Place static files, such as JS CSS html

6./routes Presentation Layer

Index.js

users.js need to use Require. ("") introduces the JS file in service (services layer) userservice.js

7.service Service Layer

Userservicejs need to use Require. ("") introduces the JS file in DAO (persistence layer) userdao.js

8.views

9.app.js need to use Require. ("address") introduces a JS file in DAO (persistence layer)

10.nmp-debug.log

11.package.json

First entry JavaScript knowledge points (eight)

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.